Skip to content

Commit

Permalink
docs: add getHeadingList to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Sep 23, 2023
1 parent a72b5de commit f044fe1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ 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)
- `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, text, level}) => `<li><a href="#${id}" class="h${level}">${text}</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>
```

## `options`

| option | type | default | description |
Expand Down

0 comments on commit f044fe1

Please sign in to comment.