Specify a custom heading id in headings with the Markdown Extended Syntax # heading {#custom-id}
The heading will be rendered with the string between {# and }
The id must be a valid id with the following criteria:
- Must start with a letter (
A-Z,a-z) - Must only contain word characters (
A-Z,a-z,0-9,_) or dashes (-)
import { marked } from "marked";
import customHeadingId from "marked-custom-heading-id";
// or UMD script
// <script src="/asset?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fmarked%2Flib%2Fmarked.umd.js"></script>
// <script src="/asset?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fmarked-custom-heading-id%2Flib%2Findex.umd.js"></script>
marked.use(customHeadingId());
marked("# heading {#custom-id}");
// <h1 id="custom-id">heading</h1>