Skip to content

Commit

Permalink
Fix duplicate headings in- and outside of the contents
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
davidtheclark authored and wooorm committed Jan 13, 2020
1 parent 1f187ef commit d9646ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,7 +74,7 @@ function search(root, expression, settings) {
map.push({
depth: child.depth,
children: child.children,
id: slugs.slug(id || value)
id: slug
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/custom-heading/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Text.

## Something elsefi

## Normal

# Something iffi
22 changes: 22 additions & 0 deletions test/fixtures/custom-heading/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@
]
}
]
},
{
"type": "listItem",
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#normal-1",
"children": [
{
"type": "text",
"value": "Normal"
}
]
}
]
}
]
}
]
}
Expand Down

0 comments on commit d9646ce

Please sign in to comment.