CommonMark在段落中间插入列表

时间:2016-05-26 15:11:11

标签: markdown commonmark

我希望以下文字(注意80个字符处的换行符):

This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds - maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error message
- a Postgres constraint failure will print a useful message, or a Javascript
exception will bubble up, or Javascript will complain about an unhandled
rejection.

在通过降价编辑器运行时,生成以下HTML:

<p>
This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds - maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error message
- a Postgres constraint failure will print a useful message, or a Javascript
exception will bubble up, or Javascript will complain about an unhandled
rejection.
</p>

当我通过cmark运行时,我得到以下内容:

<p>This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds - maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error message</p>
<ul>
<li>a Postgres constraint failure will print a useful message, or a Javascript
exception will bubble up, or Javascript will complain about an unhandled
rejection.</li>
</ul>
<p>With this error we didn't get any of those. We also observed it could happen
anywhere - it didn't seem to correlate with any individual test or test file.</p>

这似乎不正确 - 我绝对不希望列表在段落中间开始。这是规范中的一个问题,还是我可以做些不同的事情来触发这种行为?

2 个答案:

答案 0 :(得分:3)

简而言之,使用正确的字符,可能是emdash,而不是连字符。

规范states

  

在CommonMark中,列表可以中断段落。也就是说,不需要空行来将段落与以下列表分开:

Foo
- bar
- baz


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

后来解释说这是因为CommonMark遵守principle of uniformity。具体而言,以列表标记开头的行始终是列表项,而不管任何周围行。该规范甚至承认Markdown在这里的行为有所不同,以避免您遇到的问题,但CommonMark倾向于统一性而不是合理性/易用性(显然)。

因此,当该行不是列表项时,解决方案是永远不要使用列表标记开始一行。虽然你可以仔细包装你的线,但未来的编辑可能会重新引入问题。幸运的是,连字符(Unicode char \u2010,即键盘上的字符和用作列表标记的字符)很少以正确的英语语法用于列表标记。具体来说,连字符永远不应该跟空格一致,这是列表标记所必需的。如果你想按空格跟随字符,你可能需要一个endash(Unicode char \u2013),emdash(Unicode char \u2014)或减号(Unicode char \u2212)(参见{ {3}}进行解释)。因此,使用适当的字符可以避免问题。

让我们尝试一下:

This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds &mdash; maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error message
—  a Postgres constraint failure will print a useful message, or a Javascript
exception will bubble up, or Javascript will complain about an unhandled
rejection.

CommonMark的输出是:

<p>This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds — maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error message
— a Postgres constraint failure will print a useful message, or a Javascript
exception will bubble up, or Javascript will complain about an unhandled
rejection.</p>

请注意,我第一次使用HTML实体(&mdash;),而第二次使用时直接使用了emdash字符()。 CommonMark将HTML实体转换为emdash字符,并通过未更改的方式传递文字字符。为了便于阅读,实际角色肯定更好,虽然很难打字,因为它没有出现在大多数键盘上。

如果您使用this question,则可以使用双重(--)或三重(---)连字符,SmartyPants分别转换为endashes和emdashes。只要连字符之间没有空格,双连字符和三连字符就不会触发列表。

答案 1 :(得分:0)

  

有什么我能做的不同才能触发这种行为吗?

以下内容应避免列表:

This worked for a few months. Unfortunately, occasionally a test would time
out and fail after 18 seconds - maybe 1 in 100 test runs would fail with a
timeout. Normally when the tests fail you get back some kind of error 
message - a Postgres constraint failure will print a useful message, or a 
Javascript exception will bubble up, or Javascript will complain about an
unhandled rejection.