remark-autolink-headings
remark plugin to automatically add links to headings.
This package integrates with
remark-html
. It may be better to work with rehype, which is specifically made for HTML, and to userehype-autolink-headings
instead of this package.
Install
npm:
npm install remark-autolink-headings
Use
Say we have the following markdown file, example.md
:
# Lorem ipsum 😪## dolor—sit—amet### consectetur & adipisicing#### elit##### elit
And our script, example.js
, looks as follows:
const fs = const unified = const markdown = const html = const slug = const headings = const doc = // Note that this module must be included after `remark-slug`. console
Now, running node example
yields:
Lorem ipsum 😪dolor—sit—ametconsectetur & adipisicingelitelit
API
remark().use(autolinkHeadings[, options])
Automatically add links to headings.
options
options.behavior
How to create links (string
, default: 'prepend'
).
'prepend'
— inject link before the heading text'append'
— inject link after the heading text'wrap'
— wrap the whole heading text with the link'before'
— insert link before the heading'after'
— insert link after the heading
Supplying wrap
will ignore any value defined by the content
option.
Supplying prepend
, append
, or wrap
will ignore the group
option.
options.linkProperties
Extra properties to set on the link (Object?
).
Defaults to {ariaHidden: true, tabIndex: -1}
when in 'prepend'
or 'append'
mode.
options.content
hast nodes to insert in the link (Function|Node|Children
).
By default, the following is used:
type: 'element' tagName: 'span' properties: className: 'icon' 'icon-link'
If behavior
is wrap
, then content
is ignored.
If content
is a function, it’s called with the current heading (Node
) and
should return one or more nodes:
const toString = const h = // … { return }
options.group
hast node to wrap the heading and link with (Function|Node
), if
behavior
is before
or after
.
There is no default.
If behavior
is prepend
, append
, or wrap
, then group
is ignored.
If group
is a function, it’s called with the current heading (Node
) and
should return a hast node.
const h = // … { return }
Security
Use of remark-autolink-headings
can open you up to a
cross-site scripting (XSS) attack if you pass user provided content in
linkProperties
or content
.
Always be wary of user input and use rehype-sanitize
.
Contribute
See contributing.md
in remarkjs/.github
for ways
to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.