Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-bugs-reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-satteri': patch
---

Fixes `satteriHeadingIdsPlugin` sharing Slugger state across documents when reused as a user hast plugin, which caused headings with the same text in different posts to receive incorrect numeric suffixes
47 changes: 25 additions & 22 deletions packages/markdown/satteri/src/satteri-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,33 @@ export function collectHastText(
return text;
}

export function createHeadingIdsPlugin(): HastPluginDefinition {
export function createHeadingIdsPlugin(): HastPluginEntry {
const slugger = new Slugger();
// Collect headings in a separate array so we can make this idempotent
const headings: MarkdownHeading[] = [];
return {
name: 'heading-ids',
element: {
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
visit(node, ctx) {
const astro = ctx.data.astro;
const rawText = ctx.textContent(node);
const text = rawText.includes('frontmatter')
? collectHastText(node, astro?.frontmatter ?? {})
: rawText;
const existingId = node.properties?.id;
const slug = typeof existingId === 'string' ? existingId : slugger.slug(text);
const depth = Number.parseInt(node.tagName[1], 10);
headings.push({ depth, slug, text });
if (astro) astro.headings = headings;
if (typeof existingId !== 'string') {
ctx.setProperty(node, 'id', slug);
}
return () => {
slugger.reset();
// Collect headings in a separate array so we can make this idempotent
const headings: MarkdownHeading[] = [];
return {
name: 'heading-ids',
element: {
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
visit(node, ctx) {
const astro = ctx.data.astro;
const rawText = ctx.textContent(node);
const text = rawText.includes('frontmatter')
? collectHastText(node, astro?.frontmatter ?? {})
: rawText;
const existingId = node.properties?.id;
const slug = typeof existingId === 'string' ? existingId : slugger.slug(text);
const depth = Number.parseInt(node.tagName[1], 10);
headings.push({ depth, slug, text });
if (astro) astro.headings = headings;
if (typeof existingId !== 'string') {
ctx.setProperty(node, 'id', slug);
}
},
},
},
};
};
}

Expand Down
17 changes: 17 additions & 0 deletions packages/markdown/satteri/test/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ describe('satteri markdown', () => {
]);
});

it('resets heading slugs across multiple renders when `satteriHeadingIdsPlugin()` is reused', async () => {
const plugin = satteriHeadingIdsPlugin();
const processor = await createSatteriMarkdownProcessor({
hastPlugins: [plugin],
});
const first = await processor.render('## Introduction\n\n## Summary');
const second = await processor.render('## Introduction\n\n## Summary');
assert.deepEqual(first.metadata.headings, [
{ depth: 2, slug: 'introduction', text: 'Introduction' },
{ depth: 2, slug: 'summary', text: 'Summary' },
]);
assert.deepEqual(second.metadata.headings, [
{ depth: 2, slug: 'introduction', text: 'Introduction' },
{ depth: 2, slug: 'summary', text: 'Summary' },
]);
});

it('respects heading IDs set by a user hast plugin in both DOM and `headings`', async () => {
const setIdPlugin: HastPluginDefinition = {
name: 'set-heading-id',
Expand Down
Loading