Skip to content

Commit

Permalink
Merge pull request #134 from valeriangalliat/slugify-with-state
Browse files Browse the repository at this point in the history
Introduce `slugifyWithState` to avoid breaking change with `slugify` second argument
  • Loading branch information
valeriangalliat authored Aug 24, 2024
2 parents 2e889a1 + 3f99b83 commit bfa8b79
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 15 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const md = require('markdown-it')()
.use(require('markdown-it-anchor'), { slugify: s => slugify(s) })
```

### Custimizing the slugify input

Additionally, if you want to further customize the title that gets
passed to the `slugify` function, you can do so by customizing the
`getTokensText` function, that gets the plain text from a list of
Expand All @@ -144,6 +146,18 @@ the previous approach with a stock markdown-it, but would also include
custom tokens added by any of your markdown-it plugins, which might or
might not be desirable for you. Now you have the option!

### Slugifying with state

If you need access to the markdown-it state from the slugify function,
e.g. to access `state.env`, you can use `slugifyWithState` instead.

```js
const md = require('markdown-it')()
.use(require('markdown-it-anchor'), {
slugifyWithState: (title, state) => `${state.env.id}-${slugify(title)}`
})
```

## Manually setting the `id` attribute

You might want to explicitly set the `id` attribute of your headings
Expand Down
2 changes: 1 addition & 1 deletion dist/markdownItAnchor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/markdownItAnchor.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit bfa8b79

Please sign in to comment.