Detalhes do pacote

markdown-it-toc-and-anchor-with-slugid

yuki-takei198MIT1.1.4

markdown-it plugin to add toc and anchor links with slugified id in headings

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

readme (leia-me)

markdown-it-toc-and-anchor

Travis Build Status AppVeyor Build Status Version Coverage Status Dependency Status

markdown-it plugin to add toc and anchor links slugified id in headings

Installation

$ npm install markdown-it-toc-and-anchor-with-slugid

Usage

ES6

import markdownIt from "markdown-it"
import markdownItTocAndAnchor from "markdown-it-toc-and-anchor-with-slugid"
import uslug from 'uslug';

markdownIt({
    html: true,
    linkify: true,
    typography: true,
  })
    .use(markdownItTocAndAnchor, {
      // ...options

      // you can specify custom slugify function
      slugify: (header) => {
        return encodeURIComponent(uslug(header));
      }
    })
    .render(md)

ES5 / CommonJS

var markdownIt = require('markdown-it'),
    markdownItTocAndAnchor = require('markdown-it-toc-and-anchor-with-slugid').default;

markdownIt({
    html: true,
    linkify: true,
    typography: true,
  })
    .use(markdownItTocAndAnchor, {
      // ...options
    })
    .render(md)

:information_source: Note that the 'default' property has to be used when requiring this plugin, this is due to the use of Babel 6 now making ES6 compliant exports (Misunderstanding ES6 Modules, Upgrading Babel, Tears, and a Solution )

Options

toc

(default: true)

Allows you to enable/disable the toc transformation of @[toc]

tocClassName

(default: "markdownIt-TOC")

Option to customize html class of the <ul> wrapping the toc

tocFirstLevel

(default: 1)

Allows you to skip some heading level. Example: use 2 if you want to skip <h1> from the TOC.

tocLastLevel

(default: 6)

Allows you to skip some heading level. Example: use 5 if you want to skip <h6> from the TOC.

tocCallback

(default: null)

Allows you to get toc contents externally by executing a callback function returning toc elements, in addition / instead of using @[toc] tag in content. Example :

  markdownIt({
    html: true,
    linkify: true,
    typography: true,
  })
    .use(markdownItTocAndAnchor, {
      tocCallback: function(tocMarkdown, tocArray, tocHtml) {
        console.log(tocHtml)
      }
    })
    .render(md)

To allow callback to be more flexible, this option is also available in global markdown-it options, and in render environment. Example :

Callback in global markdown-it options
  var mdIt = markdownIt({
    html: true,
    linkify: true,
    typography: true,
  })
    .use(markdownItTocAndAnchor)

  ....

  mdIt.set({
    tocCallback: function(tocMarkdown, tocArray, tocHtml) {
      console.log(tocHtml)
    }
  })
    .render(md)
Callback in render environment
  var mdIt = markdownIt({
    html: true,
    linkify: true,
    typography: true,
  })
    .use(markdownItTocAndAnchor)

  ....

  mdIt
    .render(md, {
      tocCallback: function(tocMarkdown, tocArray, tocHtml) {
        console.log(tocHtml)
      }
    })

anchorLink

(default: true)

Allows you to enable/disable the anchor link in the headings

anchorLinkSymbol

(default: "#")

Allows you to customize the anchor link symbol

anchorLinkSpace

(default: true)

Allows you to enable/disable inserting a space between the anchor link and heading.

anchorLinkSymbolClassName

(default: null)

Allows you to customize the anchor link symbol class name. If not null, symbol will be rendered as <span class="anchorLinkSymbolClassName">anchorLinkSymbol</span>.

anchorLinkBefore

(default: true)

Allows you to prepend/append the anchor link in the headings

anchorClassName

(default: "markdownIt-Anchor")

Allows you to customize the anchor link class

resetIds

(default: true)

Allows you to reset (or not) ids incrementation. Use it if you will have multiple documents on the same page.

slugify

(default: jeremys/uslug)

Allows you to set the custom function to slygify ids.


CONTRIBUTING

  • ⇄ Pull requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests ($ npm test).

CHANGELOG

LICENSE

changelog (log de mudanças)

1.1.4 - 2018-06-06

  • Remove nested anchor

1.1.3 - 2018-05-12

  • Removed: uslug package
    • The default slugify function is now encodeURIComponent

1.1.2 - 2018-02-08

4.1.2 - 2017-03-07

  • Fixed: TypeError: Cannot read property 'breaks' of undefined (#30)

4.1.1 - 2016-05-05

  • Fixed: inexistent environment in markdown-it parsing on second instance

4.1.0 - 2016-04-24

  • Added: tocCallback option to allow getting toc elements in addition to / instead of @[toc] transformation

4.0.0 - 2016-04-23

  • Changed: refactored toc generation to allow full markdown processing
  • Removed: indentation option (the output is now generated by markdown-it and has no indentation)
  • Added: dependency to clone
  • Fixed: link inside heading not rendered correctly (#7)

3.1.0 - 2016-04-19

  • Added: tocLastLevel option to skip some upper heading levels (#1)

3.0.2 - 2016-04-19

  • Fixed: eslint command now using gitignore (fix for Windows), automatic syntax fix
  • Fixed: ava configuration (fix for Windows) (#14)

3.0.1 - 2016-04-10

  • Fixed: Reset anchor ids (#13)

3.0.0 - 2016-04-04

No API changes.

  • Changed: this plugin does not require markdown-it anymore (#2)

2.1.0 - 2016-01-17

  • Fixed: Should support unicode heading (#5 via #6)
  • Added: anchorLinkSpace option to enable/disable inserting a space between the anchor link and heading (#6).
  • Added: anchorLinkSymbolClassName customize the anchor link symbol class name (#6).

2.0.0 - 2015-11-01

  • Changed: markdown-it@^5.0.0.

1.0.1 - 2015-06-03

  • Fixed: publish dist/ files.

1.0.0 - 2015-06-03

✨ Initial release