Skip to content

Commit d9646ce

Browse files
davidtheclarkwooorm
authored andcommitted
Fix duplicate headings in- and outside of the contents
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 GH-63. Closes GH-64. Reviewed-by: Jonathan Haines <jonno.haines@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 1f187ef commit d9646ce

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function search(root, expression, settings) {
4747
function onheading(child, index, parent) {
4848
var value = toString(child)
4949
var id = child.data && child.data.hProperties && child.data.hProperties.id
50+
var slug = slugs.slug(id || value)
5051

5152
if (!parents(parent)) {
5253
return
@@ -73,7 +74,7 @@ function search(root, expression, settings) {
7374
map.push({
7475
depth: child.depth,
7576
children: child.children,
76-
id: slugs.slug(id || value)
77+
id: slug
7778
})
7879
}
7980
}

test/fixtures/custom-heading/input.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Text.
1010

1111
## Something elsefi
1212

13+
## Normal
14+
1315
# Something iffi

test/fixtures/custom-heading/output.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@
7474
]
7575
}
7676
]
77+
},
78+
{
79+
"type": "listItem",
80+
"spread": false,
81+
"children": [
82+
{
83+
"type": "paragraph",
84+
"children": [
85+
{
86+
"type": "link",
87+
"title": null,
88+
"url": "#normal-1",
89+
"children": [
90+
{
91+
"type": "text",
92+
"value": "Normal"
93+
}
94+
]
95+
}
96+
]
97+
}
98+
]
7799
}
78100
]
79101
}

0 commit comments

Comments
 (0)