Skip to content

Bump markdown-it to 15.0.1 - #639

Merged
joelzwarrington merged 10 commits into
mainfrom
joelzwarrington/bump-markdown-it
Oct 6, 2026
Merged

joelzwarrington merged 10 commits into
mainfrom
joelzwarrington/bump-markdown-it

Conversation

@joelzwarrington

Copy link
Copy Markdown
Contributor

Summary

Upgrades markdown-it from 12.3.2 to 15.0.1 and updates Markdoc's tokenizer/type layer to match its new API and shipped types.

  • markdown-it 15 is bundled/ESM-first: no more markdown-it/lib/* subpath imports, so Tokenizer and the plugins (annotations, comments, frontmatter, link) now import types off the main markdown-it module (MarkdownIt.StateBlock, MarkdownIt.MarkdownItOptions, etc.).
  • markdown-it now ships its own types, which removed the markdown-it/lib/token module path we were augmenting in global.d.ts. Replaced that global augmentation with a local Token type in src/types.ts that covers both real markdown-it tokens and Markdoc's own lightweight pseudo-tokens, and added the null-safety (token.tag ?? ..., token.attrs ?? [], etc.) the new optional fields require.
  • Re-added the allowIndentation patch, regenerated for 15.0.1. markdown-it 15 ships bundled dist/markdown-it.cjs.js/.mjs instead of per-rule lib/*.js files, so the patch now guards the same 16 call sites in both bundled files instead of across nine small modules. Restored patch-package as a devDependency and the prebuild script that applies it.

Why the patch is still needed

We looked at dropping the patch instead of porting it, since patches on bundled files are more brittle across upgrades. Without it, marktest fails: markdown-it treats 4+ spaces of leading indentation as a code block, which conflicts with content Markdoc needs to parse as regular blocks. Re-applying the patch unblocks this upgrade; longer-term we should look at upstreaming a proper option for this instead of patching bundled internals every version bump.

Test plan

  • npm run build (patch applies cleanly via prebuild)
  • npm test — 267 specs, 0 failures
  • npm run marktest — passes with the patch applied

Restores the patch-package tooling (prebuild script and devDependency)
so the allowIndentation patch can be regenerated against the new
version in a follow-up commit.

Committed-By-Agent: claude
markdown-it 15 ships bundled dist/markdown-it.cjs.js and .mjs files
instead of the old per-rule lib/*.js files, so the patch now targets
the same 16 indentation guards in both bundled files rather than the
individual rule modules.

Committed-By-Agent: claude
markdown-it 15 ships its own TypeScript types and no longer exposes
markdown-it/lib/token as an augmentable module path, so the global.d.ts
declaration merging no longer works. Define Markdoc's own loose Token
shape in src/types.ts instead; consuming files are updated to use it
in the next commit.

Committed-By-Agent: claude
Switch these files from importing Token off markdown-it/lib/token to
the local Token type, and add optional chaining/fallbacks for fields
(tag, attrs, info, nesting) that the new type marks as optional.

Committed-By-Agent: claude
markdown-it 15 drops the /lib subpath exports (markdown-it/lib,
markdown-it/lib/rules_block/state_block, etc.) in favor of named
exports off the main markdown-it module, and MarkdownIt.Options was
renamed to MarkdownItOptions. Update the tokenizer and its plugins
(annotations, comments, frontmatter, link) accordingly.

Committed-By-Agent: claude

@maxblank-stripe maxblank-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the optional token properties coming from updated markdown-it library or because we defined them as optional in src/types.ts? My main concerns is using non-null assertion operator for those optional properties.

Comment thread src/tokenizer/plugins/annotations.ts Outdated
const start = token.info.indexOf(OPEN);
const end = findTagEnd(token.info, start);
const content = token.info.slice(start + OPEN.length, end);
if (token.info!.includes(OPEN)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is token.info optional here? should we have a check above to prevent "Cannot read property includes of undefined"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is in part an issue due to how we were previously typing this information through global module augmentation. I've updated this code so typing information is easier to understand.

Comment thread src/tokenizer/plugins/annotations.ts Outdated
continue;

token.children = parseTags(token.content, token.map[0]);
token.children = parseTags(token.content!, token.map![0]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for token.content and token.map, how come they're optional now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#639 (comment) helps explain this a bit, I believe this is also an issue caused by removal of global.d.ts module augmentation. So we have to do a bit more type checking on our end :)

Comment thread src/parser.ts Outdated
switch (type) {
case 'heading':
return { level: Number(token.tag.replace('h', '')) };
return { level: Number((token.tag ?? '').replace('h', '')) };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if token.tag is undefined, level would be 0, which is invalid heading.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to token.tag!.

Comment thread src/parser.ts
let align;
if (attrs.style) {
if (attrs.style.includes('left')) {
const style = String(attrs.style);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what's the TS type of attrs.style?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an string | number, String() normalizes to be only string.

CleanShot 2026-09-11 at 09 03 56 CleanShot 2026-09-11 at 09 04 46

Review feedback pointed out that the non-null assertions added for
markdown-it 15 were masking real distinctions: some fields (info,
content, tag) are always set on real markdown-it tokens and never
needed loosening, while map is genuinely nullable and deserved a
guard instead of a silent assertion. The heading/fence `?? ''`
fallbacks were also risky since they could produce a bogus level 0
heading instead of surfacing a real problem.

Committed-By-Agent: claude
CI's lint step flagged it — the FenceToken type ended up using
Pick<Token, 'errors'> instead of referencing ValidationError directly.

Committed-By-Agent: claude
An `as FenceToken[]` cast on the whole tokens array hid that no unsafe
narrowing was actually needed: FenceToken only adds optional fields on
top of markdown-it's real Token, so the compiler can verify the
widening on its own via a plain variable declaration.

Committed-By-Agent: claude
Reviewers asked why this type exists rather than just using
markdown-it's Token directly; note the prior module-augmentation
approach for context on why this local type is needed instead.

Committed-By-Agent: claude
empireunitedkingdoms-cmd

This comment was marked as spam.

@joelzwarrington
joelzwarrington merged commit df5ac9a into main Oct 6, 2026
5 checks passed
@joelzwarrington
joelzwarrington deleted the joelzwarrington/bump-markdown-it branch October 6, 2026 17:47
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.

5 participants