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
14 changes: 13 additions & 1 deletion packages/repl-sdk/src/compilers/markdown/heading-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,20 @@ export function headingId(options = { defaults: false }) {
if (lastChild && lastChild.type === 'text') {
const string = lastChild.value.replace(/ +$/, '');
const matched = string.match(/ {#([^]+?)}$/);
const suffix = matched?.[0];
const customId = matched?.[1];

if (suffix && customId) {
const remaining = string.slice(0, -suffix.length);

if (remaining) {
lastChild.value = remaining;
} else {
node.children.pop();
}

setNodeId(node, customId);

if (matched) {
return;
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/repl-sdk/src/compilers/markdown/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ describe('heading ids', () => {

expect(result.text).toContain('id="a-s-b"');
});

it('uses a {#custom-id} suffix as the id and strips it from the text', async () => {
const result = await parseMarkdown(`## A very long heading {#short-id}`, { ...defaults });

expect(result.text).toBe('<h2 id="short-id">A very long heading</h2>');
});

it('supports {#custom-id} on a heading ending in formatting', async () => {
const result = await parseMarkdown(`## Use \`parseMarkdown\` here {#parse}`, {
...defaults,
});

expect(result.text).toBe('<h2 id="parse">Use <code>parseMarkdown</code> here</h2>');
});
});

describe('options', () => {
Expand Down
Loading