2016-08-20 64 views
1

當嘗試這個簡單的降價轉換爲HTML:標記返回與嵌套列表列表項和段落錯誤的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) 

回答

1

這絕對是一個錯誤。正如您注意到的那樣,other implementations按照您的預期渲染。

但是,您可能會認爲文檔中存在輕微錯誤。您應該在(外部)列表項目的第一行和嵌套列表之間添加一行。

假設您創建的文檔只包含第一個(外部)列表項的內容。正如你所列表格式,這將是這樣的:

section one 
* item one 
* item two 

This is the first section 

最減價實現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也有錯。絕對是一個錯誤。

+0

謝謝。我打開了一個[新問題](https://github.com/chjj/marked/issues/789),讓我們看看他們說了些什麼。 –