gpt4 book ai didi

javascript - 将 Markdown/ASCI(多行)转换为 HTML(nodejs)

转载 作者:行者123 更新时间:2023-11-30 15:52:58 25 4
gpt4 key购买 nike

我目前在我的编辑器中有这个..

* Line one
* Line two
some more info

在数据库中是这样的

* Line one\r\n* Line two\r\nsome more info

我正在使用 Markdown 包将 Markdown 转换为 HTML 以显示在我的网站上,这工作正常。

但是它忽略了换行符,因此给了我这个输出...

<ul><li>Line one</li><li>Line two\r\nsome more info</li></ul>

当我想要的输出是...

<ul><li>Line one</li><li>Line two</li></ul>some more info

我想我需要先从我的单行中创建一个“多行字符串”,然后再通过 Markdown 运行它?

对最佳方法有什么想法吗?

目前正在使用此代码

var markdown = require( "markdown" ).markdown;
var unMarkdownDescriptions = function(description){
//Check if currently contains HTML.
if(typeof description !== "undefined"){
if(description.indexOf("<") !=-1){
return description;
}else{

return html_content = markdown.toHTML(description);
}
}else{
return '';
}
}

当前代码检查是否已经作为 HTML 存储在数据库中并忽略它们(我们从 HTML 迁移到 MD,ERP 上生成的 HTML 在最好的时候是不可靠的!)

最佳答案

However it is ignoring the line breaks

根据Markdown spec,这就是它应该的行为方式:

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines. (A blank line is any line that looks like a blank line — a line containing nothing but spaces or tabs is considered blank.) Normal paragraphs should not be indented with spaces or tabs.

The implication of the "one or more consecutive lines of text" rule is that Markdown supports "hard-wrapped" text paragraphs. This differs significantly from most other text-to-HTML formatters (including Movable Type’s “Convert Line Breaks” option) which translate every line break character in a paragraph into a <br /> tag.

要获得所需的输出,只需在列表后添加一个空行:

* Line one
* Line two

some more info

关于javascript - 将 Markdown/ASCI(多行)转换为 HTML(nodejs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39023429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com