Merge pull request #1043 from SISheogorath/fix/tocEmptyHead

Fix ToC breaking documents with empty h* elements
This commit is contained in:
Christoph (Sheogorath) Kern 2018-11-19 21:33:34 +01:00 committed by GitHub
commit 271dff3808
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 6 deletions

View file

@ -850,10 +850,12 @@ const linkifyAnchors = (level, containingElement) => {
const id = slugifyWithUTF8(getHeaderContent(header)) const id = slugifyWithUTF8(getHeaderContent(header))
header.id = id header.id = id
} }
if (!(typeof header.id === 'undefined' || header.id === '')) {
header.insertBefore(anchorForId(header.id), header.firstChild) header.insertBefore(anchorForId(header.id), header.firstChild)
} }
} }
} }
}
export function autoLinkify (view) { export function autoLinkify (view) {
const contentBlock = view[0] const contentBlock = view[0]

View file

@ -2788,6 +2788,7 @@ function updateViewInner () {
renderTOC(ui.area.markdown) renderTOC(ui.area.markdown)
generateToc('ui-toc') generateToc('ui-toc')
generateToc('ui-toc-affix') generateToc('ui-toc-affix')
autoLinkify(ui.area.markdown)
generateScrollspy() generateScrollspy()
updateScrollspy() updateScrollspy()
smoothHashScroll() smoothHashScroll()

View file

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