GithubHelp home page GithubHelp logo

Comments (4)

valeriangalliat avatar valeriangalliat commented on July 28, 2024

markdown-it-anchor and markdown-it-toc were not developed to be compatible, so I'm not surprised they don't play well together by default.

  • markdown-it-toc overrides the heading renderer, so if you use it after markdown-it-anchor, it will effectively do nothing (markdown-it-anchor renderer never called).
  • markdown-it-anchor extends the heading renderer, calling the previous one after modifying the token chain. This is why, when called after, the second anchor is added. However markdown-it-toc ignores the attributes from the heading token when rendering, that's why markdown-it-anchor heading ID is never added.
  • Both plugins have a different way to manage IDs. markdown-it-anchor adds an ID on the heading, while markdown-it-toc adds an empty anchor with an ID.
  • Both plugins have a different way to slugify the title to generate an ID. markdown-it-anchor allow to customize this function with the slugify option.

According to this, you can have both plugins to work together by relying on a "bug" (the fact that markdown-it-toc ignores the heading token attributes): just pass the markdown-it-toc title transform function to markdown-it-anchor with the slugify option so the href links to the markdown-it-toc anchors.

But I'm afraid you'll have to copy/paste this function since markdown-it-toc don't expose it, and you'll run into bugs if the plugin ever updates this function while you have the old version.

Also, if markdown-it-toc updates its renderer to consider the token attributes (fixing the "bug" you'll rely on), you'll end up with duplicate IDs, one on the heading and one on the anchor, making your HTML invalid.


But if we look at the problem another way, you just want to add a # inside the empty anchor markdown-it-toc adds to each heading, and a href with the ID of the same anchor.

You can achieve this by writing your own plugin:

function permalink (md) {
  var originalHeadingOpen = md.renderer.rules.heading_open

  md.renderer.rules.heading_open = function (tokens, index) {
    var html = originalHeadingOpen.apply(this, arguments)

    // Transform the HTML to add a `#` inside the anchor and a `href`.

    return html
  }
}


var md = markdownIt(opts)
  .use(markdownItToc)
  .use(permalink)

from markdown-it-anchor.

MoOx avatar MoOx commented on July 28, 2024

I came to the same conclusions.

But I don't like to rely on a bug, so I am working on a new plugin "markdown-it-toc-and-anchor". I will let you know when it's ready ;)

from markdown-it-anchor.

MoOx avatar MoOx commented on July 28, 2024

https://github.com/MoOx/markdown-it-toc-and-anchor

from markdown-it-anchor.

valeriangalliat avatar valeriangalliat commented on July 28, 2024

Pretty nice!

from markdown-it-anchor.

Related Issues (20)

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.