Skip to content

Commit a49f7c9

Browse files
JeanMechethePunderWoman
authored andcommitted
docs(docs-infra): extract captured group from heading custom id regexp (angular#57206)
Backport of angular/dev-infra#2202 PR Close angular#57206
1 parent 6ae7d5e commit a49f7c9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

adev/shared-docs/pipeline/guides/testing/heading/heading.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('markdown to html', () => {
8080
const h2HeaderId = h2?.getAttribute('id');
8181
const h2AnchorHref = h2Anchor?.getAttribute('href');
8282

83-
expect(h2HeaderId).toContain('my-custom-id');
83+
expect(h2HeaderId).toBe('my-custom-id');
8484
expect(h2AnchorHref).toBe(`#${h2HeaderId}`);
8585
});
8686
});

adev/shared-docs/pipeline/guides/tranformations/heading.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ export const headingRender: RendererApi['heading'] = (text, level, raw) => {
2828
const regex = /<a\s+(?:[^>]*?\s+)?href.*?>(.*?)<\/a>/gi;
2929
const anchorLessText = text.replace(regex, '$1');
3030

31-
const link = getHeaderId(anchorLessText);
32-
const label = anchorLessText.replace(/`(.*?)`/g, '<code>$1</code>');
31+
// extract the extended markdown heading id
32+
// ex: ## MyHeading {# myId}
33+
const customIdRegex = /{#\s*([\w-]+)\s*}/g;
34+
const customId = customIdRegex.exec(anchorLessText)?.[1];
35+
const link = customId ?? getHeaderId(anchorLessText);
36+
const label = anchorLessText
37+
.replaceAll(/`(.*?)`/g, '<code>$1</code>')
38+
.replaceAll(customIdRegex, '');
3339

3440
return `
3541
<h${level} id="${link}">

0 commit comments

Comments
 (0)