Value of the tabindex attribute on headings, set to false to disable.
-1
uniqueSlugStartIndex
Index to start with when making duplicate slugs unique.
1
All headers greater than the minimum level will have an id attribute
with a slug of their content. For example, you can set level to 2 to
add anchors to all headers but h1. You can also pass an array of
header levels to apply the anchor, like [2, 3] to have an anchor on
only level 2 and 3 headers.
If a permalink renderer is given, it will be called for each matching header
to add a permalink. See permalinks below.
If a slugify function is given, you can decide how to transform a
heading text to a URL slug. See user-friendly URLs.
The callback option is a function that will be called at the end of
rendering with the token and an info object. The info object has
title and slug properties with the token content and the slug used
for the identifier.
We set by default tabindex="-1"
on headers. This marks the headers as focusable elements that are not
reachable by keyboard navigation. The effect is that screen readers will
read the title content when it's being jumped to. Outside of screen
readers, the experience is the same as not setting that attribute. You
can override this behavior with the tabIndex option. Set it to false
to remove the attribute altogether, otherwise the value will be used as
attribute value.
Finally, you can customize how the title text is extracted from the
markdown-it tokens (to later generate the slug). See user-friendly URLs.
User-friendly URLs
Starting from v5.0.0, markdown-it-anchor dropped the string
package to retain our core value of being an impartial and secure
library. Nevertheless, users looking for backward compatibility may want
the old slugify function:
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
markdown-it inline tokens:
By default we include only text and code_inline tokens, which
appeared to be a sensible approach for the vast majority of use cases.
An alternative approach is to include every token's content except for
html_inline and image tokens, which yields the exact same results as
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!
Manually setting the id attribute
You might want to explicitly set the id attribute of your headings
from the Markdown document, for example to keep them consistent across
translations.
markdown-it-anchor is designed to reuse any existing id, making markdown-it-attrs
a perfect fit for this use case. Make sure to load it before markdown-it-anchor!
Then you can do something like this:
#Your title {#your-custom-id}
The anchor link will reuse the id that you explicitly defined.
Compatible table of contents plugin
Looking for an automatic table of contents (TOC) generator? Take a look at
markdown-it-toc-done-right
it's made from the ground to be a great companion of this plugin.
Parsing headings from HTML blocks
markdown-it-anchor doesn't parse HTML blocks, so headings defined in
HTML blocks will be ignored. If you need to add anchors to both HTML
headings and Markdown headings, the easiest way would be to do it on the
final HTML rather than during the Markdown parsing phase:
请发表评论