Pagedown Markdown Converter无法正常工作

时间:2018-05-19 07:15:55

标签: javascript editor markdown

我正在尝试使用pagedown - Markdown Converter将markdown文本转换为HTML。它格式不正确。

这是JsFiddle网址,它似乎适用于某些标记 - https://jsfiddle.net/oeraroks/

这是堆栈溢出代码段 -

var converter = Markdown.getSanitizingConverter();
jQuery('.mdl-card__markdown-content').each(function() {
  text = jQuery(this).text();
  vhtml = converter.makeHtml(text);
  vhtml = vhtml.replace(/(?:\r\n|\r|\n)/g, '<br>');
  jQuery(this).html(vhtml);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pagedown/1.0/Markdown.Sanitizer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pagedown/1.0/Markdown.Converter.min.js"></script>

<div class="mdl-card__markdown-content">
**DISCLAIMER** He is also known for playing Hal in Malcom in the Middle. Bryan Cranston played the role of Walter in Breaking Bad.
* He is also known for playing Hal in Malcom in the Middle.
* Bryan Cranston played the role of Walter in Breaking Bad.
* He is also known for playing Hal in Malcom in the Middle. Bryan Cranston played the role of Walter in Breaking Bad.
* He is also known for playing Hal in Malcom in the Middle.
* Bryan Cranston played the role of Walter in Breaking Bad. He is also known for playing Hal in Malcom in the Middle.
</div>

如果你可以在JSFiddle中看到,你会看到只有Disclaimer文字变得粗体。在预期列表的所有位置保持不变且没有变化。

常见问题

  1. 我使用的是SimpleMDE编辑器 - https://github.com/sparksuite/simplemde-markdown-editor
  2. 由于它无法将其更改为HTML,因此我必须向下看pagedown转换器,以便将markdown text转换为html text

    寻求将上述文本转换为html的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

我对markdown本身并不是很好,但我注意到的一件事是我需要在段落和列表之间休息才能意识到列表就在那里。

<div class="mdl-card__markdown-content">
**DISCLAIMER** He is also known for playing Hal in Malcom in the Middle. 
Bryan Cranston played the role of Walter in Breaking Bad.

* He is also known for playing Hal in Malcom in the Middle.
* Bryan Cranston played the role of Walter in Breaking Bad.
* He is also known for playing Hal in Malcom in the Middle. Bryan Cranston 
played the role of Walter in Breaking Bad.
* He is also known for playing Hal in Malcom in the Middle.
* Bryan Cranston played the role of Walter in Breaking Bad. He is also known 
for playing Hal in Malcom in the Middle.
</div>

即使在本网站(您的版本)上也能看到差异:

免责声明他也因在中间的Malcom中扮演Hal而闻名。     布莱恩克兰斯顿扮演沃尔特在破坏中的角色。 *他也因在中间的Malcom中扮演Hal而闻名。 *布莱恩·克兰斯顿扮演沃尔特的角色。 *他也因在中间的Malcom中扮演Hal而闻名。布莱恩克兰斯顿 扮演沃尔特在打破坏人的角色。 *他也因在中间的Malcom中扮演Hal而闻名。 *布莱恩·克兰斯顿扮演沃尔特的角色。他也是众所周知的     在中间的Malcom玩Hal。

ws(带休息的版本):

免责声明他也因在中间的Malcom中扮演Hal而闻名。     布莱恩·克兰斯顿(Bryan Cranston)扮演沃尔特(Walter)的角色。

  • 他也因在中间的马尔科姆扮演哈尔而闻名。
  • 布莱恩·克兰斯顿扮演沃尔特的角色。
  • 他也因在中间的Malcom中扮演Hal而闻名。布莱恩克兰斯顿 扮演沃尔特在破坏中的角色。
  • 他也因在中间的马尔科姆扮演哈尔而闻名。
  • 布莱恩克兰斯顿扮演沃尔特在破坏中的角色。他也是众所周知的 在中间的马尔科姆玩哈尔。

然后你可能想删除

vhtml = vhtml.replace(/(?:\r\n|\r|\n)/g, '<br>');

让它看起来更漂亮