From fc6df04b19eff226cf3da691186213826d1ac09e Mon Sep 17 00:00:00 2001 From: David Clark Date: Sat, 11 Jan 2020 16:23:49 -0700 Subject: [PATCH] Fix bug with duplicate headings inside and outside the TOC tree Register the slug of every heading, even if it is before the TOC. That way, if a heading within the TOC's tree is a duplicate of one outside the TOC tree, the TOC's slug links still be accurate. Closes #63. --- lib/search.js | 3 ++- test/fixtures/custom-heading/input.md | 2 ++ test/fixtures/custom-heading/output.json | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/search.js b/lib/search.js index 93613c1..26d6cca 100644 --- a/lib/search.js +++ b/lib/search.js @@ -47,6 +47,7 @@ function search(root, expression, settings) { function onheading(child, index, parent) { var value = toString(child) var id = child.data && child.data.hProperties && child.data.hProperties.id + var slug = slugs.slug(id || value) if (!parents(parent)) { return @@ -73,7 +74,7 @@ function search(root, expression, settings) { map.push({ depth: child.depth, children: child.children, - id: slugs.slug(id || value) + id: slug }) } } diff --git a/test/fixtures/custom-heading/input.md b/test/fixtures/custom-heading/input.md index 2714456..75adc75 100644 --- a/test/fixtures/custom-heading/input.md +++ b/test/fixtures/custom-heading/input.md @@ -10,4 +10,6 @@ Text. ## Something elsefi +## Normal + # Something iffi diff --git a/test/fixtures/custom-heading/output.json b/test/fixtures/custom-heading/output.json index 600984e..845f299 100644 --- a/test/fixtures/custom-heading/output.json +++ b/test/fixtures/custom-heading/output.json @@ -74,6 +74,28 @@ ] } ] + }, + { + "type": "listItem", + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#normal-1", + "children": [ + { + "type": "text", + "value": "Normal" + } + ] + } + ] + } + ] } ] }