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.

This commit is contained in:
2026-01-10 12:37:09 +01:00
parent 11a6f3a57b
commit 7f48f5d5cd
3 changed files with 85 additions and 28 deletions

View File

@@ -8,7 +8,7 @@ plugins {
} }
group = "nl.astraeus" group = "nl.astraeus"
version = "1.0.5" version = "1.0.6"
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -36,40 +36,47 @@ fun markdown(text: String): List<MarkdownPart> {
//println("BUFFER [${buffer.length}] TYPE ${type} \t LINE - ${line}") //println("BUFFER [${buffer.length}] TYPE ${type} \t LINE - ${line}")
when { when {
type == MarkdownType.ORDERED_LIST -> { type == MarkdownType.ORDERED_LIST -> {
if (!line.startsWith("${listIndex++}.") && !line.startsWith("-.")) { if (line.isBlank()) {
parseBuffer() parseBuffer()
continue continue
} else { } else if (line.startsWith("${listIndex++}.") || line.startsWith("-.")) {
buffer.append(line.substring(2))
buffer.append("\n") buffer.append("\n")
buffer.append(line.substring(2))
} else {
buffer.append(" ")
buffer.append(line)
} }
} }
type == MarkdownType.CHECKBOX_LIST -> { 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)) parts.add(MarkdownPart.CheckboxList(checkboxList))
parseBuffer() parseBuffer()
continue continue
} else if (line.startsWith("- [ ]") || line.startsWith("- [x]")) {
if (buffer.isNotBlank()) {
addCheckbox(checkboxList, index, buffer)
}
buffer.append(line)
} else { } else {
checkboxList.add( buffer.append(" ")
CheckboxItem( buffer.append(line)
index,
line.startsWith("- [x]"),
line.substring(5).trim()
)
)
} }
} }
type == MarkdownType.UNORDERED_LIST -> { type == MarkdownType.UNORDERED_LIST -> {
if (!line.startsWith("- ") && if (line.isBlank()) {
!line.startsWith("* ")
) {
parseBuffer() parseBuffer()
continue continue
} else { } else if (line.startsWith("- ") || line.startsWith("* ")) {
buffer.append(line.substring(2))
buffer.append("\n") buffer.append("\n")
buffer.append(line.substring(2))
} else {
buffer.append(" ")
buffer.append(line)
} }
} }
@@ -120,26 +127,18 @@ fun markdown(text: String): List<MarkdownPart> {
type = MarkdownType.ORDERED_LIST type = MarkdownType.ORDERED_LIST
listIndex = 2 listIndex = 2
buffer.append(line.substring(2)) buffer.append(line.substring(2))
buffer.append("\n")
} }
line.startsWith("- [ ]") || line.startsWith("- [x]") -> { line.startsWith("- [ ]") || line.startsWith("- [x]") -> {
parseBuffer() parseBuffer()
type = MarkdownType.CHECKBOX_LIST type = MarkdownType.CHECKBOX_LIST
checkboxList.add( buffer.append(line)
CheckboxItem(
index,
line.startsWith("- [x]"),
line.substring(5).trim()
)
)
} }
line.startsWith("- ") || line.startsWith("* ") -> { line.startsWith("- ") || line.startsWith("* ") -> {
parseBuffer() parseBuffer()
type = MarkdownType.UNORDERED_LIST type = MarkdownType.UNORDERED_LIST
buffer.append(line.substring(2)) buffer.append(line.substring(2))
buffer.append("\n")
} }
line.startsWith("|") -> { line.startsWith("|") -> {
@@ -191,6 +190,25 @@ fun markdown(text: String): List<MarkdownPart> {
return parts return parts
} }
private fun addCheckbox(
checkboxList: MutableList<CheckboxItem>,
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( private fun handleBuffer(
type: MarkdownType, type: MarkdownType,
text: String, text: String,

View File

@@ -50,13 +50,52 @@ class ParseTest {
printMarkdownParts(md) 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 @Test
fun testCheckboxList() { fun testCheckboxList() {
val input = """ val input = """
Dit is een text Dit is een text
- [ ] Not checked - [ ] Not checked,
- [x] Checked with some more text here
- [x] Checked!
- [x] Checked,
text it!
Meer text Meer text
""".trimIndent() """.trimIndent()