Markdown:列表转换不完美

8

我正在尝试使用Markdown创建一个列表。根据我所阅读的一些文档,如果我编写以下Markdown代码:

My list
* first item
* second item
* third item

Not in the list

我将得到与在HTML中编写此内容相同的结果:
<p>My list</p>
<li>
   <ul>first item</ul>
   <ul>second item</ul>
   <ul>third item</ul>
</li>
<p>Not in the list</p>

我使用Atom作为编辑器和Markdown预览器,一切正常,但是当我使用pandoc将我的Markdown文件转换为以下格式时:

pandoc test.md -o test.odt

我得到的是这个:
My list * first item * second item * third item
Not in the list

我做错了什么?


5
"My list" 后面缺少空行。您可以在 StackOverflow 的帖子预览中测试此问题。 - melpomene
哇,那是我犯的一个简单又愚蠢的错误……非常感谢你! - Ommadawn
为什么不将这个作为答案,这样问题就能被关闭了。 - Alex Harvey
如果melpomene在几天内没有完成,你可以做这件事,@AlexHarvey :) - Ommadawn
我不明白这与编程无关。在 Stack Overflow 上有大约 5000 个标记为“markdown”的问题。 - Alex Harvey
显示剩余3条评论
1个回答

11

你的问题有两个可能的解决方案:

  1. Add a blank line between the paragraph and the list (as @melpomene mentioned in a comment).

    My list
    
    * first item
    * second item
    * third item
    
    Not in the list
    
  2. Leave out the blank line and tell Pandoc to use commonmark as the input format rather than the default, markdown.

    pandoc -f commonmark -o test.odt test.md
    

问题在于Atom编辑器使用的是CommonMark解析器,而默认情况下Pandoc使用一种老式Markdown解析器,该解析器基本遵循这些规则和参考实现(markdown.pl)。事实上,Commonmark规范明确承认了这种差异:

In CommonMark, a list can interrupt a paragraph. That is, no blank line is needed to separate a paragraph from a following list:

Foo
- bar
- baz

<p>Foo</p>
<ul>
<li>bar</li>
<li>baz</li>
</ul>

Markdown.pl does not allow this, through fear of triggering a list via a numeral in a hard-wrapped line:

The number of windows in my house is
14.  The number of doors is 6.
如果您希望工具之间拥有共同的行为,那么您只需要使用遵循相同行为的工具即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接