generated from rnentjes/kotlin-server-web-empty
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:
@@ -8,7 +8,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "nl.astraeus"
|
||||
version = "1.0.5"
|
||||
version = "1.0.6"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -36,40 +36,47 @@ fun markdown(text: String): List<MarkdownPart> {
|
||||
//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<MarkdownPart> {
|
||||
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<MarkdownPart> {
|
||||
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(
|
||||
type: MarkdownType,
|
||||
text: String,
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user