Skip to content

Commit

Permalink
feat: add options.appendIdToHeading, set it to false to prevent id …
Browse files Browse the repository at this point in the history
…attribute appended
  • Loading branch information
hikerpig committed Jul 27, 2019
1 parent 20c4985 commit ec45059
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ documents on the same page.

Allows you to customize the slug function that create ids from string.

#### `appendIdToHeading`

(default: `true`)

To make anchor links work, should append id attribute to headings, but if other plugins have already done this, you can turn it off.

Ex:
```js
// ...
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ test("markdown-it-toc-and-anchor toc", t => {
"should work with soft breaks"
);

t.is(
mdIt(
`@[toc]
# Heading`,
{
toc: true,
appendIdToHeading: false
}
),
`<p><ul class="markdownIt-TOC">
<li><a href="#heading">Heading</a></li>
</ul>
</p>
<h1>Heading</h1>\n`,
"should not add id attribute if `appendIdToHeading` is falsy"
);


t.is(
mdIt(
`test
Expand Down Expand Up @@ -422,4 +440,5 @@ resetIds is true and toc is true`
same names on different renderings with the same markdownIt instance when
resetIds is false and toc is true`
);

});
35 changes: 19 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default function(md, options) {
anchorLinkSpace: true,
anchorLinkSymbolClassName: null,
wrapHeadingTextInAnchor: false,
appendIdToHeading: true,
...options
};

Expand Down Expand Up @@ -277,26 +278,28 @@ export default function(md, options) {
return true;
});

const originalHeadingOpen =
md.renderer.rules.heading_open ||
function(...args) {
const [tokens, idx, options, , self] = args;
return self.renderToken(tokens, idx, options);
};
if (options.appendIdToHeading) {
const originalHeadingOpen =
md.renderer.rules.heading_open ||
function(...args) {
const [tokens, idx, options, , self] = args;
return self.renderToken(tokens, idx, options);
};

md.renderer.rules.heading_open = function(...args) {
const [tokens, idx, , ,] = args;
md.renderer.rules.heading_open = function(...args) {
const [tokens, idx, , ,] = args;

const attrs = (tokens[idx].attrs = tokens[idx].attrs || []);
const anchor = tokens[idx + 1]._tocAnchor;
attrs.push(["id", anchor]);
const attrs = (tokens[idx].attrs = tokens[idx].attrs || []);
const anchor = tokens[idx + 1]._tocAnchor;
attrs.push(["id", anchor]);

if (options.anchorLink) {
renderAnchorLink(anchor, options, ...args);
}
if (options.anchorLink) {
renderAnchorLink(anchor, options, ...args);
}

return originalHeadingOpen.apply(this, args);
};
return originalHeadingOpen.apply(this, args);
};
}

md.renderer.rules.toc_open = () => "";
md.renderer.rules.toc_close = () => "";
Expand Down

0 comments on commit ec45059

Please sign in to comment.