【问题标题】:Client side html markdown conversion客户端 html markdown 转换
【发布时间】:2011-01-27 11:20:22
【问题描述】:

我一直在尝试创建一个客户端编辑器,它允许最终用户在 html 或 markdown 中创建内容。用户有两个选项卡用于在两者之间切换。我设法找到了一些将 markdown 转换为 html 的 javascript,因此如果用户一直在编写 markdown 并切换到 html 选项卡,则会显示 html equivilant。我一直找不到将html转换为markdown的javascript,只有一个python脚本。

python 脚本显然是服务器端的。这些选项卡只是其中带有脚本的超链接。当用户单击选项卡时,有什么方法可以转换 Markdown html?

【问题讨论】:

  • 您确定要人们直接编辑 html 吗?最好的过渡将是有损的。有很多东西在markdown中不能很好的表现出来。例如,如果我添加<span class='blue'>...</span>,您希望在降价中看到什么?

标签: javascript html markdown


【解决方案1】:

您只需使用 AJAX 将数据发送到服务器,在服务器上执行转换,然后将结果返回给浏览器。在 jQuery 中,这很简单,例如:

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

【讨论】:

    【解决方案2】:

    为什么不使用WMD-Editor?它具有预览html的能力。

    【讨论】:

      【解决方案3】:

      随意尝试我的 lib,reMarked.js,用于客户端 html/DOM > markdown

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

      反过来你可以试试marked,但要注意它不支持一些php-markdown-extra特性,比如解析漂亮的表格http://michelf.ca/projects/php-markdown/extra/#table

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

      【讨论】:

        【解决方案4】:

        当前接受的答案实际上告诉您在服务器端进行。

        要真正进行客户端转换,您可以尝试以下库之一(按受欢迎程度排序,按 GitHub 星数衡量):

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-30
          • 1970-01-01
          • 2012-01-03
          • 1970-01-01
          • 1970-01-01
          • 2017-05-11
          相关资源
          最近更新 更多