From e74a555db72ce3400225fbcbfc7159767730cef8 Mon Sep 17 00:00:00 2001 From: Soichiro Miki Date: Thu, 7 Feb 2019 00:27:47 +0900 Subject: [PATCH] Implement custom-id syntax on headings --- plugins/gatsby-remark-header-custom-ids/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/gatsby-remark-header-custom-ids/index.js b/plugins/gatsby-remark-header-custom-ids/index.js index b883608d..df645707 100644 --- a/plugins/gatsby-remark-header-custom-ids/index.js +++ b/plugins/gatsby-remark-header-custom-ids/index.js @@ -25,7 +25,16 @@ module.exports = ( slugs.reset(); visit(markdownAST, 'heading', node => { - const id = slugs.slug(toString(node), maintainCase); + // Support custom-id syntax. + const rawHeader = toString(node); + const match = /^.+(\s*\{#([a-z0-9\-_]+?)\}\s*)$/.exec(rawHeader); + const id = match ? match[2] : slugs.slug(rawHeader, maintainCase); + if (match) { + // Remove the custom ID part from the text node. + const lastNode = node.children[node.children.length - 1]; + lastNode.value = lastNode.value.replace(match[1], ''); + } + const data = patch(node, 'data', {}); patch(data, 'id', id);