@@ -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 )
0 commit comments