Skip to content

Commit e74a555

Browse files
committed
Implement custom-id syntax on headings
1 parent add5007 commit e74a555

File tree

1 file changed

+10
-1
lines changed
  • plugins/gatsby-remark-header-custom-ids

1 file changed

+10
-1
lines changed

plugins/gatsby-remark-header-custom-ids/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ module.exports = (
2525
slugs.reset();
2626

2727
visit(markdownAST, 'heading', node => {
28-
const id = slugs.slug(toString(node), maintainCase);
28+
// Support custom-id syntax.
29+
const rawHeader = toString(node);
30+
const match = /^.+(\s*\{#([a-z0-9\-_]+?)\}\s*)$/.exec(rawHeader);
31+
const id = match ? match[2] : slugs.slug(rawHeader, maintainCase);
32+
if (match) {
33+
// Remove the custom ID part from the text node.
34+
const lastNode = node.children[node.children.length - 1];
35+
lastNode.value = lastNode.value.replace(match[1], '');
36+
}
37+
2938
const data = patch(node, 'data', {});
3039

3140
patch(data, 'id', id);

0 commit comments

Comments
 (0)