Installations
npm install markdown-it-toc-done-right
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
12.18.3
NPM Version
6.14.8
Releases
Unable to fetch releases
Contributors
Languages
JavaScript (50.31%)
HTML (47.72%)
Makefile (1.97%)
Developer
nagaozen
Download Statistics
Total Downloads
2,490,707
Last Day
2,812
Last Week
13,085
Last Month
57,969
Last Year
725,360
GitHub Statistics
135 Stars
77 Commits
25 Forks
3 Watching
20 Branches
9 Contributors
Bundle Size
2.96 kB
Minified
1.38 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.2.0
Package Id
markdown-it-toc-done-right@4.2.0
Size
14.75 kB
NPM Version
6.14.8
Node Version
12.18.3
Publised On
19 Nov 2020
Total Downloads
Cumulative downloads
Total Downloads
2,490,707
Last day
-1.2%
2,812
Compared to previous day
Last week
-13.4%
13,085
Compared to previous week
Last month
1.1%
57,969
Compared to previous month
Last year
-5.3%
725,360
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
markdown-it-toc-done-right
A table of contents (TOC) plugin for Markdown-it with focus on semantic and security. Made to work gracefully with markdown-it-anchor.
tl;dr
If you are drowning in table of contents plugins options, just pick this one. It delivers an accessible, semantic, SEO friendly and safe TOC. Place one of: ${toc}
, [[toc]]
, [toc]
, [[_toc_]]
in your markdown and, BOOM, the <nav class="table-of-contents">
will be there. Click here for additional information.
Installation
1$ npm i -S markdown-it-toc-done-right markdown-it-anchor
Usage
1// node.js 2var md = require("markdown-it")({ 3 html: false, 4 xhtmlOut: true, 5 typographer: true 6}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§' } ) 7 .use( require("markdown-it-toc-done-right") ); 8 9var result = md.render("# markdown-it rulezz!\n\n${toc}\n## with markdown-it-toc-done-right rulezz even more!"); 10 11// browser without AMD, added to "window" on script load 12// Note, there is no dash in "markdownit". 13var md = window.markdownit({ 14 html: false, 15 xhtmlOut: true, 16 typographer: true 17}).use( window.markdownitAnchor, { permalink: true, permalinkBefore: true, permalinkSymbol: '§' } ) 18 .use( window.markdownitTocDoneRight ); 19 20var result = md.render("# markdown-it rulezz!\n\n${toc}\n## with markdown-it-toc-done-right rulezz even more!");
Options
You may specify options when using the plugin:
1md.use(require("markdown-it-toc-done-right"), options);
These options are available:
Name | Description | Default |
---|---|---|
placeholder | The pattern serving as the TOC placeholder in your markdown | "(\$\{toc\} |
slugify | A custom slugification function | See index.js |
uniqueSlugStartIndex | Index to start with when making duplicate slugs unique. | 1 |
containerClass | The class for the container DIV | "table-of-contents" |
containerId | The ID for the container DIV | undefined |
listClass | The class for the listType HTMLElement | undefined |
itemClass | The class for the LI | undefined |
linkClass | The class for the A | undefined |
level | Minimum level to apply anchors on or array of selected levels | 1 |
listType | Type of list (ul for unordered, ol for ordered) | ol |
format | A function for formatting headings (see below) | undefined |
callback | A function (html, ast) {} called after rendering. | undefined |
format
is an optional function for changing how the headings are displayed in the TOC e.g. Wrapping content in <span>
:
1function format(x, htmlencode) { 2 return `<span>${htmlencode(x)}</span>`; 3}
User-Friendly URLs
Starting from v2.0.0
, markdown-it-toc-done-right
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:
1$ npm i -S string
1var string = require("string"); 2 3function legacySlugify(s) { 4 return string(s).slugify().toString(); 5} 6 7var md = require("markdown-it")({ 8 html: false, 9 xhtmlOut: true, 10 typographer: true 11}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', slugify: legacySlugify } ) 12 .use( require("markdown-it-toc-done-right"), { 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 uslug
:
1$ npm i -S uslug
1var uslug = require("uslug"); 2 3function uslugify(s) { 4 return uslug(s); 5} 6 7var md = require("markdown-it")({ 8 html: false, 9 xhtmlOut: true, 10 typographer: true 11}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', slugify: uslugify } ) 12 .use( require("markdown-it-toc-done-right"), { slugify: uslugify } );
Really? Another markdown-it table of contents plugin?
Unfortunately, I'm a little rigorous developer and most of the existing plugins (exempli gratia markdown-it-toc-and-anchor
, markdown-it-table-of-contents
, markdown-it-toc
, markdown-it-toc-x3
et cetera) fail under, at least, one of these criteria: correctness, semantic, security.
Let me first define those:
- correctness
(C1)
, means the that the algorithm is correct with respect to a specification. - semantic
(C2)
, means the that the algorithm delivers meaningful output (id est more than presentation driven HTML). - security
(C3)
, means the that the algorithm protects you, by default, against malicious users.
Whenever I need to work with markdown syntax, I leverage Markdown-it. It's GREAT, the developers care about security, it's built for flexibility, have a huge community extending it and outputs a semantic perfect (X)HTML. Somehow, not all plugin developers keep the same high standards for their extensions. Take for example the three most popular, at the time of writing this README, solutions for this problem: markdown-it-toc-and-anchor
, markdown-it-table-of-contents
, markdown-it-toc
. They all implement their TOC as inline
and, most obscure of all, after the emphasis
rule. Does it make any sense? Could someone explain why it's like that? Two errors get derived from this single choice:
(C1)
Invalidates your HTML at Nu Html Checker. Using those plugins you will inject the "ERROR: Nop
element in scope but ap
end tag seen." to your page.(C2)
Throw HTML5 semantics away. One of the reasons for the tag<nav>
to exists is exactly to be used as a container for table of contents. Semantic is great for SEO and Accessibility. If you, like me, care about SEO and Accessibility you can't use those plugins.
Of course that neither of the above arguments degrades your presentation, but as a coach that trains people to be A-grade web developers to work with very sensitive data (i.e. money and credit card) it doesn't make sense for me to keep any one of these plugins. Another very common (C1)
mistake that can be found in the wild is using an optional regular expression to match the placeholder in the source, but using a hard-coded charCodeAt
value to reject a token. Finally, (C3)
is also a concern since the prevalence of unescaped HTML is currently very high; opening your page to XSS and other HTML injection security vulnerabilities.
The only other plugin that addresses (C2)
and (C3)
found is markdown-it-toc-x3
. It's made because the author also noted (C2)
and addresses (C3)
by always using markdown-it token.attrSet(k,v)
, but I've technical (D1)
and philosophical (D2)
disagreements with the code:
(D1)
, the approach is very obscure:(i)
cloning the argumentmd
intomd2
, using it to renderheading_open next token
into something that needs to be slugified, stripped and will generate more tokens to be concatenated with token.children;(ii)
hard-coding<svg>
into it?;(iii)
building a string representation of the TOC tomd2
parse and get the tokens back? Really? I feel it's a bit hard to maintain code like that.(D2)
composability, do one thing and do it well. If a plugin could be broken in two plugins, it should be broken.+1
for maintenance.
And those are the "why"s of markdown-it-toc-done-right
. It protects you by HTML enconding critical points (C3)
✓, outputs a nice semantic tag nav
which works the same as WAI-ARIA landmark role="navigation"
saving you from this kind of issue (C2)
✓ and get the job done with an über simple approach (C1)
✓.
How it works?
The idea behind the plugin is incredibly simple:
- Filter the heading tokens
- Use 'em to build an AST
- Apply the HTML semantics in the AST.
Very straightforward, it's one of the smallest plugins you will find around. +1
for maintenance.
Cherry on top
Really? You reached here? You deserve a dessert! Try applying this CSS to your markdown-it-toc-done-right
page.
1body { scroll-behavior: smooth; } 2 3ol { counter-reset: list-item; } 4li { display: block; counter-increment: list-item; } 5li:before { content: counters(list-item,'.') ' '; }
Want to know more about scroll-behavior? Visit https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior.
Want to know more about nested counters? Visit https://www.w3.org/TR/css-lists-3/.
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
).
Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 7/10 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 27 are checked with a SAST tool
Reason
41 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-257v-vj4p-3w2h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr
- Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-6vfc-qv3f-vr6c
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
2.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to markdown-it-toc-done-right
@gerhobbelt/markdown-it-toc-done-right
Table of contents (TOC) for markdown-it markdown parser with focus on semantic and security.
markdown-it-toc-done-right-extended
Table of contents (TOC) for markdown-it markdown parser with focus on semantic and security.
@liradb2000/markdown-it-toc-done-right
Forked Package 'markdown-it-toc-done-right'