Skip to content

Commit 154cf2f

Browse files
authored
feat: enable to specify any id for headings (#3)
1 parent 59c1623 commit 154cf2f

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

apps/svelte.dev/src/routes/content.json/+server.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ async function content() {
4444
rank
4545
});
4646

47+
const headingRegex = /(.*?)(?:\s(<!--(.*?)-->))?$/;
48+
4749
for (const section of sections) {
4850
const lines = section.split('\n');
4951
const h2 = lines.shift();
@@ -52,13 +54,17 @@ async function content() {
5254
continue;
5355
}
5456

57+
const h2match = headingRegex.exec(h2);
58+
const h2text = h2match && h2match[1] || h2;
59+
const h2slug = h2match && h2match[3] || slugify(h2);
60+
5561
const content = lines.join('\n');
5662
const subsections = content.trim().split('## ');
5763
const intro = subsections?.shift()?.trim();
5864
if (intro) {
5965
blocks.push({
60-
breadcrumbs: [...breadcrumbs, clean(metadata.title), clean(h2)],
61-
href: get_href([slug, slugify(h2)]),
66+
breadcrumbs: [...breadcrumbs, clean(metadata.title), clean(h2text)],
67+
href: get_href([slug, slugify(h2slug)]),
6268
content: await plaintext(intro),
6369
rank
6470
});
@@ -72,9 +78,13 @@ async function content() {
7278
continue;
7379
}
7480

81+
const h3match = headingRegex.exec(h3);
82+
const h3text = h3match && h3match[1] || h3;
83+
const h3slug = h3match && h3match[3] || slugify(h3);
84+
7585
blocks.push({
76-
breadcrumbs: [...breadcrumbs, clean(metadata.title), clean(h2), clean(h3)],
77-
href: get_href([slug, slugify(h2) + '-' + slugify(h3)]),
86+
breadcrumbs: [...breadcrumbs, clean(metadata.title), clean(h2text), clean(h3text)],
87+
href: get_href([slug, slugify(h2slug) + '-' + slugify(h3slug)]),
7888
content: await plaintext(lines.join('\n').trim()),
7989
rank
8090
});

packages/site-kit/src/lib/markdown/renderer.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,14 @@ export async function render_content_markdown(
315315
},
316316
heading({ tokens, depth }) {
317317
const text = this.parser!.parseInline(tokens);
318-
const html = text.replace(/<\/?code>/g, '');
319318

320-
headings[depth - 1] = slugify(text);
319+
const match = text.match(/(.*?)(?:\s(<!--(.*?)-->))?$/);
320+
321+
const title = (match && match[2]) ? match[1] : text;
322+
323+
const html = title.replace(/<\/?code>/g, '');
324+
325+
headings[depth - 1] = (match && match[2]) ? match[3] : slugify(html);
321326
headings.length = depth;
322327
const slug = headings.filter(Boolean).join('-');
323328

packages/site-kit/src/lib/server/content/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ export async function create_index(
3131
'<code>$1</code>'
3232
);
3333

34-
const sections = Array.from(body.matchAll(/^##\s+(.*)$/gm)).map((match) => {
34+
const sections = Array.from(body.matchAll(/^##\s+(.*?)(?:\s(<!--(.*?)-->))?$/gm)).map((match) => {
3535
const title = smart_quotes(match[1])
3636
// replace < and > inside code spans
3737
.replace(/`(.+?)`/, (_, contents) => contents.replace(/</g, '&lt;').replace(/>/g, '&gt;'))
3838
// turn e.g. `class:_name_` into `class:<em>name</em>`
3939
.replace(/_(.+)_/g, (_, contents) => `<em>${contents}</em>`);
4040

41-
const slug = slugify(title);
41+
const slug = match[3] || slugify(title);
4242

4343
return { slug, title };
4444
});

0 commit comments

Comments
 (0)