Add test for unordered list indentation, enhance parser to handle nested lists with indentation, and bump version to 1.1.0

This commit is contained in:
2026-01-17 16:46:44 +01:00
parent deb96c665e
commit 382ee2f5aa
4 changed files with 42 additions and 5 deletions

View File

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

View File

@@ -66,7 +66,7 @@ sealed class MarkdownPart {
) : MarkdownPart()
data class UnorderedList(
val lines: List<String>,
val lines: List<Pair<Int, String>>,
) : MarkdownPart()
data class CheckboxList(

View File

@@ -80,7 +80,7 @@ fun markdown(text: String): List<MarkdownPart> {
continue
} else if (line.startsWith("- ") || line.startsWith("* ")) {
buffer.append("\n")
buffer.append(line.substring(2))
buffer.append(rawLine)
} else {
buffer.append(" ")
buffer.append(line)
@@ -146,7 +146,7 @@ fun markdown(text: String): List<MarkdownPart> {
line.startsWith("- ") || line.startsWith("* ") -> {
parseBuffer()
type = MarkdownType.UNORDERED_LIST
buffer.append(line.substring(2))
buffer.append(rawLine)
}
line.startsWith("|") -> {
@@ -220,6 +220,16 @@ private fun addCheckbox(
buffer.clear()
}
private fun String.countSpaces(): Int {
var count = 0
while (count < this.length && this[count] == ' ') {
count++
}
return count
}
private fun handleBuffer(
type: MarkdownType,
text: String,
@@ -242,7 +252,14 @@ private fun handleBuffer(
}
MarkdownType.UNORDERED_LIST -> {
listOf(MarkdownPart.UnorderedList(text.lines()))
val lines = text.lines()
val list = mutableListOf<Pair<Int, String>>()
for (line in lines) {
val indent: Int = line.countSpaces()
list.add((indent / 2) to line.substring(indent + 2))
}
listOf(MarkdownPart.UnorderedList(list))
}
MarkdownType.CHECKBOX_LIST -> {

View File

@@ -86,6 +86,26 @@ class ParseTest {
printMarkdownParts(md)
}
@Test
fun testUnorderedListIndentation() {
val input = """
Dit is een text
* First
More text
* Second
More text
* Sub 1
* Sub 2
Another paragraph
""".trimIndent()
val md = markdown(input)
printMarkdownParts(md)
}
@Test
fun testOrderedList() {
val input = """