Skip to main content

Module refine

Module refine 

Source
Expand description

This module refines the shape of already-rendered markdown without deleting any of its visible text.

It is not a general markdown formatter, and it is not cleanup (the noise-removal stage each un* renderer already ships). cleanup deletes — page-number lines, repeated headers/footers, mojibake. This module never deletes: it enforces two invariants across every pass it runs.

  • Lossless — every visible text token present before a pass is still present after it (a superset relation: a pass may add text, such as a recovered table separator row, but never remove any).
  • Idempotent — refine(refine(x)) == refine(x).

Because of those two invariants, this module composes with a renderer’s existing cleanup stage in either order, and never changes output that has already shipped to a consumer.

0.1.0 implements all five passes the design calls for: table shape normalization (missing-separator recovery and ragged-row widening), ordered-list renumbering (the round trip’s serializer configuration), link/image path normalization (backslash-to-forward-slash, duplicate-slash collapse — real URLs and protocol-relative references are left untouched), frontmatter normalization (canonical key: value spacing, and dropping an entry or list item that exactly repeats an earlier one; key order is left as authored, since nothing asked for it to be sorted), and section anchors (a GitHub-compatible slug assigned via CommonMark’s own heading- attributes extension, # text {#id}; a heading that already carries an id is left alone). refine always runs the CommonMark round trip (parse with pulldown-cmark, re-serialize with pulldown-cmark-to-cmark) every pass runs inside, plus the frontmatter handling and parse-failure fallback that round trip needs — that round trip alone is not a no-op (see roundtrip’s ordered-list renumbering, which changes output on any ordered list, not only a malformed one).

One thing this pass deliberately does not attempt: a link or image destination containing a raw space is not valid CommonMark syntax at all outside <...> — it never becomes a Link/Image event to normalize. Probing found all three un* renderers emit destinations unescaped, so this can happen on real input; the fix belongs at the emission site instead.

Structs§

RefineOptions
Options controlling which passes refine runs.

Functions§

refine
Refines the shape of markdown without deleting any of its visible text.