comark icon indicating copy to clipboard operation
comark copied to clipboard

`renderMarkdown` round trip changes AST for `0.` lists, `10.` list nesting, adjacent lists, `*_x_*`, and multi-line headings

Open trackoor opened this issue 2 weeks ago • 0 comments

Environment

  • Operating System: Linux
  • Node Version: v26.13.0
  • Package Manager: [email protected]
  • Comark Version: 0.9.2 (and main at 632e874)
  • Framework: None

Comark Version

v0.9.2 (also reproduces on main at 632e874)

Reproduction

Playground links (open the Formatted tab to see renderMarkdown output):

  • 0. start: https://comark.dev/play#code/M9BTqEotyucy1FPIz0vlAgA
  • Adjacent lists: https://comark.dev/play#code/01VI5NJVSOLi0lZI5gIA
  • Nesting under 10.: https://comark.dev/play#code/MzTQUyhJzeNSAAJdhbzU4pLUFC4A
  • Nested emphasis: https://comark.dev/play#code/04pPy8-P1-ICAA
  • Multi-line heading: https://comark.dev/play#code/c8vP50pKLOKytbXlAgA

Minimal script (npm i [email protected] and node repro.mjs):

import { parseMarkdown } from 'comark'
import { renderMarkdown } from 'comark/render'

const cases = [
  '0. zero\n1. one\n',
  '- a\n- b\n\n+ c\n',
  '10. ten\n    - nested\n',
  '*_foo_*\n',
  'Foo\nbar\n===\n',
]

for (const md of cases) {
  const before = await parseMarkdown(md)
  const rendered = await renderMarkdown(before)
  const after = await parseMarkdown(rendered)
  console.log(JSON.stringify({
    input: md,
    rendered,
    before: before.nodes,
    after: after.nodes,
  }))
}

Description

renderMarkdown produces Markdown that re-parses to a different AST in five CommonMark cases:

  1. Ordered list starting at 0: 0. zero\n1. one\n renders as 1. zero\n2. one, losing start: "0". handlers/ol.ts clamps start >= 1 ? start : 1 while IMPLICIT_ATTRS drops start, though CommonMark 0.31.2 allows 0 as an ordered list start number.
  2. Adjacent lists of the same kind: - a\n- b\n\n+ c\n parses as two ul nodes, but renderMarkdown writes both with - , producing - a\n- b\n\n- c, which re-parses as one three-item ul. Adjacent ol nodes using . and ) merge the same way.
  3. Nested lists under item 10 or higher: 10. ten\n - nested\n (or item 10+ in a 1. list) renders as 10. ten\n - nested with 3 spaces of indent instead of 4, so - nested falls out of the item and ends the ol on re-parse. handlers/ol.ts hardcodes listIndent: 3 instead of using prefix.length from handlers/li.ts.
  4. Nested emphasis: *_foo_*\n (em inside em) renders as **foo**, which re-parses as strong. handlers/emphesis.ts always writes *...*.
  5. Multi-line headings: Foo\nbar\n===\n (['h1', { id: 'foo-bar' }, 'Foo\nbar']) renders as # Foo\nbar, which re-parses as h1 (Foo) followed by a p (bar).

Additional context

None of these five cases is in #525.

Logs

{"input":"0. zero\n1. one\n","rendered":"1. zero\n2. one","before":[["ol",{"start":"0"},["li",{},"zero"],["li",{},"one"]]],"after":[["ol",{},["li",{},"zero"],["li",{},"one"]]]}
{"input":"- a\n- b\n\n+ c\n","rendered":"- a\n- b\n\n- c","before":[["ul",{},["li",{},"a"],["li",{},"b"]],["ul",{},["li",{},"c"]]],"after":[["ul",{},["li",{},"a"],["li",{},"b"],["li",{},"c"]]]}
{"input":"10. ten\n    - nested\n","rendered":"10. ten\n   - nested","before":[["ol",{"start":"10"},["li",{},["p",{},"ten"],["ul",{},["li",{},"nested"]]]]],"after":[["ol",{"start":"10"},["li",{},"ten"]],["ul",{},["li",{},"nested"]]]}
{"input":"*_foo_*\n","rendered":"**foo**","before":[["p",{},["em",{},["em",{},"foo"]]]],"after":[["p",{},["strong",{},"foo"]]]}
{"input":"Foo\nbar\n===\n","rendered":"# Foo\nbar","before":[["h1",{"id":"foo-bar"},"Foo\nbar"]],"after":[["h1",{"id":"foo"},"Foo"],["p",{},"bar"]]}

Credit: https://ucsbplse.github.io/

trackoor avatar Nov 14 '26 10:11 trackoor