Skip to content

Commit 271dff3

Browse files
authored
Merge pull request #1043 from SISheogorath/fix/tocEmptyHead
Fix ToC breaking documents with empty h* elements
2 parents f46a84a + d6dd336 commit 271dff3

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

public/js/extra.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,13 @@ const linkifyAnchors = (level, containingElement) => {
846846
let header = headers[i]
847847
if (header.getElementsByClassName('anchor').length === 0) {
848848
if (typeof header.id === 'undefined' || header.id === '') {
849-
// to escape characters not allow in css and humanize
849+
// to escape characters not allow in css and humanize
850850
const id = slugifyWithUTF8(getHeaderContent(header))
851851
header.id = id
852852
}
853-
header.insertBefore(anchorForId(header.id), header.firstChild)
853+
if (!(typeof header.id === 'undefined' || header.id === '')) {
854+
header.insertBefore(anchorForId(header.id), header.firstChild)
855+
}
854856
}
855857
}
856858
}

public/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,7 @@ function updateViewInner () {
27882788
renderTOC(ui.area.markdown)
27892789
generateToc('ui-toc')
27902790
generateToc('ui-toc-affix')
2791+
autoLinkify(ui.area.markdown)
27912792
generateScrollspy()
27922793
updateScrollspy()
27932794
smoothHashScroll()

public/vendor/md-toc.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545
}
4646

47-
Toc.prototype._createTocContent = function recursiveToc(level = 0, titleElements = [], titleNames = [], ulClass = undefined) {
47+
Toc.prototype._createTocContent = function recursiveToc(level = 0, titleElements = [], titleNames = [], ulClass = undefined, index = 0) {
4848
// Inititalize our elements from the toc object
4949
// which is only available on level 0
5050
if (level === 0) {
@@ -74,8 +74,8 @@
7474
var elementText = (typeof this.process === 'function' ? this.process(element) : element.innerHTML).replace(/<(?:.|\n)*?>/gm, '')
7575
var id = element.getAttribute('id')
7676
if (!id) {
77-
element.setAttribute('id', 'tip' + i)
78-
id = '#tip' + i
77+
element.setAttribute('id', 'tip' + ++index)
78+
id = '#tip' + index
7979
} else {
8080
id = '#' + id
8181
}
@@ -97,7 +97,7 @@
9797
// This element is for the lower lever, we have to re-add it before we send the list down there.
9898
titleElements.unshift(element)
9999
// Let's call ourself and get to the next level
100-
content += recursiveToc(level + 1, titleElements, titleNames, ulClass)
100+
content += recursiveToc(level + 1, titleElements, titleNames, ulClass, index)
101101
} else {
102102
// When we end up here, met a higher level element
103103
// This is not our business so back into the list with the element and let's end this loop

0 commit comments

Comments
 (0)