Skip to content

feat: expose code block structure info from the source map - #142

Merged
luojiyin1987 merged 6 commits into
masterfrom
feat/code-source-info
Sep 29, 2026
Merged

luojiyin1987 merged 6 commits into
masterfrom
feat/code-source-info

Conversation

@luojiyin1987

@luojiyin1987 luojiyin1987 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add MarkdownSourceMap.getCodeSourceInfo(node).
  • Return { kind: 'indented' } or { kind: 'fenced', openingFence, infoInsertPoint }.
  • Record the structure during parsing. Do not rescan the Markdown.
  • Record the opening fence sequence and opening-line end directly from parser events; the latter also initializes emptyOffset.
  • Export CodeSourceInfo, FencedCodeSourceInfo, and IndentedCodeSourceInfo.

Closes #141.

API

openingFence is the fence sequence range. It excludes the container prefix,
the indentation, the info string, and the line ending.

infoInsertPoint is the end of the opening fence line, before its line ending.
Insert an info string there when node.lang is absent. The point stays before
\r in a CRLF ending. If the opening fence line has no line ending, the point
is the end of the input.

getCodeSourceInfo throws SourceMapUnavailableError for a node from another
document, a node added after parsing, or a node that is not a block code node.

Implementation

The parser already separates the two code paths. onentercodefenced and
onexitcodefenced build a FencedCodeRecording. onentercodeindented and
onexitcodeindented build a CodeValueRecording.

This change adds codeSourceInfos: WeakMap<object, RecordedCodeSourceInfo>.
The fenced path records the opening fence sequence and the opening line end.
The indented path records { kind: 'indented' }. build-source-map.ts only
converts the recorded offsets to ParsedPoint.

The openingFence range and the info insert point both come from the parser
events: codeFencedFenceSequence gives the fence run, and codeFencedFence
gives its line end. No code re-derives them from codeFenced start offsets.

Tests

__tests__/source-map/code-source-info.spec.ts has 38 cases:

  • backtick and tilde fences
  • three or more fence characters
  • LF, CR, and CRLF line endings
  • blockquote and list containers
  • tab indentation
  • empty and unclosed fenced blocks
  • indented code
  • fixer convergence and error paths

__tests__/types/package-exports.mts and package-exports.cts reference the
three new types and call getCodeSourceInfo, so a dropped CJS/ESM export fails
test:types.

Validation

  • pnpm test: 2017 tests passed.
  • pnpm run lint passed.
  • pnpm run test:types passed.
  • pnpm run test:package passed.
  • pnpm run build passed. The API Extractor report is updated.

@luojiyin1987
luojiyin1987 merged commit b052589 into master Sep 29, 2026
13 checks passed
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: expose code block structure info (fenced vs indented, openingFence, infoInsertPoint)

1 participant