Skip to content

feat: map code block source ranges - #72

Merged
luojiyin1987 merged 4 commits into
masterfrom
feat/source-map-code-blocks
Jul 18, 2026
Merged

feat: map code block source ranges#72
luojiyin1987 merged 4 commits into
masterfrom
feat/source-map-code-blocks

Conversation

@luojiyin1987

@luojiyin1987 luojiyin1987 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Closes #67.

What changed

  • Add source mappings for fenced and indented block-code value fields.

  • Support MarkdownSourceMap.getSourceRange() with MarkdownCodeNode.

  • Exclude syntax that does not belong to code.value, including:

    • opening and closing fences;
    • language, info, and meta strings;
    • stripped fenced-code indentation;
    • four-column indented-code prefixes;
    • blockquote and list container prefixes.
  • Preserve LF, CR, CRLF, blank lines, trailing-line behavior, tab indentation, and per-code-unit mappings.

  • Support fenced and indented code nested inside blockquotes, lists, and blockquote-list combinations.

  • Map empty fenced-code values to an accurate content insertion point.

  • Detect post-parse mutations of mapped code.value fields and throw SourceMapConsistencyError.

  • Validate reconstructed source content against the parsed value before accepting a mapping, avoiding fabricated source ranges.

  • Update the public API report, README, CHANGELOG, and package type-contract tests.

Implementation notes

Code-block mappings are reconstructed from each node’s original source position and parsed value.

The implementation removes container prefixes, fences, and parser-stripped indentation on a line-by-line basis. A mapping is accepted only when the reconstructed source content exactly matches node.value; otherwise, source mapping remains unavailable rather than returning an incorrect range.

Internal source-map segment types remain private.

Test coverage

Added coverage for:

  • fenced code with LF, CR, and CRLF;
  • backtick and tilde fences;
  • info and meta strings;
  • opening-fence indentation;
  • indented code with spaces and tabs;
  • blank lines and trailing line endings;
  • blockquote-contained fenced and indented code;
  • list-contained fenced and indented code;
  • combined blockquote and list containers;
  • empty fenced-code content points;
  • whole-value and per-code-unit ranges;
  • modified code.value consistency errors;
  • AST parity between parseMd() and parseMdWithSourceMap().

Validation

  • pnpm run lint
  • pnpm run build
  • pnpm run test
  • GitHub Actions on Node.js 20, 22, and 24
  • API Extractor report check
  • package type-contract tests
  • source-map benchmark smoke test on Node.js 22
  • Source-map Core compatibility workflow against the packed candidate: passed

@luojiyin1987
luojiyin1987 marked this pull request as ready for review July 18, 2026 14:39
@luojiyin1987
luojiyin1987 merged commit ca43a02 into master Jul 18, 2026
14 checks passed
@luojiyin1987
luojiyin1987 deleted the feat/source-map-code-blocks branch July 18, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

source map: map code.value for fenced and indented blocks

1 participant