├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {type Node} from 'unist'; 2 | 3 | export default function remarkCustomHeaderId(): (node: Node) => void; 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {visit, SKIP} from 'unist-util-visit'; 2 | 3 | // TODO: Use the same identifier for both when it's possible: https://github.com/tc39/proposal-duplicate-named-capturing-groups 4 | const idRegex = / {#(?[^}]+)}$| \|\|(?[^|]+)\|\|$/; 5 | 6 | export default function remarkCustomHeaderId() { 7 | return function (node) { 8 | visit(node, 'heading', node => { 9 | const textNode = node.children.at(-1); 10 | if (textNode?.type !== 'text') { 11 | return SKIP; 12 | } 13 | 14 | const text = textNode.value.trimEnd(); 15 | 16 | const matched = idRegex.exec(text); 17 | if (!matched) { 18 | return SKIP; 19 | } 20 | 21 | textNode.value = text.slice(0, matched.index); 22 | 23 | const {id1, id2} = matched.groups; 24 | const id = id1 ?? id2; 25 | node.data ??= {}; 26 | node.data.id = id; 27 | node.data.hProperties ??= {}; 28 | node.data.hProperties.id = id; 29 | }); 30 | }; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "remark-custom-header-id", 3 | "version": "1.0.0", 4 | "description": "Remark plugin for adding a custom ID attribute to Markdown headers", 5 | "license": "MIT", 6 | "repository": "sindresorhus/remark-custom-header-id", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsc index.d.ts" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "remark", 31 | "remark-plugin", 32 | "markdown", 33 | "header", 34 | "heading", 35 | "unified", 36 | "plugin", 37 | "html", 38 | "id", 39 | "attribute", 40 | "astro" 41 | ], 42 | "dependencies": { 43 | "@types/unist": "^3.0.2", 44 | "unist-util-visit": "^5.0.0" 45 | }, 46 | "devDependencies": { 47 | "ava": "^6.1.3", 48 | "rehype-stringify": "^10.0.0", 49 | "remark-parse": "^11.0.0", 50 | "remark-rehype": "^11.1.0", 51 | "typescript": "^5.4.5", 52 | "unified": "^11.0.4", 53 | "xo": "^0.58.0" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # remark-custom-header-id 2 | 3 | > [Remark](https://github.com/remarkjs/remark) plugin for adding a custom ID attribute to Markdown headers 4 | 5 | This allows for stable and more meaningful anchor links in your generated HTML. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install remark-custom-header-id 11 | ``` 12 | 13 | ## Usage 14 | 15 | ### Remark 16 | 17 | ```js 18 | import {remark} from 'remark'; 19 | import remarkRehype from 'remark-rehype'; 20 | import rehypeStringify from 'rehype-stringify'; 21 | import remarkCustomHeaderId from 'remark-custom-header-id'; 22 | 23 | const file = await remark() 24 | .use(remarkCustomHeaderId) 25 | .use(remarkRehype) // Markdown to HTML 26 | .use(rehypeStringify) // Stringify the HTML 27 | .process('# Foo {#custom-id}'); 28 | 29 | console.log(String(file)); 30 | //=> '

Foo

' 31 | ``` 32 | 33 | ### Astro 34 | 35 | ```js 36 | // astro.config.js 37 | import remarkCustomHeaderId from 'remark-custom-header-id'; 38 | 39 | export default defineConfig({ 40 | // … 41 | markdown: { 42 | remarkPlugins: [ 43 | remarkCustomHeaderId, 44 | ], 45 | }, 46 | // … 47 | }); 48 | ``` 49 | 50 | ### Alternative syntax for MDX files 51 | 52 | The `{#custom-id}` notation does not work in [MDX](https://mdxjs.com) files because MDX treats `{}` as JSX syntax, causing a parsing error. 53 | 54 | You can use one of these alternatives: 55 | 56 | - Escape curly braces: `\{#custom-id\}` 57 | - Use this syntax: `||custom-id||` 58 | 59 | Examples: 60 | 61 | - `## Some header \{#custom-id\}` 62 | - `## Some header ||custom-id||` 63 | 64 | ## API 65 | 66 | ### remarkCustomHeaderId() 67 | 68 | Returns a transformer function to be used with Remark. 69 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {unified} from 'unified'; 3 | import remarkParse from 'remark-parse'; 4 | import remarkRehype from 'remark-rehype'; 5 | import rehypeStringify from 'rehype-stringify'; 6 | import remarkCustomHeaderId from './index.js'; 7 | 8 | test('main', async t => { 9 | const file = await unified() 10 | .use(remarkParse) 11 | .use(remarkCustomHeaderId) 12 | .use(remarkRehype) 13 | .use(rehypeStringify) 14 | .process(` 15 | # unicorn {#foo-bar} 16 | # a {#aa} 17 | # b 18 | ## c {#foo bar} 19 | # unicorn ||foo-bar|| 20 | # a ||aa|| 21 | ## c ||foo bar|| 22 | # d {#wrong id|| 23 | # e ||wrong id} 24 | `.trim()); 25 | 26 | t.is(file.value, ` 27 |

unicorn

28 |

a

29 |

b

30 |

c

31 |

unicorn

32 |

a

33 |

c

34 |

d {#wrong id||

35 |

e ||wrong id}

36 | `.trim()); 37 | }); 38 | --------------------------------------------------------------------------------