Code Monkey home page Code Monkey logo

markdown-it-toc-done-right's Introduction

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.

Build Status Coverage Status NPM version PRs Welcome Try markdown-it-toc-done-right on RunKit

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

$ npm i -S markdown-it-toc-done-right markdown-it-anchor

Usage

// node.js
var md = require("markdown-it")({
	html: false,
	xhtmlOut: true,
	typographer: true
}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§' } )
  .use( require("markdown-it-toc-done-right") );

var result = md.render("# markdown-it rulezz!\n\n${toc}\n## with markdown-it-toc-done-right rulezz even more!");

// browser without AMD, added to "window" on script load
// Note, there is no dash in "markdownit".
var md = window.markdownit({
	html: false,
	xhtmlOut: true,
	typographer: true
}).use( window.markdownItAnchor, { permalink: true, permalinkBefore: true, permalinkSymbol: '§' } )
  .use( window.markdownItTocDoneRight );

var 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:

md.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>:

function format(x, htmlencode) {
	return `<span>${htmlencode(x)}</span>`;
}

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:

$ npm i -S string
var string = require("string");

function legacySlugify(s) {
	return string(s).slugify().toString();
}

var md = require("markdown-it")({
	html: false,
	xhtmlOut: true,
	typographer: true
}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', slugify: legacySlugify } )
  .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:

$ npm i -S uslug
var uslug = require("uslug");

function uslugify(s) {
	return uslug(s);
}

var md = require("markdown-it")({
	html: false,
	xhtmlOut: true,
	typographer: true
}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', slugify: uslugify } )
  .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:

  1. (C1) Invalidates your HTML at Nu Html Checker. Using those plugins you will inject the "ERROR: No p element in scope but a p end tag seen." to your page.
  2. (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:

  1. (D1), the approach is very obscure: (i) cloning the argument md into md2, using it to render heading_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 to md2 parse and get the tokens back? Really? I feel it's a bit hard to maintain code like that.
  2. (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:

  1. Filter the heading tokens
  2. Use 'em to build an AST
  3. 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.

body { scroll-behavior: smooth; }

ol { counter-reset: list-item; }
li { display: block; counter-increment: list-item; }
li: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.

markdown-it-toc-done-right's People

Contributors

chmorgan avatar dependabot[bot] avatar dermolly avatar erikmichelson avatar korki43 avatar nagaozen avatar pascalgn avatar peschee avatar seahindeniz avatar uzay-g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

markdown-it-toc-done-right's Issues

Generate toc in separate div

Sorry if i have missed this but I am trying to make the TOC render in a div to the side of the markdown content div and not in the document itself.

I have the TOC showing using a placeholder in my markdown. it just i'd prefer to be able to specify the div that the TOC is shown in instead of being rendered as part of the document.

Is this possible? Like this

https://i.stack.imgur.com/ZDMmW.png

[Feature Requests] Optional Generation of the Nav Container Element

Hi,

Thank you for this amazing TOC generator! I was wondering if it would be possible to add a flag for optional generation of the <nav class="table-of-contents"> element.

I'm integrating markdown-it-toc-done-right into @superflycss/cli. Authors of MD documents may want to add additional classes to the nav element. For example:

<nav class="TableOfContents  u-ol-list-style-none u-background-color-white">
${toc}
</nav>

Thus this feature would give them design time / pre compile time control of the element.

Would also be really nice if we could control ol or ul through the name of the tag. For example:

${toc} // Default to ol
or
${tocul} renders the toc using ul

"Cherry on top" numbering KO when there are lists before the TOC

Hi,
it is my experience that the sample code given in the "Cherry on top" paragraph doesn't work correctly if there are lists in the page before the TOC (if you have say 3 lists before the TOC, numbering in the TOC starts at 3). I think this is related to the fact that "list-item" refers to the "The Implicit list-item Counter"

I changed the sample code to instantiating its own counter ("list-item-toc"), and got it to work:

.table-of-contents ol { counter-reset: list-item-toc; }
.table-of-contents li { display: block; counter-increment: list-item-toc; }
.table-of-contents li:before { content: counters(list-item-toc,'.') ' '; }

Thanks for markdown-it-toc-done-right, and happy new year - feliz ano novo

Doesn't work with hash routing

When using with vue-router hash routing, this link: http://localhost:5173/#/note/5 becomes this http://localhost:5173/#/addizione-e-sottrazione when clicking on a toc heading, causing the page to be empty.

Plugin conflict

Hello.

As I posted in the other issue I’m trying to fix the markdown-it-hierarchy plugin.

I switched approaches to modify the text content to add the hierarchy numbering at the core phrase but unexpectedly I’m seeing a conflict with this plugin.

I had expected the heading id attribute to be used but it looks like because I’m altering the content text this is resulting in the ahref values in the toc using the hierarchy adjusted text, breaking them.

If the id on the heading was “some-section” this is becoming “1.2-some-section” in the toc and is then mismatched with the href on the heading that is/was added to the heading by the anchor plugin.

I’d like my plugin not to conflict with this or any other plugin but I’m not sure what I should be doing differently. Should I not be updating the content during the core phase and do it in another phase? Inserting tags instead? Should this plugin be using the id attribute of the headers instead of recreating it from the text? (I assume this may be due to supporting the anchor plugin running either before or after this plugin, if before then there wouldn’t yet be id attributes)

TOC Creation does not respect heading level parameter

There does not appear to be a way to limit the TOC creation to a certain level of heading. For example, I don't want to create TOC entries for any H1s.

Given the peer dependency of markdown-it-anchor, I would expect that this package would respect the anchor level option, but it does not.

Expectation: The TOC package does not create entries for any headings without anchors (e.g. headings that were skipped by markdown-it-anchor)

Actual Behavior: The TOC package creates entries for all headings, even those without anchors. If the heading does not have an anchor, then the TOC package renders it as a non-functioning link.

Example:

const fs = require('fs');

const MarkdownIt = require('markdown-it');
const MdAnchor = require('markdown-it-anchor');
const MdToc = require('markdown-it-toc-done-right');

const mdString = `
# This is an H1 heading meant to be used for the {TITLE}

# Table of contents - This should not be an entry in the TOC
\${toc}

# BODY

## Section 1
foo bar

## Section 2
foobar
`;

const mdIt = new MarkdownIt({html: false, breaks: true, linkify: true,})
	.use(MdAnchor, {permalink: true, permalinkBefore: true, permalinkSymbol: '', level: 2})
	.use(MdToc);

const html = mdIt.render(mdString);
console.log(html);

Produces (indentation added)

<h1>This is an H1 heading meant to be used for the {TITLE}</h1>
<h1>Table of contents - This should not be an entry in the TOC</h1>
<nav class="table-of-contents">
   <ol>
      <li><a href="#this-is-an-h1-heading-meant-to-be-used-for-the-%7Btitle%7D">This is an H1 heading meant to be used
         for the {TITLE}</a></li>
      <li><a href="#table-of-contents---this-should-not-be-an-entry-in-the-toc">Table of contents - This should not be
         an entry in the TOC</a></li>
      <li><a href="#body">BODY</a>
         <ol>
            <li><a href="#section-1"> Section 1</a></li>
            <li><a href="#section-2"> Section 2</a></li>
         </ol>
      </li>
   </ol>
</nav><h1>BODY</h1>
<h2 id="section-1"><a class="header-anchor" href="#section-1" aria-hidden="true"></a> Section 1</h2>
<p>foo bar</p>
<h2 id="section-2"><a class="header-anchor" href="#section-2" aria-hidden="true"></a> Section 2</h2>
<p>foobar</p>

Ability to exclude certain headings from TOC

I am looking for a way to exclude certain headings from the TOC.

For example I want a TOC for this md:

# Header 1

# Header 2 {no_toc}

# Header 3

I would like the resulting TOC to only include the first and third header.
I am unsure if an attribute is the best way to go, as I don't know if it is even possible for the plugin to remove it from the markdown. So it wouldn't get rendered.

Another idea would be to add a list or a callback as an option. The list would contain the headings to ignore. The callback would be called on each heading and the result would decide if this heading should be ignored.

I am also a little bit unsure about subheadings:

# Header 1

## Subheader 1.1

## Subheader 1.2

# Header 2 {no_toc}

## Subheader 2.1

## Subheader 2.2

# Header 3

I think if the second header should be excluded all it's subheadings should be excluded too.

I already saw, there was a similar Issue #15. But that got closed without a real resolution.

I am open to contributing and adding the feature myself.

placeholder as pattern

I would like a table of contents creation for the following 4 cases:

[toc]
[TOC]
[[toc]]
[[TOC]]

Unfortunately the placeholder does not allow to specify a pattern, otherwise I could just use /^\[{1,2}toc\]{1,2}/im.

Any chance you could allow the use of a regex pattern?

How to compile ToC without md.render?

Hello,

First, thanks for the plugin. It works like a charm with markdown-it-anchor.
However, I got a small problem...

Is it possible to generate a ToC in a build script without having [[toc]] in my .md files?
If yes, how the function would look like?
(This function should return the html ToC snippet: <nav class="table-of-contents">...</nav>)

Why I'd like to do this?
Because of my html template, I'd like to paste html ToC in some outside CSS div.

Thanks a lot for your help.

Clicking TOC link with a non-alphanumeric character doesn't jump to correct position

Hello again, I was again asked in Joplin repo to post this problem here.

Joplin version used: Windows 1.0.169

Steps To Reproduce

  1. Paste/import the following test file to Joplin: testing.md.txt

  2. Click any of the TOC links.

= Only the first TOC link ("what") works, all other TOC links don't do anything.

I didn't test all non-alphanumeric characters, but testing few common ones seem to suggest that only alphanumeric characters in TOC links work.

Section numbers

FWIW: with this CSS, you get section numbers (for sections and subsections; subsubsections etc. can be supported analogously).

.table-of-contents {
  counter-reset: toc-section;
}
.table-of-contents > ul {
  list-style: none;
}

.table-of-contents > ul > li > a:before {
  counter-increment: toc-section;
  content: counter(toc-section) "\2002";
}
.table-of-contents > ul > li > a {
  counter-reset: toc-subsection;
}

.table-of-contents > ul > li > ul {
  list-style: none;
}
.table-of-contents > ul > li > ul > li > a:before {
  counter-increment: toc-subsection;
  content: counter(toc-section) "." counter(toc-subsection) "\2002";
}

Default:

1. Introduction
  1. About the topic
  2. Tips
2. First steps
  1. Getting started

With the CSS and {listType: 'ul'}:

1  Introduction
  1.1  About the topic
  2.1  Tips
2  First steps
  2.1  Getting started

Retain HTML in headings

Some of my headings have inline HTML to italicize certain words. These tags are stripped from the heading before the TOC renders. Is it possible to retain the inline tags?

Input: # Future Tense: *will*
Expected Output: <li><a href=#future-tense-will>Future Tense: <em>will</em></a></li>
Actual Output: <li><a href=#future-tense-will>Future Tense: will</a></li>

Support for markdown-it-attrs?

The plugin markdown-it-attrs lets you set custom IDs like this:

## Some heading {#custom-id}

Alas, if I use it together with markdown-it-toc-done-right, the custom IDs are only applied to the headings themselves, but not used in the TOC:

var md = require("markdown-it")({
    html: false,
    xhtmlOut: true,
    typographer: true
  })
  .use(require('markdown-it-attrs'))
  .use(require("markdown-it-anchor"), {
    permalink: true,
    permalinkBefore: true,
    permalinkSymbol: '§'
  })
  .use(require("markdown-it-toc-done-right"));

var result = md.render(`
# Title

[[toc]]

## First section {#one}

## Second section {#two}
`.trim());

console.log(result);

/* Output (with a few newlines inserted):
<h1 id="title"><a class="header-anchor" href="#title">§</a> Title</h1>

<nav class="table-of-contents"><ol>
<li><a href="#title"> Title</a><ol>
<li><a href="#first-section"> First section</a></li>
<li><a href="#second-section"> Second section</a></li></ol></li></ol></nav>

<h2 id="one"><a class="header-anchor" href="#one">§</a> First section</h2>
<h2 id="two"><a class="header-anchor" href="#two">§</a> Second section</h2>
*/

add the slugified URL to the AST

it would be convenient if the generated AST had a field to hold the slugified links. I know we can augment the AST using the callback function, but having it built-in ensures the same slugify method is used.

Example of current AST:

{
  "l": 2,
  "n": "My Title",
  "c": []
}

Example of proposed AST:

{
 "l": 2,
 "n": "My Title",
 "u": "#my-title",
 "c": []
}

I can make a PR if you’re interested!

Option to set class for top-level list

I'm looking for a way to set unique class for top-level list. In my own markup, I have different class at 2nd or deeper levels of ol/uls like below:

<nav class="containerClass">  
	<ul class="topListClass"> <!-- <-- An exception to linkClass option that I wish to set separately) -->
		<li class="itemClass">
			<a class="linkClass" href="#list-item">Top-Level List Item #1</a>
		</li>
		<li class="itemClass">
			<a class="linkClass" href="#list-item">Top-Level List Item #1</a>
			<ul class="submenuListClass"> <!-- <-- (i.e. I'd prefer to set this as default linkClass option for the rest of levels) -->
				<li>...</li>
				<li>...</li>
				<li ...>
					<a ...></a>
					<ul class="submenuListClass">...</ul> <!-- <-- -->
				</li>
				<li>...</li>
			</ul>
		</li>
		<li class="itemClass">
			<a class="linkClass" href="#list-item">Top-Level List Item #3</a>
		</li>
	</ul>
</nav>

Only H2s?

Is there a way to specify in the options that only certain header levels are included in the Table of Contents? For instance, if I want only H2 and H3 in the ToC, but not H4, H5, and H6?

The only thing that looks close is "level", which specifies the maximum level.

Perhaps as an enhancement, keep the existing functionality for level when a single value is passed in, but if an array is passed in, use just the levels in the array?

[Feature Requests] Callback Option

Adding this functionality can help us to place toc separately.

markdown.use(require("markdown-it-toc-done-right"), { tocCallback: toc => { //... } })

max depth of toc levels

We are using your module in Joplin and it recently came to my attention that there's a request to only show the first X levels of headers in the toc.

I did some research and noticed that other toc implementations provide an option to set the number of levels or max depth.

Is there any chance you could add such an option as well? From a coding standpoint, it should not be too complicated to make this happen.

Add an optional aria-label to the nav element for screen readers

When including a TOC on a page, it can happen that this is not the only nav container on a page, with the main navigation possibly being at least one other. it would be good if the plugin would include a default name for the nav element, set via aria-label, and add an option to override this. So the nav would be:

nav aria-label="Table of contents" ...
...
</nav>

or the user provided alternative if they set it.

I am using Markdown-It-TOC-Done-Right in a new Eleventy project and found it the best TOC solution, with only this little bit missing.

Thank you for considering accessibility as one of the reasons why Markdown-It-TOC-Done-Right is the better choice over others! ❤️

TypeError: plugin.apply is not a function with v4.0.0

Running into "TypeError: plugin.apply is not a function" with version 4.0.0.

r TypeError: plugin.apply is not a function
    at MarkdownIt.push../node_modules/markdown-it/lib/index.js.MarkdownIt.use (vendors.._src_components_pages_blog__year__month__day__post.._src_components_pages_index.js:13325)
    at _callee$ (._src_components_pages_blog__year__month__day__post.19e4fd7a250bcd634c5a.hot-update.js:145)
    at tryCatch (commons.app.js:6771)
    at Generator.invoke [as _invoke] (commons.app.js:6997)
    at Generator.prototype.<computed> [as next] (commons.app.js:6823)
    at asyncGeneratorStep (vendors.app.js:31)
    at _next (vendors.app.js:53)
    at vendors.app.js:60
    at new Promise (<anonymous>)
    at vendors.app.js:49

Reproducible using this repo.

Compatibility with markdown-it@^11.0.0

I'm getting this warning when using markdown-it@^11.0.0 in my package.json:

warning " > [email protected]" has incorrect peer dependency "markdown-it@^10.0.0".

Are there any issues with markdown-it@^11.0.0 or could you bump the peer dependency?

Disable numbering?

Is there a way to disable numbering? I’m working on fixing the markdown-it-hierarchy plugin and the approach that seems to make the most sense is to change that plugin to modify the header text during a core phase to add in the hierarchy text, like “1.1” or “2.”, prefixing it with the existing header text. But this means that there are duplicate numbers in the toc.

Am I missing the option to disable the numbering?

Is it possible to place TOC in a different place (from the content)?

I'm wondering how I might be able to go about adding the TOC in a different location from the content.
I'm using Vue/Nuxt for my website, and have a repeater for my content (some markdown, some other).

I'm hoping to be able to achieve the following:

<div v-html="$md.render('${toc}\n' + articleCombinedContent)" /> //without articleCombinedContent

<template v-for="(content, index) in article.content">
      <div :key="index" v-if="article.type === 'rich-text'" v-html="$md.render('content.content')" />
      <div :key="index" v-if="article.type === 'spacer'" :class="spacer" />
</template> -->

Is there a way to get JSON format of toc?

I want to get the JSON format of toc and render it in other place. Is there a way to do this?

Just like markdown-it-title does (inject title to env):

const md = require('markdown-it')({ typographer: true })

md.use(require('markdown-it-title'))

const env = {}
md.render('# Hello, *`world`!(c)*', env)
env.title === 'Hello, world!©'

access the state object in the callback

Would it be possible to add the state object, or the runtime env, as an argument of the callback function?

I'm using markdown-it-doc-done-right in a context (building an Eleventy site)) where I don't control the call to the markdown-it renderer, but where I'd like to augment the passed environment with the toc AST.

I'd like to use this as a callback:

(html, ast, state) => {
  if (state.env && !state.env.toc) {
    state.env.toc = ast;
  }
}

I can submit a PR if you’re interested!

TypeError: Cannot set property 'Your Heading Text' of undefined

Input:

${toc}


## Foo

##### Bar

abc

## BOOM

Output:

TypeError: Cannot set property 'BOOM' of undefined
    at headings_ast (/home/ilyaigpetrov/Repos/sl-sites/node_modules/markdown-it-toc-done-right/index.js:117:31)
    at Object.md.renderer.rules.toc_body (/home/ilyaigpetrov/Repos/sl-sites/node_modules/markdown-it-toc-done-right/index.js:72:20)
    at Renderer.render (/home/ilyaigpetrov/Repos/sl-sites/node_modules/markdown-it/lib/renderer.js:326:38)
    at MarkdownIt.render (/home/ilyaigpetrov/Repos/sl-sites/node_modules/markdown-it/lib/index.js:543:24)
    at /home/ilyaigpetrov/Repos/sl-sites/node_modules/metalsmith-markdownit/lib/index.js:75:28
    at Array.forEach (<anonymous>)
    at /home/ilyaigpetrov/Repos/sl-sites/node_modules/metalsmith-markdownit/lib/index.js:71:28
    at Array.forEach (<anonymous>)
    at Ware.plugin (/home/ilyaigpetrov/Repos/sl-sites/node_modules/metalsmith-markdownit/lib/index.js:59:72)
    at Ware.<anonymous> (/home/ilyaigpetrov/Repos/sl-sites/node_modules/wrap-fn/index.js:45:19)

Details

markdownit({
    html: true,
    linkify: true,
    typographer: true,
  })
  .use(require('markdown-it-anchor'), {
    slugify,
  })
  .use(require('markdown-it-toc-done-right'), {
    slugify,
  });

Well done

Just popping in to say "well done" on this plugin. Best one I've found by far.

[BUG] headings with the same text are not correctly handled

I know that it is an uncommon event, but I noticed that if you enter two or more headings of the same level with the same title one after another, only the first one will appear in the TOC.

# Title
text
## Section
example
### subSection
text
### subSection
text
# else

In that case only the first h3 heading will appear.

Feature Request: Option to link back to TOC from every H2

What do you think adding support for a link back to the table of contents from items listed in the TOC, in order to facilitate navigation to and from. I'd be happy to draft a PR if this is something you'd be interested in. Thanks!

behaviour of render a title with link

when rendering a title with anchor, such as ## title with [anchor](github.com), the result in TOC is title with [anchor](github.com). Maybe we can add a option that determine whethere output []()?

How to get this working on github markdown files?

I was searching for this kind of thing so that I could automatically generate a TOC for github markdown files in general, but it does not seem that it is supported right now. The only thing that shows up is ${toc}, no actual table of contents.

Is there anything that I need to do to enable this on Github or is this not supported?

TOC Automatically sorts some Headers in Rendered view

Hello, I was asked in Joplin repo to post this problem here.

Joplin version used: Windows 1.0.165

Steps To Reproduce

  1. Download and import/paste this md test file to Joplin: test.md.txt
  2. Change the layout to side-by-side raw & rendered view.

= Rendered view shows these TOC entries/links in incorrect order compared to the raw/md text:

  • Headers with numbers 0-4 and 6-9 are put on top of the TOC, although those headers are located after header "z". (Header "5 test" is not affected, it's in correct place in the TOC.)
  • Header with number 0 is put as the first TOC entry, although it's actually after header 9.

Here's a screenshot from Joplin that shows the beginning of the TOC in side-by-side raw/md & rendered view:

joplin

Expected behavior: All TOC entries/links in rendered view should be in the order that they are in the raw/md text, i.e. no sorting.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.