Skip to content

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

Open

Description

@trackoor

Environment

  • Operating System: Linux
  • Node Version: v26.13.0
  • Package Manager: npm@11.20.0
  • 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):

Minimal script (npm i comark@0.9.2 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/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions