From 7f48f5d5cd0964bfcaed081eb77c9f2029b40748 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sat, 10 Jan 2026 12:37:09 +0100 Subject: [PATCH] Extend list parsing to handle multi-line items, improve checkbox list parsing with edge case handling, add unordered/ordered list tests, and update version to 1.0.6. --- build.gradle.kts | 2 +- .../nl/astraeus/markdown/parser/Parser.kt | 68 ++++++++++++------- .../nl/astraeus/markdown/parser/ParseTest.kt | 43 +++++++++++- 3 files changed, 85 insertions(+), 28 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a72a0fe..b12e55a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "nl.astraeus" -version = "1.0.5" +version = "1.0.6" repositories { mavenCentral() diff --git a/src/commonMain/kotlin/nl/astraeus/markdown/parser/Parser.kt b/src/commonMain/kotlin/nl/astraeus/markdown/parser/Parser.kt index 2640e68..14c0878 100644 --- a/src/commonMain/kotlin/nl/astraeus/markdown/parser/Parser.kt +++ b/src/commonMain/kotlin/nl/astraeus/markdown/parser/Parser.kt @@ -36,40 +36,47 @@ fun markdown(text: String): List { //println("BUFFER [${buffer.length}] TYPE ${type} \t LINE - ${line}") when { type == MarkdownType.ORDERED_LIST -> { - if (!line.startsWith("${listIndex++}.") && !line.startsWith("-.")) { + if (line.isBlank()) { parseBuffer() continue - } else { - buffer.append(line.substring(2)) + } else if (line.startsWith("${listIndex++}.") || line.startsWith("-.")) { buffer.append("\n") + buffer.append(line.substring(2)) + } else { + buffer.append(" ") + buffer.append(line) } } type == MarkdownType.CHECKBOX_LIST -> { - if (!line.startsWith("- [ ]") && !line.startsWith("- [x]")) { + if (line.isBlank()) { + if (buffer.isNotBlank()) { + addCheckbox(checkboxList, index, buffer) + } parts.add(MarkdownPart.CheckboxList(checkboxList)) parseBuffer() continue + } else if (line.startsWith("- [ ]") || line.startsWith("- [x]")) { + if (buffer.isNotBlank()) { + addCheckbox(checkboxList, index, buffer) + } + buffer.append(line) } else { - checkboxList.add( - CheckboxItem( - index, - line.startsWith("- [x]"), - line.substring(5).trim() - ) - ) + buffer.append(" ") + buffer.append(line) } } type == MarkdownType.UNORDERED_LIST -> { - if (!line.startsWith("- ") && - !line.startsWith("* ") - ) { + if (line.isBlank()) { parseBuffer() continue - } else { - buffer.append(line.substring(2)) + } else if (line.startsWith("- ") || line.startsWith("* ")) { buffer.append("\n") + buffer.append(line.substring(2)) + } else { + buffer.append(" ") + buffer.append(line) } } @@ -120,26 +127,18 @@ fun markdown(text: String): List { type = MarkdownType.ORDERED_LIST listIndex = 2 buffer.append(line.substring(2)) - buffer.append("\n") } line.startsWith("- [ ]") || line.startsWith("- [x]") -> { parseBuffer() type = MarkdownType.CHECKBOX_LIST - checkboxList.add( - CheckboxItem( - index, - line.startsWith("- [x]"), - line.substring(5).trim() - ) - ) + buffer.append(line) } line.startsWith("- ") || line.startsWith("* ") -> { parseBuffer() type = MarkdownType.UNORDERED_LIST buffer.append(line.substring(2)) - buffer.append("\n") } line.startsWith("|") -> { @@ -191,6 +190,25 @@ fun markdown(text: String): List { return parts } +private fun addCheckbox( + checkboxList: MutableList, + index: Int, + buffer: StringBuilder +) { + if (buffer.length >= 5) { + checkboxList.add( + CheckboxItem( + index, + buffer.startsWith("- [x]"), + buffer.substring(5).trim() + ) + ) + } else { + println("Invalid checkbox format: $buffer") + } + buffer.clear() +} + private fun handleBuffer( type: MarkdownType, text: String, diff --git a/src/commonTest/kotlin/nl/astraeus/markdown/parser/ParseTest.kt b/src/commonTest/kotlin/nl/astraeus/markdown/parser/ParseTest.kt index 552d53c..d870014 100644 --- a/src/commonTest/kotlin/nl/astraeus/markdown/parser/ParseTest.kt +++ b/src/commonTest/kotlin/nl/astraeus/markdown/parser/ParseTest.kt @@ -50,13 +50,52 @@ class ParseTest { printMarkdownParts(md) } + @Test + fun testUnorderedList() { + val input = """ + Dit is een text + + - First + More text + - Second + More text + + Another paragraph + """.trimIndent() + + val md = markdown(input) + + printMarkdownParts(md) + } + + @Test + fun testOrderedList() { + val input = """ + Dit is een text + + -. First + More text + -. Second + More text + + Another paragraph + """.trimIndent() + + val md = markdown(input) + + printMarkdownParts(md) + } + @Test fun testCheckboxList() { val input = """ Dit is een text - - [ ] Not checked - - [x] Checked + - [ ] Not checked, + with some more text here + - [x] Checked! + - [x] Checked, + text it! Meer text """.trimIndent()