Javascript 将 Markdown/Textile 转换为 HTML(理想情况下,返回 Markdown/Textile)
有几个很好的用于 Markdown / Textile 的 Javascript 编辑器(例如:http://attacklab.net/ showdown/,我现在正在使用的那个),但我需要的只是一个从 Markdown / Textile 转换字符串的 Javascript 函数 -> HTML 和返回。
最好的方法是什么? (理想情况下,它是 jQuery 友好的 - 例如,$("#editor").markdown_to_html()
)
编辑: 另一种说法是,我寻找 Rails 的 textilize()
和 markdown()
文本助手的 Javascript 实现
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/, the one I'm using right now), but all I need is a Javascript function that converts a string from Markdown / Textile -> HTML and back.
What's the best way to do this? (Ideally it would be jQuery-friendly -- e.g., $("#editor").markdown_to_html()
)
Edit: Another way to put it is that I'm looking for a Javascript implementation of Rails' textilize()
and markdown()
text helpers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对于 Markdown -> HTML,有 Showdown
StackOverflow 本身使用 Markdown 语言进行问答;你尝试看看它是如何工作的吗?
好吧,它似乎正在使用 PageDown ,它可以在 MIT 许可证下使用
问题 有没有好的 Markdown Javascript 库或控件?及其答案也可能有帮助:-)
当然,完整的编辑器并不完全是您所要求的;但他们必须使用某种函数将 Markdown 代码转换为 HTML;并且,根据这些编辑器的许可证,您也许能够重复使用该函数...
实际上,如果您仔细查看 Showdown,会发现其代码源(文件 showdown.js),您会发现这部分注释:
它不是 jQuery 语法,但应该很容易集成到您的应用程序中;-)
关于纺织,似乎很难找到有用的东西:-(
在另一边,HTML -> Markdown,我想事情可能会有点困难...
我会做的是将 Markdown 和 HTML 都存储在我的应用程序数据存储(数据库?)中,并使用一个用于编辑,另一个用于渲染...会需要更多空间,但它似乎比“解密” HTML 风险要小......
For Markdown -> HTML, there is Showdown
StackOverflow itself uses Markdown language for questions and answers ; did you try to take a look at how it works ?
Well, it seems it is using PageDown which is available under the MIT License
The question Is there any good Markdown Javascript library or control? and its answers might help, too :-)
A full editor is, of course, not exactly what you asked for ; but they must use some kind of function to transform the Markdown code to HTML ; and, depending on the license of these editors, you might be able to re-use that function...
Actually, if you take a close look at Showdown, in its code source (file showdown.js), you'll find this portion of comment :
It's not jQuery syntax, but should be quite easy to integrate in your application ;-)
About Textile, it seems to be a bit harder to find anything useful :-(
In the other side, HTML -> Markdown, I guess things might be a bit harder...
What I would do is store both Markdown and HTML in my application data store (database ? ), and use one for editing, and the other for rendering... Would take more space, but it seems less risky than "decrypting" HTML...
我认为在这里列出 JavaScript 解决方案及其缩小(未压缩)的大小和优点/缺点是值得的。缩小代码的压缩大小约为未压缩大小的 50%。归结为:
所有这些都使用 MIT 许可证,大部分都在 npm 上。
markdown-it:104KB。 自 CommonMark 迁移以来为 StackExchange 提供支持。遵循 CommonMark 规范,现在或多或少是黄金标准;支持语法扩展;默认情况下产生安全输出。快速地;与摊牌一样强大,但规模非常大。具有大量功能(例如同步滚动)。也是 http://dillinger.io/ 的基础。
摊牌:28KB。具有全面的 CommonMark 支持,并且是以前的黄金标准;明显小于 Markdown-It 但速度较慢。它是向下翻页的基础。
pagedown:8KB。 在 CommonMark 迁移之前为 StackExchange 提供支持。它非常强大,但缺少表格、定义列表、脚注等。除了 8KB 转换器脚本之外,它还提供编辑器和清理器脚本。
缩减:1.3KB。完全公开,我写的。比任何其他轻量级转换器更广泛的功能范围;处理大多数但不是全部的 CommonMark 规范。不推荐用于用户编辑,但对于在网络应用程序中呈现信息非常有用。没有内联 HTML。
标记:19KB。综合的;针对单元测试套件进行测试;支持自定义词法分析器规则。
micromarkdown:5KB。支持很多功能,但缺少一些常见的功能,例如使用
*
的无序列表,以及一些严格来说不属于规范一部分的常见功能,例如受保护的代码块。许多错误,在大多数较长的文档上引发异常。我认为它是实验性的。nano-markdown:1.9KB。功能范围仅限于大多数文档使用的内容;比 micromarkdown 更强大,但并不完美;使用自己的非常基本的单元测试。相当稳健,但在许多边缘情况下都会出现问题。
mmd.js:800 字节。这是努力制作尽可能最小且仍然可用的解析器的结果。支持一小部分;需要为其量身定制文档。
markdown-js:54KB(缩小后无法下载;可能会缩小到约 20KB )。看起来很全面,包括测试,但我不是很熟悉。
meltdown:41KB(缩小后无法下载;可能会缩小到约 15KB)。 jQuery 插件; Markdown Extra(表格、定义列表、脚注)。
unified.js:各不相同,5-100KB。一个基于插件的系统,用于在 html、markdown 和散文之间进行转换。根据您需要的插件(拼写检查、语法突出显示、输入清理),文件大小会有所不同。可能更多地使用服务器端而不是客户端。
I thought it would be worthwhile to make a list here of the JavaScript solutions out there and their minified (uncompressed) size and strengths/weaknesses. Compressed size for minified code will be around 50% of uncompressed size. What it comes down to:
All of these use the MIT license, most are on npm.
markdown-it: 104KB. Powers StackExchange since the CommonMark migration. Follows the CommonMark spec and is now more or less the gold standard; supports syntax extensions; produces secure output by default. Fast; as robust as showdown, but very large. Has a ton of features (e.g. synced scrolling). Is also the basis for http://dillinger.io/.
showdown: 28KB. Has comprehensive CommonMark support and was previously the gold standard; is significantly smaller than Markdown-It but slower. it is the basis for pagedown.
pagedown: 8KB. Powered StackExchange before the CommonMark migration. It is very robust but missing tables, definition lists, footnotes, etc. In addition to the 8KB converter script, it also offers editor and sanitizer scripts.
drawdown: 1.3KB. Full disclosure, I wrote it. Broader feature scope than any other lightweight converter; handles most but not all of the CommonMark spec. Not recommended for user editing but very useful for presenting information in web apps. No inline HTML.
marked: 19KB. Comprehensive; tested against unit test suite; supports custom lexer rules.
micromarkdown: 5KB. Supports a lot of features, but is missing some common ones like unordered lists using
*
and some common ones that aren't strictly part of the spec like fenced code blocks. Many bugs, throws exceptions on most longer documents. I consider it experimental.nano-markdown: 1.9KB. Feature scope limited to things used by most documents; more robust than micromarkdown but not perfect; uses its own very basic unit test. Reasonably robust but breaks on many edge cases.
mmd.js: 800 bytes. The result of an effort to make the smallest possible parser that is still functional. Supports a small subset; document needs to be tailored for it.
markdown-js: 54KB (not available for download minified; would probably minify to ~20KB). Looks pretty comprehensive and includes tests, but I'm not very familiar with it.
meltdown: 41KB (not available for download minified; would probably minify to ~15KB). jQuery plugin; Markdown Extra (tables, definition lists, footnotes).
unified.js: varies, 5-100KB. A plugin-based system for converting between html, markdown, and prose. Depending on what plugins you need (spell-checking, syntax-highlighting, input sanitizing) the file size will vary. Probably used more server-side than client-side.
Textile
您可以此处找到 Textile 的看似非常精细的 Javascript 实现,以及另一个那里(也许不太好,但有一个很好的转换为你-输入示例页面)。
注意:我链接的第一个实现中存在一个错误:水平条未正确呈现。要修复它,您可以在文件中添加以下代码。
Textile
You can find a seemingly very fine Javascript implementation of Textile here, and another one there (maybe not so good, but has a nice convert-as-you-type example page).
Note: there is a bug in the first implementation I made a link to : horizontal bars are not rendered correctly. To fix it, you can add the following code in the file.
我正在使用微小的简约脚本 - mmd.js,它只支持 Markdown 可能性的子集,但这可能是人们所需要的全部,所以这个小于 1kb 的脚本是惊人的,不会是一个过大的杀伤力。
支持的功能
#
>
1
*
[]()
![]()
*
**
不支持的功能
I am using the tiny minimalistic script - mmd.js, which only supports a subset of Markdown possibilities, but this might be all that one would need anyway, so this script which is less than 1kb is amazing and won't be an overkill.
Supported features
#
>
1
*
[]()
![]()
*
**
Unsupported features
我发现这个问题很有趣,所以我决定开始做一些事情(仅替换
strong
和italic
降价标签)。在花了一个小时尝试使用正则表达式设计解决方案后,我放弃了并最终得到了以下结果,这似乎工作得很好。也就是说,它肯定可以进一步优化,我不确定它在这种形式下的真实弹性如何:测试代码:
输出:
编辑:V 0.024 中的新功能 - 自动删除未关闭的降价标签
I found this question intriguing, so I decided to start something off (only replaces
strong
anditalic
markdown tags). Having spent an hour trying to devise a solution using regexes, I gave up and ended up with the following, which seems to work nicely. That said, it can surely be further optimized and I'm not sure as to just how real-world resilient it will be in this form:Test code:
Output:
EDIT: New in V 0.024 - Automatic removal of unclosed markdown tags
无论是否使用 jQuery,都可以轻松使用 Showdown。这是一个 jQuery 示例:
It’s easy to use Showdown with or without jQuery. Here’s a jQuery example:
Showdown Attacklab-Link 已关闭,因此请使用 https://github.com/coreyti/showdown 进行转换需要:)
The Showdown Attacklab-Link is down so use https://github.com/coreyti/showdown for your conversion needs :)
这并没有解决整个请求(它不是一个编辑器),但textile-js是一个javascript渲染库: https://github.com/borgar/textile-js。演示位于 http://borgar.github.io/textile-js/
This doesn't address the entire request (it isn't an editor), but textile-js is a javascript rendering library: https://github.com/borgar/textile-js. A demonstration is available at http://borgar.github.io/textile-js/
让我们编写一个简单的 Markdown 解析器函数,它将接受一行 Markdown 并转换为适当的 HTML。为了简单起见,我们在语法中仅支持 Markdown 的一项功能:标题。
标头由 (1-6) 个哈希值指定,后跟空格,然后是文本。哈希值的数量决定了 HTML 输出的标头级别。
Let's write a simple markdown parser function that will take in a single line of markdown and be translated into the appropriate HTML. To keep it simple, we'll support only one feature of markdown in syntax: headers.
Headers are designated by (1-6) hashes followed by a space, followed by text. The number of hashes determines the header level of the HTML output.
markdown-js 是一个很好的 javascript markdown 解析器,一个带有测试的活跃项目。
markdown-js is a nice javascript markdown parser, an active project with tests.
您是否看过 Mylyn 的 Eclipse WikiText 库?它将从许多 wiki 语法转换为 xhtml 和 xdocs/DITA。看起来很酷。
http://help .eclipse.org/galileo/topic/org.eclipse.mylyn.wikitext.help.ui/help/Markup-Conversion.html
有人找到了 HTML->textile 问题的解决方案吗?我们当前的所有文档都是 M$ Word 格式,我们很乐意将其引入 Redmine Wiki 中进行协作维护。我们还没有找到任何可以进行转换的工具。我们发现 Open Office 扩展可以生成 mediawiki 格式的文本,但 Redmine Wiki 使用的是 Textile 的子集。
有人知道有一种工具可以将 mediawiki、Word、XDocs 或 HTML 转换为TO 纺织品吗?
Have you looked at the Eclipse WikiText library that is part of Mylyn. It will convert from many wiki syntax to xhtml and to xdocs/DITA. It looks way cool.
http://help.eclipse.org/galileo/topic/org.eclipse.mylyn.wikitext.help.ui/help/Markup-Conversion.html
Has anyone found a solution to the HTML->textile problem? All of our current documentation is in M$ Word format and we would love to bring it into Redmine Wiki for collaborative maintenance. We have not found any tool that will make the conversion. We have found the Open Office extension that produces mediawiki formatted text but Redmine Wiki uses a subset of textile.
Anyone know of a tool that converts TO textile from mediawiki, Word, XDocs, or HTML?
对于 Textile:
我最近修补了一个 HTML 到 Textile 转换器:https: //github.com/cmroanirgo/to-textile
对于反向 Textile 到 HTML,我使用并推荐 https://github.com/borgar/textile-js,其他答案已经提到过。
For Textile:
I've recently patched together an HTML to Textile converter: https://github.com/cmroanirgo/to-textile
For the reverse Textile to HTML, I use and recommend https://github.com/borgar/textile-js, which other answers have already mentioned.
我发现 karim79 的答案很有用。我稍微修改了一下,所以我想分享我的版本。也许它会对那里的人有所帮助。
我修改了答案,将其从 JavaScript 调整为 PHP(我知道问题没有 PHP 标签)
我还从
while()
更改为for()
因为:它仍然不完美,例如它会“加强”两个远距离之间的任何东西分开*,消耗它们两者。但我认为这也是 WhatsApp 的行为方式。
我认为有比我所做的更好的强制循环结束的方法,我只是遵循“如果它没有损坏,就不要修复它”的规则。我很高兴收到建议
I found the answer by karim79 useful. I modified it a bit, so I want to share my version. Maybe it will help someone out there.
I modified the answer to adapt it from JavaScript to PHP (I know the question doesn't have a PHP tag)
I also changed from
while()
tofor()
because:It's still not perfect, it will for example "strongify" anything between two far apart *'s, consuming both of them. But I think this is how whatsapp behaves also.
I assume there are better ways of forcing the end of the loop than what I did, I just followed the rule "if it aint broken, don't fix it". I'm happy to receive suggestions