mirror of https://github.com/status-im/codimd.git
Fix ToC breaking documents with empty h* elements
Right now, the ToC has an undefined variable i that was an index in the
original ToC code. Since the major rewrite in
4fe0620853
it's a recursive function
without this index. The variable `i` was wrongly copied into its current
place from the old code.
This patch replaces the variable `i` with the index of the header
element. Fix the undefined variable problem.
Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
This commit is contained in:
parent
4e5e7df4f8
commit
9951b7df7c
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue