【问题标题】:Marked returns wrong html for a list item with nested list and a paragraph标记为具有嵌套列表和段落的列表项返回错误的 html
【发布时间】:2016-08-21 22:36:15
【问题描述】:

当尝试将这个简单的 Markdown 转换为 html 时:

* section one
    * item one
    * item two

    This is the first section

* section two
    * item one
    * item two

    This is the second section

我收到的 html 是:

<ul>
    <li>
        <p>section one</p>
        <ul>
            <li>item one</li>
            <li>
                <p>item two</p>
                <p>This is the first section</p>
            </li>
        </ul>
    </li>
    <li>
        <p>section two</p>
        <ul>
            <li>item one</li>
            <li>
                <p>item two</p>
                <p>This is the second section</p>
            </li>
        </ul>
    </li>
</ul>

第一级列表中的段落是嵌套列表的第二个列表项的一部分。
我希望这一段是嵌套列表的兄弟,我什至在不同的在线编辑器中对其进行了测试,它们按照我的预期呈现,而不像 marked 的结果。

我做错了什么还是marked 中的错误?
我尝试使用这些选项,但没有任何帮助。

代码如下:

const marked = require("marked");
let str = "* section one\n\t* item one\n\t* item two\n\n\tThis is the first section";
str += "\n\n* section two\n\t* item one\n\t* item two\n\n\tThis is the second section";

marked(str)

【问题讨论】:

    标签: javascript markdown javascript-marked


    【解决方案1】:

    这绝对是一个错误。正如您所注意到的,other implementations 按您的预期呈现。

    但是,可能会认为您的文档中存在轻微错误。您应该在(外部)列表项的第一行和嵌套列表之间添加一行。

    假设您正在创建一个仅包含第一个(外部)列表项内容的文档。当您对列表进行格式化后,它看起来像这样:

    section one
    * item one
    * item two
    
    This is the first section
    

    大多数 Markdown 实现 interpret 在以下之一:

    <p>section one * item one * item two</p>
    <p>This is the first section</p>
    

    <p>section one <em> item one </em> item two</p>
    <p>This is the first section</p>
    

    当然,第一行和列表之间需要一个空行。像这样:

    section one
    
    * item one
    * item two
    
    This is the first section
    

    始终renders 为:

    <p>section one</p>
    <ul>
        <li>item one</li>
        <li>item two</li>
    </ul>
    <p>This is the first section</p>
    

    将其应用于您的完整文档,如下所示:

    * section one
    
        * item one
        * item two
    
        This is the first section
    
    * section two
    
        * item one
        * item two
    
        This is the second section
    

    你会得到好的,一致的results

    嗯,好吧,事实证明 Marked 也弄错了。绝对是一个错误。

    【讨论】:

    • 谢谢。我开了new issue,看看他们怎么说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多