Comments (4)
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.
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.
https://github.com/MoOx/markdown-it-toc-and-anchor
from markdown-it-anchor.
Pretty nice!
from markdown-it-anchor.
Related Issues (20)
- Add container around heading and anchor HOT 3
- Expose custom options regardless of permalink styles HOT 4
- Headings containing a hashtag yield invalid hrefs HOT 2
- Headers in html_block Tokens aren't given slugs HOT 11
- Add <span> inside permalink.headerLink style until safari reader more is fixed HOT 1
- incorrect type for permalink.headerLink() HOT 2
- Feature request: container option for linkAfterHeader HOT 5
- KaTeX Expressions aren't Included in Headings HOT 4
- Should return type for `PermalinkGenerator` be `void` instead of `string`? HOT 1
- Missing `wrapper` property in `LinkAfterHeaderPermalinkOptions` type HOT 1
- Add brief description of what plugin does to top of readme. HOT 1
- Types invalid with `"type": "module"` and `"moduleResolution": "Node16"` HOT 7
- @types in peerDeps HOT 2
- Suggestion: add `tabindex` to targets
- Curly braces anchor definitions HOT 2
- Improvement: render a nbsp instead of a space HOT 2
- Issue with linkAfterHeader
- Update typescript types HOT 1
- styleOfPermalinks is not a function HOT 2
- Advice for keeping permalink symbol token stable HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from markdown-it-anchor.