Skip to content

Commit df05bff

Browse files
committed
ToC: Some HTML improvements and style fixes
The ToC generated broken HTML with unclosed `<li>` tags. This got fixed as well as some minor optimisation and adding list elements for the subentries so the elements appear in the ToC while scrolling. Signed-off-by: Sheogorath <[email protected]>
1 parent cf934a4 commit df05bff

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

public/css/extra.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
color: #777;
9494
}
9595

96+
.toc .invisable-node {
97+
list-style-type: none;
98+
}
99+
96100
.ui-toc {
97101
position: fixed;
98102
bottom: 20px;

public/vendor/md-toc.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,17 @@
5252
titleNames = this._elTitlesNames
5353
ulClass = this.ulClass
5454
}
55-
var titleElementsLen = titleElements.length
5655
// No need to do anything for an empty ToC
57-
if (!titleElementsLen) return
56+
if (!titleElements.length) return
5857

59-
this.tocContent = ''
6058
var content = '<ul'
6159
if (ulClass) {
6260
content += ' class="' + ulClass + '"'
6361
}
64-
content += '>'
62+
content += '>\n'
6563
var iterTag = titleNames[level]
6664
var recurse = false
65+
var openTag = false
6766

6867
for (var element; element = titleElements.shift();) {
6968
var elementTag = element.tagName.toLowerCase()
@@ -80,13 +79,21 @@
8079
} else {
8180
id = '#' + id
8281
}
82+
if (openTag) {
83+
content += '</li>\n'
84+
openTag = false
85+
}
8386
content += '<li><a href="' + id + '" title="'+ elementTitle +'">' + elementText + '</a>'
8487
// Reset recursion. We need it for the next subsections
8588
recurse = false
86-
89+
openTag = true
8790
// Check if the current element has a lower level than ours, if so, we have to go down the rabbithole!
8891
} else if (!recurse && titleNames.indexOf(elementTag.toLowerCase()) > level) {
8992
recurse = true
93+
if (!openTag) {
94+
content += '<li class="invisable-node">'
95+
openTag = true
96+
}
9097
// This element is for the lower lever, we have to re-add it before we send the list down there.
9198
titleElements.unshift(element)
9299
// Let's call ourself and get to the next level
@@ -99,7 +106,10 @@
99106
}
100107
}
101108

102-
content += '</ul>'
109+
if (openTag) {
110+
content += '</li>\n'
111+
}
112+
content += '</ul>\n'
103113

104114
// Set ToC content of the level 0 everything else pass things to the upper level!
105115
if (level === 0) {

0 commit comments

Comments
 (0)