【问题标题】:CommonMark inserts list in the middle of paragraphCommonMark 在段落中间插入列表
【发布时间】:2016-09-24 16:32:10
【问题描述】:

我期望以下文本(注意 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.

通过 Markdown 编辑器运行时,生成以下 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>

这似乎不正确 - 我绝对不希望列表从段落中间开始。这是规范中的问题还是我可以采取不同的措施来不触发这种行为?

【问题讨论】:

  • 有趣。这是与 Markdown 的分歧。见Babelmark

标签: markdown commonmark


【解决方案1】:

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

规范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)(请参阅this question 以获得解释) .因此,使用适当的字符,问题就避免了。

让我们试一试:

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 实体 (&amp;mdash;),而第二次出现时我直接使用了 emdash 字符 (—)。 CommonMark 将 HTML 实体转换为 emdash 字符,并通过原样传递文字字符。就可读性而言,实际字符肯定更好,但由于大多数键盘上不显示而难以键入。

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

【讨论】:

    【解决方案2】:

    我可以做些不同的事情来不触发这种行为吗?

    以下内容应避免列出:

    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.
    

    【讨论】:

    • 对,你可以把连字符加进去,这样它就不会开始行了,但这并不能防止将来出现问题。
    • 同意,这就是为什么我专门提出了一个替代方案来避免这种行为。
    • @mdickin 问题是在未来的编辑中,该段落可能会被重新包装并重新引入问题。这不是解决方案,只是临时解决方法。在我的回答中,我提供了一个解决方案,未来的编辑不会重新引入问题。
    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多