Skip to content

refactor(markdown-toolkit): read internals only from a known element - #197

Merged
Jason3S merged 4 commits into
mainfrom
claude/md-internals-overload
Nov 11, 2026
Merged

Jason3S merged 4 commits into
mainfrom
claude/md-internals-overload

Conversation

@Jason3S

@Jason3S Jason3S commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

markdownInternalsOf now takes an element and returns its internals, with no undefined. It no longer doubles as the test for "is this an element": callers check first, and a new assertIsMDElement does that check where a public function receives a caller's value. No behavior changes.

Changes

One signature.

export function markdownInternalsOf(element: MDElement): MarkdownInternals;

A new assert, assertIsMDElement(value, expected). It throws a TypeError that names what was expected and what was given, and it narrows the value to MDElement. It has its own tests.

Callers that asserted through the read now test first.

  • substitutePlaceholders calls assertIsMDElement. The error class and message are the same as before, so its existing test is unchanged.
  • inlineNodes and blockNodes rely on isKind, which they already called.

Fallbacks removed. Seven callers read from this or from a value just checked with isFrontmatter or isListItem, so their ?. and ?? [] could never run:

  • MarkdownElement.toString
  • document
  • DocumentElement.frontmatter, withFrontmatter (twice) and withStyle
  • list

Guide example. docs/writing-comments.md quoted the old comment as its example of an undefined contract. It now quotes readFrontmatter's comment, which still has one.

The type holds the non-public side of an element: its syntax tree and
the style to write it out with. Nothing in it has been rendered, so
"Rendering" named the wrong thing, and the key, the function and the
local variables all took that name from it.

The type is now `MarkdownInternals`, and the rest follow:
`markdownInternalsKey`, `markdownInternalsOf` and `markdownInternals`.
No behavior changes.
Called with an element, the function now returns its internals without
`undefined` in the type. Called with anything else, it still returns
`undefined` for a non-element.

Seven callers already hold an element, so their `?.` and `?? []`
fallbacks could never run. They are gone.
`markdownInternalsOf` now has one signature: it takes an element and
returns its internals. It no longer doubles as the test for "is this an
element", so the overload that returned `undefined` is gone.

The three places that asserted through it test first:

- `substitutePlaceholders` calls the new `assertIsMDElement`, which
  throws the same TypeError and message as before.
- `inlineNodes` and `blockNodes` rely on `isKind`, which they already
  called.

The comment guide quoted the old comment as its example of an
`undefined` contract, so it now quotes `readFrontmatter`'s.
@Jason3S Jason3S changed the title refactor(markdown-toolkit): overload markdownInternalsOf for elements refactor(markdown-toolkit): read internals only from a known element Nov 11, 2026
…verload

# Conflicts:
#	docs/writing-comments.md
#	packages/markdown-toolkit/src/builder/document.ts
#	packages/markdown-toolkit/src/builder/list.ts
#	packages/markdown-toolkit/src/element/content.ts
#	packages/markdown-toolkit/src/element/element.ts
#	packages/markdown-toolkit/src/element/index.ts
#	packages/markdown-toolkit/src/template/substitute.ts
@Jason3S
Jason3S marked this pull request as ready for review November 11, 2026 06:48
@Jason3S
Jason3S merged commit 1fa60f8 into main Nov 11, 2026
8 checks passed
@Jason3S
Jason3S deleted the claude/md-internals-overload branch November 11, 2026 06:57
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.

1 participant