Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/markedjs/marked-gfm-heading-id
Add unique heading ids like GitHub
https://github.com/markedjs/marked-gfm-heading-id
extension gfm heading id marked
Last synced: about 20 hours ago
JSON representation
Add unique heading ids like GitHub
- Host: GitHub
- URL: https://github.com/markedjs/marked-gfm-heading-id
- Owner: markedjs
- License: mit
- Created: 2021-05-17T03:02:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-20T17:46:25.000Z (11 days ago)
- Last Synced: 2025-01-22T20:08:14.278Z (9 days ago)
- Topics: extension, gfm, heading, id, marked
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/marked-gfm-heading-id
- Size: 4.96 MB
- Stars: 16
- Watchers: 4
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# marked-gfm-heading-id
Add ids to headings like GitHub.
# Usage
```js
import { marked } from "marked";
import { gfmHeadingId } from "marked-gfm-heading-id";// or UMD script
//
//const options = {
prefix: "my-prefix-",
};marked.use(gfmHeadingId(options));
marked("# heading");
//heading
```## Get heading list
`getHeadingList` is a function that is exported to provide the list of headings.
The headings will each be an object with the following properties:
- `text`: The rendered HTML for the heading
- `level`: The heading level (1-7)
- `raw`: The raw text (stripped of HTML rendering if any; this is usefull for situation like `marked("# [heading](./link)");`)
- `id`: The id given to the heading including any prefix```js
import { marked } from "marked";
import { gfmHeadingId, getHeadingList } from "marked-gfm-heading-id";marked.use(gfmHeadingId({prefix: "my-prefix-"}), {
hooks: {
postprocess(html) {
const headings = getHeadingList();return `
- ${raw} `)}
${headings.map(({id, raw, level}) => `
${html}`;
}
}
});
marked("# heading");
//
- heading
//
//
//
heading
```
## Clear Heading List
`resetHeadings` is a function to purge the stored list of headings and reset the Slugger. This is only needed when the globalSlugs option ( see below) is set to true and you wish to reset the slugger and exportable Headers list.
## `options`
| option | type | default | description |
|-------------|--------|---------|:----------------------------------------------|
| prefix | string | `""` | A string to prepend to all ids. |
| globalSlugs | bool | `false` | Track ids from one use of marked to the next. This ensures unique headers when parsing multiple markdown fragments and rendering the results as a single document. When set to false, the slugger and headers lists are cleared on every marked run.