Skip to content

Commit 0c36d38

Browse files
fix(content): use md4x heading ids and release the streamMarkdown reader (#2770)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3f9677f commit 0c36d38

8 files changed

Lines changed: 95 additions & 87 deletions

‎packages/content/md4x/src/lib/md4x-content-renderer.service.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ Lorem ipsum 2....
161161
expect(result.toc[4].text).toBe('Level 3 ?? complex test && &@@');
162162
});
163163

164+
it('keeps ids aligned past raw HTML headings and honors {#id}', async () => {
165+
const renderer = setup();
166+
const result = await renderer.render(
167+
'<h2 class="raw">Raw</h2>\n\n## Custom {#my-id}\n\n## Next',
168+
);
169+
170+
expect(result.toc).toEqual([
171+
{ id: 'my-id', level: 2, text: 'Custom' },
172+
{ id: 'next', level: 2, text: 'Next' },
173+
]);
174+
expect(result.content).toContain('<h2 class="raw">Raw</h2>');
175+
expect(result.content).toContain('<h2 id="my-id">Custom</h2>');
176+
expect(result.content).toContain('<h2 id="next">Next</h2>');
177+
});
178+
164179
it('handles all six heading levels', async () => {
165180
const renderer = setup();
166181
const result = await renderer.render(

‎packages/content/md4x/src/lib/md4x-content-renderer.service.ts‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Injectable, inject, InjectionToken } from '@angular/core';
33
import {
44
ContentRenderer,
55
RenderedContent,
6-
TableOfContentItem,
76
} from '../../../src/lib/content-renderer';
87

98
/**
@@ -25,36 +24,6 @@ export interface Md4xRendererOptions {
2524
export const MD4X_RENDERER_OPTIONS: InjectionToken<Md4xRendererOptions> =
2625
new InjectionToken<Md4xRendererOptions>('md4x_renderer_options');
2726

28-
function makeSlug(text: string, slugCounts: Map<string, number>): string {
29-
const baseSlug = text
30-
.toLowerCase()
31-
.replace(/[^\w\s-]/g, '')
32-
.trim()
33-
.replace(/\s+/g, '-');
34-
const count = slugCounts.get(baseSlug) ?? 0;
35-
slugCounts.set(baseSlug, count + 1);
36-
return count === 0 ? baseSlug : `${baseSlug}-${count}`;
37-
}
38-
39-
function headingsToToc(
40-
headings: Array<{ level: number; text: string }>,
41-
): TableOfContentItem[] {
42-
const slugCounts = new Map<string, number>();
43-
return headings.map((h) => ({
44-
id: makeSlug(h.text, slugCounts),
45-
level: h.level,
46-
text: h.text,
47-
}));
48-
}
49-
50-
function injectHeadingIds(html: string, toc: TableOfContentItem[]): string {
51-
let tocIndex = 0;
52-
return html.replace(/<h([1-6])>/g, (match, level) => {
53-
const item = toc[tocIndex++];
54-
return item ? `<h${level} id="${item.id}">` : match;
55-
});
56-
}
57-
5827
/**
5928
* Content renderer backed by md4x (C-based CommonMark parser compiled with Zig).
6029
* 50-70x faster than marked for complex documents.
@@ -70,12 +39,8 @@ export class Md4xContentRendererService extends ContentRenderer {
7039
const html = renderToHtml(content, {
7140
heal: this.options?.heal,
7241
highlighter: this.options?.highlighter,
42+
headingIds: true,
7343
});
74-
const meta = parseMeta(content);
75-
const toc = headingsToToc(meta.headings);
76-
return {
77-
content: injectHeadingIds(html, toc),
78-
toc,
79-
};
44+
return { content: html, toc: parseMeta(content).headings };
8045
}
8146
}

‎packages/content/md4x/src/lib/md4x-wasm-content-renderer.service.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ describe('Md4xWasmContentRendererService', () => {
102102
expect(result.toc[0].text).toBe('Level 3 ?? complex && &@@');
103103
});
104104

105+
it('keeps ids aligned past raw HTML headings and honors {#id}', async () => {
106+
const renderer = setup();
107+
const result = await renderer.render(
108+
'<h2 class="raw">Raw</h2>\n\n## Custom {#my-id}\n\n## Next',
109+
);
110+
111+
expect(result.toc).toEqual([
112+
{ id: 'my-id', level: 2, text: 'Custom' },
113+
{ id: 'next', level: 2, text: 'Next' },
114+
]);
115+
expect(result.content).toContain('<h2 class="raw">Raw</h2>');
116+
expect(result.content).toContain('<h2 id="my-id">Custom</h2>');
117+
expect(result.content).toContain('<h2 id="next">Next</h2>');
118+
});
119+
105120
it('handles all six heading levels', async () => {
106121
const renderer = setup();
107122
const result = await renderer.render(

‎packages/content/md4x/src/lib/md4x-wasm-content-renderer.service.ts‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,12 @@ import { Injectable, inject } from '@angular/core';
33
import {
44
ContentRenderer,
55
RenderedContent,
6-
TableOfContentItem,
76
} from '../../../src/lib/content-renderer';
87
import {
98
MD4X_RENDERER_OPTIONS,
109
Md4xRendererOptions,
1110
} from './md4x-content-renderer.service';
1211

13-
function makeSlug(text: string, slugCounts: Map<string, number>): string {
14-
const baseSlug = text
15-
.toLowerCase()
16-
.replace(/[^\w\s-]/g, '')
17-
.trim()
18-
.replace(/\s+/g, '-');
19-
const count = slugCounts.get(baseSlug) ?? 0;
20-
slugCounts.set(baseSlug, count + 1);
21-
return count === 0 ? baseSlug : `${baseSlug}-${count}`;
22-
}
23-
24-
function headingsToToc(
25-
headings: Array<{ level: number; text: string }>,
26-
): TableOfContentItem[] {
27-
const slugCounts = new Map<string, number>();
28-
return headings.map((h) => ({
29-
id: makeSlug(h.text, slugCounts),
30-
level: h.level,
31-
text: h.text,
32-
}));
33-
}
34-
35-
function injectHeadingIds(html: string, toc: TableOfContentItem[]): string {
36-
let tocIndex = 0;
37-
return html.replace(/<h([1-6])>/g, (match, level) => {
38-
const item = toc[tocIndex++];
39-
return item ? `<h${level} id="${item.id}">` : match;
40-
});
41-
}
42-
4312
/**
4413
* Content renderer backed by md4x/wasm for client-side (browser) rendering.
4514
* ~100KB gzip, 3-6x faster than marked in the browser.
@@ -61,12 +30,8 @@ export class Md4xWasmContentRendererService extends ContentRenderer {
6130
const html = wasm.renderToHtml(content, {
6231
heal: this.options?.heal,
6332
highlighter: this.options?.highlighter,
33+
headingIds: true,
6434
});
65-
const meta = wasm.parseMeta(content);
66-
const toc = headingsToToc(meta.headings);
67-
return {
68-
content: injectHeadingIds(html, toc),
69-
toc,
70-
};
35+
return { content: html, toc: wasm.parseMeta(content).headings };
7136
}
7237
}

‎packages/content/md4x/src/lib/streaming-markdown-renderer.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,42 @@ describe('streamMarkdown', () => {
172172
await expect(reader.read()).rejects.toThrow('Stream interrupted');
173173
});
174174

175+
it('cancels and releases the input when the output is cancelled', async () => {
176+
let cancelReason: unknown;
177+
const input = new ReadableStream<string>({
178+
pull(controller) {
179+
controller.enqueue('# Chunk\n\n');
180+
},
181+
cancel(reason) {
182+
cancelReason = reason;
183+
},
184+
});
185+
const output = await streamMarkdown(input);
186+
const reader = output.getReader();
187+
await reader.read();
188+
189+
await reader.cancel('stop');
190+
191+
expect(cancelReason).toBe('stop');
192+
expect(input.locked).toBe(false);
193+
});
194+
195+
it('releases the input when it errors', async () => {
196+
const input = createErrorStream([], 0);
197+
const output = await streamMarkdown(input);
198+
199+
await expect(output.getReader().read()).rejects.toThrow();
200+
201+
expect(input.locked).toBe(false);
202+
});
203+
204+
it('releases the input on completion', async () => {
205+
const input = createChunkStream(['# Done']);
206+
await collectStream(await streamMarkdown(input));
207+
208+
expect(input.locked).toBe(false);
209+
});
210+
175211
// ── Large content ────────────────────────────────────────────────
176212

177213
it('handles many small chunks', async () => {

‎packages/content/md4x/src/lib/streaming-markdown-renderer.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,25 @@ export async function streamMarkdown(
2323
const { renderToHtml, heal } = await import('md4x/napi');
2424

2525
let buffer = '';
26+
let closed = false;
2627
const reader = input.getReader();
2728

29+
const release = (reason?: unknown) => {
30+
closed = true;
31+
return reader.cancel(reason).finally(() => reader.releaseLock());
32+
};
33+
2834
return new ReadableStream<string>({
2935
async pull(controller) {
3036
try {
3137
const { done, value } = await reader.read();
38+
if (closed) {
39+
return;
40+
}
3241

3342
if (done) {
43+
closed = true;
44+
reader.releaseLock();
3445
if (buffer) {
3546
const source = options?.heal ? heal(buffer) : buffer;
3647
controller.enqueue(renderToHtml(source));
@@ -44,10 +55,11 @@ export async function streamMarkdown(
4455
controller.enqueue(renderToHtml(source));
4556
} catch (error) {
4657
controller.error(error);
58+
await release(error).catch(() => undefined);
4759
}
4860
},
49-
cancel() {
50-
reader.cancel();
61+
cancel(reason) {
62+
return release(reason);
5163
},
5264
});
5365
}

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ catalog:
135135
marked-highlight: ^2.2.3
136136
marked-mangle: ^1.1.12
137137
marked-shiki: ^1.2.1
138-
md4x: ^0.0.25
138+
md4x: ^0.0.30
139139
mermaid: ^11.13.0
140140
minimist: ^1.2.8
141141
ng-packagr: 22.2.1
@@ -262,7 +262,7 @@ catalogs:
262262
vitest: ^4.0.7 || ^5.0.0
263263
zone.js: '>=0.15.0'
264264
peerContentOptional:
265-
md4x: '>=0.0.20'
265+
md4x: '>=0.0.28'
266266
satori: ^0.10.14
267267

268268
# Build-script allowlists belong in the root package.json under pnpm.* config.

0 commit comments

Comments
 (0)