commonmark / commonmark/cmark

Line breaking in a code span can break the block structure

Open
#323 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
2k
Forks
691
Avg merge
1d 16h
Merged PRs (30d)
1

Description

In CommonMark rendering, line breaking in a code span can break the block structure. Here are just a few examples:

$ printf '`xx --- xx`' | cmark -t commonmark --width 3 | cmark -t html
<h2>`xx</h2>
<p>xx`</p>
$ printf '`xx ``` xx`' | cmark -t commonmark --width 3 | cmark -t html
<p>`xx</p>
<pre><code>xx`
</code></pre>
$ printf '`xx + x xx`' | cmark -t commonmark --width 3 | cmark -t html
<p>`xx</p>
<ul>
<li>x
xx`</li>
</ul>

In case anyone wants to discuss, here is my attempt to specify how I think CommonMark rendering should work:

Line breaking is only implemented inside a paragraph, not inside a heading or an HTML block.

Except inside raw HTML (which I've not thought about), any group of consecutive whitespace characters will turn into a single space or a newline followed by the current line prefix.

Break each line at the last breakable space that doesn't make the line too long. So a line will be wider than the specified width only if it contains no breakable spaces.

In code spans a space is breakable if and only if the following group of non-space characters does not match any of the following Perl regular expressions:

/^[#*-=_]+$/      thematic break, heading
/^(```|~~~)/      fenced code block
/^[<>]/           HTML block, block quote
/^[*+-]$/         list item
/^[0-9]+[).]$/    list item

In textual content all spaces are breakable. A backslash is inserted for the following groups of characters:

/[*<[\\_`]/       escape, code, emphasis, links, images, raw HTML
/[&][#A-Za-z]/    entity or numeric character reference
/^[#*-=_]+$/      thematic break, heading
/^(```|~~~)/      fenced code block
/^[<>]/           HTML block, block quote
/^[*+-]$/         list item
/^[0-9]+[).]$/    list item

In these expressions, ^ means the start of a line (perhaps following a line prefix) and $ means space or the end of the line.

The backslash is inserted in front of each group, except in the last case, where it is inserted in front of the parenthesis or dot.

I've almost certainly forgotten something in there.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported cases with the cmark commands in the issue, especially the code-span inputs rendered with --width 3. Read the proposed line-breaking rules and compare them with CommonMark parsing and rendering behavior. Done means line breaking inside code spans no longer changes headings, paragraphs, fenced code blocks, or list structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.