Détail du package

@gerhobbelt/markdown-it-anchor

GerHobbelt36UNLICENSE7.0.2-27

Header anchors for markdown-it.

markdown, markdown-it, markdown-it-plugin, markdown-it

readme

markdown-it-anchor

Build Status npm version

Header anchors for markdown-it.

English | 中文 (v7.0.1)

Usage

const md = require('markdown-it')()
  .use(require('markdown-it-anchor'), opts)

See a demo as JSFiddle.

The opts object can contain:

Name Description Default
level Maximum level to apply anchors on or array of selected levels. 6
slugify A custom slugification function. See index.js
uniqueSlugStartIndex Index to start with when making duplicate slugs unique. 1
permalink Whether to add permalinks next to titles. false
renderPermalink A custom permalink rendering function. See index.js
permalinkClass The class of the permalink anchor. header-anchor
permalinkSpace Place space between the header text and the permalink anchor. true
permalinkSymbol The symbol in the permalink anchor.
permalinkBefore Place the permalink before the title. false
permalinkHref A custom permalink href rendering function. See index.js
permalinkAttrs A custom permalink attributes rendering function. See index.js
callback Called with token and info after rendering. undefined

The renderPermalink function takes the slug, an options object with the above options, and then all the usual markdown-it rendering arguments.

All headers at or below level will then have an id attribute with a slug of their content. level can also be an array of headers levels to apply the anchor, like [2, 3] to have an anchor on only level 2 and 3 headers.

If permalink is true, a symbol linking to the header itself will be added.

You may want to use the link symbol as permalinkSymbol, or a symbol from your favorite web font.

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.

User-Friendly URLs

Starting from v5.0.0, markdown-it-anchor dropped package string keeping it's core value of being an unopinionated and secure library. Yet, users looking for backward compatibility may want the old slugify:

$ npm i -S string
const string = require('string')
const legacySlugify = s => string(s).slugify().toString()

const md = require('markdown-it')()
const anchor = require('markdown-it-anchor', {
    slugify: legacySlugify
})

Unicode Support

Unicode is supported by default. Yet, if you are looking for a "prettier" --opinionated-- link, i.e without %xx, you may want to take a look at @gerhobbelt/slug:

$ npm i -S @gerhobbelt/slug
const slug = require('@gerhobbelt/slug')
const uslugify = s => slug(s, { mode: 'rfc3986' })

const md = require('markdown-it')()
const anchor = require('markdown-it-anchor', {
    slugify: uslugify
})

Table of Contents

Looking for an automatic table of contents (TOC) generator? Take a look at markdown-it-toc-done-right: it's made from the ground up to be a great companion of this plugin.

Browser Example

See example.html.

License

UNLICENSE

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

7.0.2 - 2021-02-06

  • Optimize token parsing (#80)

7.0.1 - 2021-01-28

  • Add a Chinese readme (#79)

7.0.0 - 2021-01-04

  • Depend on any markdown-it version (#76)

6.0.1 - 2020-11-19

  • Added example.html test case
  • Added uniqueSlugStartIndex test case
  • Fix equal -> strictEqual
  • Updated dependencies -> found 0 vulnerabilities

6.0.0 - 2020-09-29

  • Allow to configure unique slug start index, and make it 1 instead of 2 to mimic what markdown-toc, github-slugger, and GitHub itself does by default. This should improve out of the box compatibility with other packages. (#74)

5.3.0 - 2020-05-12

  • Fix support for user defined ids by using markdown-it-attrs.
  • Updated dependencies -> found 0 vulnerabilities

5.2.7 - 2020-04-01

  • Added permalinkAttrs option

5.2.6 - ???

  • ???

5.2.5 - 2019-10-16

  • removing aria-hidden from links (#58)

5.2.4 - 2019-06-03

  • rolled back to ...linkTokens
  • executed npm audit fix to fix dependencies vunerabilities

5.2.3 - 2019-05-28

  • ...linkTokens -> (...).apply(null, linkTokens) IE doesn't support spread syntax.

5.2.2 - 2019-05-28

  • ...linkTokens -> [].concat(linkTokens) makes IE compatible.

5.2.1 - 2019-05-28

  • fix typo

5.2.0 - 2019-05-28

  • Added support for unpkg
  • Added support for mjs
  • Fixes: #40 and [#46]
  • New option permalinkSpace makes possible to suppress the whitespace between the permalink and the header text value. Defaults to true ([#52])
  • Patch to fix (#35)

5.0.1 - 2018-06-14

  • trim() before toLowerCase() to prevent dashes as prefixes and suffixes.

5.0.0 - 2018-06-14

  • NEW Contributor: nagaozen
  • Drop string package in favour of encodeURIComponent.
    Fixes: #44, #43, #38, #17. Obsoletes #45.

4.0.0 - 2017-02-26

  • Drop Babel. This drops support for Node.js versions that doesn't support ES6.
  • Support code in titles. (#27)
  • Support individual header level selection. (#27)

3.0.0 - 2017-02-06

  • Use existing ID as slug if present. This drops the support for markdown-it 5 and lower, hence the major bump. (#22)

2.5.1 - 2016-11-19

  • Patch for supporting "Constructor" title. (#18)

2.5.0 - 2016-03-22

  • Test against markdown-it 6.
  • Support anchors with HTML in header.

2.4.0 - 2016-02-12

  • Add a callback option. (#16).

2.3.3 - 2015-12-21

  • Add a live example (#13).

2.3.2 - 2015-11-29

  • Test against markdown-it 5.
  • Keep assigning module.exports after Babel 6 upgrade (that assigns exports.default only instead), using babel-plugin-add-module-exports. (#12)

2.3.1 - 2015-11-29

  • Remove hard dependency on markdown-it and replace lodash.assign with Object.assign. (#11)
  • Move to Babel 6.
  • Use babel-plugin-transform-object-assign to have Object.assign work in ES5 environments.
  • Add the permalink during compilation instead of rendering.

2.3.0 - 2015-08-13

  • Allow to pass HTML as permalink symbol. (#8)

2.2.1 - 2015-08-13

  • Do not crash when permalink is enabled and headers below specified level are present. (#7)

2.2.0 - 2015-07-20

  • Use core.ruler to add attributes so other plugins can reuse them. (#5)

2.1.0 - 2015-06-22

  • Set aria-hidden on permalink anchor.

2.0.0 - 2015-05-28

  • Place the permalink after the header by default. (#3)

    If you want to keep the old behavior, set the permalinkBefore option to true:

    const md = require('markdown-it')
      .use(require('markdown-it-anchor'), {
        permalink: true,
        permalinkBefore: true
      })
    

1.1.2 - 2015-05-23

  • Fix a code example in the readme.

1.1.1 - 2015-05-20

  • Slight tweaks in package.json.
  • Upgrade Babel.

1.1.0 - 2015-04-24

  • Allow to customize the permalink symbol. (#1)
  • Handle duplicate slugs by appending a number.

1.0.0 - 2015-03-18

  • Initial release.