Skip to content

Commit 49e7466

Browse files
committed
fix: skip headers in HTML comment blocks
Ensure a header that is within an HTML comment (`<!-- -->`) doesn't show up in the TOC.
1 parent 1f38840 commit 49e7466

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/commonMain/kotlin/ch/derlin/bitdowntoc/BitGenerator.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ object BitGenerator {
7373

7474
while (iter.hasNext()) {
7575
val line = iter.next()
76-
val codeMarker = listOf('`', '~').firstNotNullOfOrNull { line.getCodeStart(it) }
7776

78-
if (!codeMarker.isNullOrBlank()) iter.consumeCode(codeMarker)
77+
if (iter.consumeCode('`', line) || iter.consumeCode('~', line))
78+
else if (iter.consumeHtmlCommentBlock(line))
7979
else if (commenter.isAnchor(line)) iter.remove()
8080
else {
8181
line.parseHeader(toc)?.let {
@@ -115,16 +115,29 @@ object BitGenerator {
115115
throw MissingTocEndException()
116116
}
117117

118-
private fun String.getCodeStart(char: Char): String? =
119-
codeStartTrimmer.replace(this, "")
118+
private fun Iterator<String>.consumeCode(char: Char, currentLine: String): Boolean {
119+
val codeStart = codeStartTrimmer.replace(currentLine, "")
120120
// We expect at least 3 for code start (```), but 4 is also supported.
121121
// See https://github.com/derlin/bitdowntoc/issues/65
122122
.takeIf { it.startsWith("$char".repeat(3)) }
123123
?.let { trimmedLine -> trimmedLine.takeWhile { it == char } }
124+
if (codeStart != null) {
125+
while (this.hasNext() && !this.next().trim().startsWith(codeStart));
126+
return true
127+
}
128+
return false
129+
}
124130

131+
private fun Iterator<String>.consumeHtmlCommentBlock(currentLine: String): Boolean {
132+
val htmlCommentStartIndex = currentLine.indexOf("<!--")
133+
val htmlCommentStopIndex = currentLine.indexOf("-->")
125134

126-
private fun Iterator<String>.consumeCode(codeMarker: String) {
127-
while (this.hasNext() && !this.next().trim().startsWith(codeMarker));
135+
// we only look for block comments, comments within a single line do not matter
136+
if (htmlCommentStartIndex >= 0 && htmlCommentStopIndex < htmlCommentStartIndex) {
137+
while (this.hasNext() && !this.next().contains("-->"));
138+
return true
139+
}
140+
return false
128141
}
129142

130143
private fun Iterable<String>.asText() = this.joinToString(NL)

src/commonTest/kotlin/ch.derlin.bitdowntoc/GenerateTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ class GenerateTest {
118118
""".trimIndent()
119119
)
120120

121+
assertDoesNotChangeToc(
122+
"""
123+
<!--
124+
## comment, not header
125+
-->
126+
127+
something <!-- something
128+
## html comment start end
129+
--> # end comment
130+
131+
<!-- ## comment inline, not header -->
132+
""".trimIndent()
133+
)
134+
121135
assertDoesNotChangeToc(
122136
"""
123137
- ```

0 commit comments

Comments
 (0)