Skip to content

Commit

Permalink
Merge pull request #52 from cpadilla/master
Browse files Browse the repository at this point in the history
Fixed header links to increment if duplicates found
  • Loading branch information
cpadilla committed Nov 11, 2021
2 parents 0f22759 + c0e2cb7 commit 7851a62
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ module.exports = function(md, o) {
var options = Object.assign({}, defaults, o);
var tocRegexp = options.markerPattern;
var gstate;
var links;

function toc(state, silent) {
var token;
var match;
links = [];

// Reject if the token does not start with [
if (state.src.charCodeAt(state.pos) !== 0x5B /* [ */ ) {
Expand Down Expand Up @@ -130,8 +132,17 @@ module.exports = function(md, o) {
if (options.transformLink) {
link = options.transformLink(link);
}
buffer = `<li><a href="${link}">`;
buffer += options.format(content, md, link);

// Check if this link has been generated before and increment link if so
var generatedLink = link;
var index = 2;
while (links.indexOf(generatedLink) >= 0) {
generatedLink = link + "-" + index++;
}
links.push(generatedLink);

buffer = `<li><a href="${generatedLink}">`;
buffer += options.format(content, md, generatedLink);
buffer += `</a>`;
i++;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/fixtures/simple-with-duplicate-headings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 id="an-article">An article</h1>
<p><div class="table-of-contents"><ul><li><a href="#an-article">An article</a><ul><li><a href="#sub-heading">Sub heading</a><ul><li><a href="#common-sub-heading">Common sub heading</a><ul><li><a href="#duplicate-heading">Duplicate heading</a></li><li><a href="#duplicate-heading-2">Duplicate heading</a></li></ul></li></ul></li><li><a href="#sub-heading-2">Sub heading 2</a><ul><li><a href="#common-sub-heading-2">Common sub heading</a></li></ul></li><li><a href="#duplicate-heading-3">Duplicate heading</a></li></ul></li></ul></div></p>
<h2 id="sub-heading">Sub heading</h2>
<p>Some nice text</p>
<h3 id="common-sub-heading">Common sub heading</h3>
<p>Some very nice text</p>
<h4 id="duplicate-heading">Duplicate heading</h4>
<p>Some exceptionally nice text</p>
<h4 id="duplicate-heading-2">Duplicate heading</h4>
<p>Some exceptionally nice text</p>
<h2 id="sub-heading-2">Sub heading 2</h2>
<p>Some even nicer text</p>
<h3 id="common-sub-heading-2">Common sub heading</h3>
<p>Some more nice text</p>
<h2 id="duplicate-heading-3">Duplicate heading</h2>
<p>Some exceptionally nice text</p>
24 changes: 24 additions & 0 deletions test/fixtures/simple-with-duplicate-headings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# An article

[[toc]]

## Sub heading
Some nice text

### Common sub heading
Some very nice text

#### Duplicate heading
Some exceptionally nice text

#### Duplicate heading
Some exceptionally nice text

## Sub heading 2
Some even nicer text

### Common sub heading
Some more nice text

## Duplicate heading
Some exceptionally nice text
14 changes: 12 additions & 2 deletions test/modules/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ var simple1LevelHTML = fs.readFileSync("test/fixtures/simple-1-level.html", "utf
var simpleWithAnchorsHTML = fs.readFileSync("test/fixtures/simple-with-anchors.html", "utf-8");
var simpleWithHeaderFooterHTML = fs.readFileSync("test/fixtures/simple-with-header-footer.html", "utf-8");
var simpleWithTransformLink = fs.readFileSync("test/fixtures/simple-with-transform-link.html", "utf-8");
var simpleWithHeadingLink = fs.readFileSync("test/fixtures/simple-with-heading-link.md", "utf-8");
var simpleWithHeadingLinkHTML = fs.readFileSync("test/fixtures/simple-with-heading-link.html", "utf-8");
var simpleWithHeadingLink = fs.readFileSync("test/fixtures/simple-with-heading-links.md", "utf-8");
var simpleWithHeadingLinkHTML = fs.readFileSync("test/fixtures/simple-with-heading-links.html", "utf-8");
var simpleWithDuplicateHeadings = fs.readFileSync("test/fixtures/simple-with-duplicate-headings.md", "utf-8");
var simpleWithDuplicateHeadingsHTML = fs.readFileSync("test/fixtures/simple-with-duplicate-headings.html", "utf-8");
var emptyMarkdown = defaultMarker;
var emptyMarkdownHtml = fs.readFileSync("test/fixtures/empty.html", "utf-8");
var fullTocSampleMarkdown = fs.readFileSync("test/fixtures/full-toc-sample.md", "utf-8");
Expand Down Expand Up @@ -158,4 +160,12 @@ describe("Testing Markdown rendering", function() {
assert.equal(adjustEOL(md.render(simpleWithHeadingLink)), simpleWithHeadingLinkHTML);
done();
});

it("Parses correctly with duplicate headers", function (done) {
md.use(markdownItTOC, {
"includeLevel": [1,2,3,4]
});
assert.equal(adjustEOL(md.render(simpleWithDuplicateHeadings)), simpleWithDuplicateHeadingsHTML);
done();
});
});

0 comments on commit 7851a62

Please sign in to comment.