我一直在尝试创建一个客户端编辑器,允许最终用户以 html 或 markdown 格式创建内容。用户有两个选项卡可以在两者之间切换。我设法找到一些将 markdown 转换为 html 的 javascript,因此如果用户一直在编写 markdown 并切换到 html 选项卡,则会显示 html 等效项。我还没有找到一个将 html 转换为 markdown 的 javascript,只有一个 python 脚本。
python脚本显然是服务器端的。这些选项卡只是其中带有脚本的超链接。当用户单击选项卡时,有什么方法可以转换 markdown html 吗?
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 been 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?
发布评论
评论(4)
当前接受的答案实际上告诉您在服务器端执行此操作。
要真正进行客户端转换,您可以尝试以下库之一(按受欢迎程度排序,以 GitHub 星级衡量):
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):
请随意尝试我的 lib,
reMarked.js
,用于客户端 html/DOM > markdownhttps://github.com/leoniya/reMarked.js
相反,你可以尝试
marked< /code>,但请注意,它不支持某些 php-markdown-extra 功能,例如解析漂亮的表格 http://michelf.ca/projects/php-markdown/extra/#table
https://github.com/chjj /标记/
feel free to try my lib,
reMarked.js
, for client-side html/DOM > markdownhttps://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/#tablehttps://github.com/chjj/marked/
您只需使用 AJAX 将数据发送到服务器,在服务器上执行转换,然后将结果返回给浏览器。在 jQuery 中,这很简单,例如:
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.:
为什么不使用 WMD 编辑器?它具有预览 html 的能力。
Why don't you use WMD-Editor? It has the ability to preview the html.