# marked-gfm-heading-id

> marked GFM heading ids

Latest version **4.1.4** (published 2026-04-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install marked-gfm-heading-id
pnpm add marked-gfm-heading-id
yarn add marked-gfm-heading-id
bun add marked-gfm-heading-id
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.1.4 |
| Published | 2026-04-07 |
| First published | 2021-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 26 |
| Author | Tony Brix |
| Maintainers | tonybrix |
| Keywords | marked, extension |

## Links

- npm: https://www.npmjs.com/package/marked-gfm-heading-id
- Repository: https://github.com/markedjs/marked-gfm-heading-id
- Homepage: https://github.com/markedjs/marked-gfm-heading-id#readme
- Issues: https://github.com/markedjs/marked-gfm-heading-id/issues
- npm.io page: https://npm.io/package/marked-gfm-heading-id

## Dependencies (1)

- [github-slugger](https://npm.io/package/github-slugger.md) ^2.0.0

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 4.1.4 (latest) — 2026-04-07
- 4.1.3 — 2025-11-07
- 4.1.2 — 2025-06-27
- 4.1.1 — 2024-11-09
- 4.1.0 — 2024-08-23
- 4.0.1 — 2024-08-07
- 4.0.0 — 2024-06-12
- 3.2.0 — 2024-06-10
- 3.1.3 — 2024-02-06
- 3.1.2 — 2023-11-30
- 3.1.1 — 2023-11-11
- 3.1.0 — 2023-09-23
- 3.0.8 — 2023-09-11
- 3.0.7 — 2023-09-03
- 3.0.6 — 2023-08-07
- … 10 more at https://npm.io/package/marked-gfm-heading-id/versions

## 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
// <script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/marked-gfm-heading-id/lib/index.umd.js"></script>

const options = {
	prefix: "my-prefix-",
};

marked.use(gfmHeadingId(options));

marked("# heading");
// <h1 id="my-prefix-heading">heading</h1>
```

## 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 `
<ul id="table-of-contents">
	${headings.map(({id, raw, level}) => `<li><a href="#${id}" class="h${level}">${raw}</a></li>`)}
</ul>
${html}`;
		}
	}
});

marked("# heading");
// <ul id="table-of-contents">
//   <li><a href="#my-prefix-heading" class="h1">heading</a></li>
// </ul>
// <h1 id="my-prefix-heading">heading</h1>
```

## 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.

---
_Source: https://npm.io/package/marked-gfm-heading-id · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
