generated from rnentjes/kotlin-server-web-empty
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:
@@ -8,7 +8,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "nl.astraeus"
|
||||
version = "1.0.11"
|
||||
version = "1.1.0"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 -> {
|
||||
|
||||
@@ -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 = """
|
||||
|
||||
Reference in New Issue
Block a user