开发者

Client side html markdown conversion

I've been trying to create a client side editor which allows the end user to create content in html or markdown. The user has two tabs for switching between the two. I managed to find some javascript that converts markdown to html, so if a user has been writing markdown and switches to the html tab, the html equivilant is shown. I haven't be开发者_StackOverflow社区en able to find a javascript that converts html to markdown, only a python script.

The python script is obviously server side. The tabs are just hyperlinks with script in there. Is there any way I can convert the markdown html when the user clicks the tab?


The currently accepted answer actually tells you to do it on the server-side.

To really do client-side conversion, you could try one of these libraries (in order of popularity, measured by GitHub stars):

  • marked
  • showdown
  • markdown-it
  • markdown-js
  • reMarked.js


feel free to try my lib, reMarked.js, for client-side html/DOM > markdown

https://github.com/leeoniya/reMarked.js

the other way around you can try marked, but be aware that it doesn't support some php-markdown-extra features, like parsing pretty tables http://michelf.ca/projects/php-markdown/extra/#table

https://github.com/chjj/marked/


You only have to send the data to the server using AJAX, perform the conversion on the server and then return the results back to the browser. In jQuery this is as simple as e.g.:

$.ajax({
    type: "GET",
    url: <converter url>,
    data: <html>
    success: function(markdown_text){
        $('#id_container').text(markdown_text);
    }
    error: function(XMLHttpRequest, textStatus, errorThrown){
             alert('Error!');
    }
});


Why don't you use WMD-Editor? It has the ability to preview the html.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜