From 340fc50af3a33f5d3f2fc1928d6ce34baae0bd4e Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 24 Mar 2024 16:15:13 +0100 Subject: Add freestanding star test --- src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 14 +++++++++----- src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt index 19a4e99..40c4c30 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -70,17 +70,21 @@ class MarkdownParser(source: String) { } fun parseInlineTextOnce(lookback: MarkdownFormat, text: String): Pair { + require(text.isNotEmpty()) // TODO handle empty string val parser = inlineParsers.find { it.detect(lookback, text) } - if (parser != null) + if (parser != null) { return parser.parse(this, text) - require(!text.isEmpty()) // TODO handle empty string - if (text[0] == ' ') + } + if (text[0] == ' ') { return Pair(Whitespace(), text.substring(1)) + } val nextSpecial = text.indexOfFirst { it in inlineParsers.flatMap { it.specialSyntax } || it == ' ' } - if (nextSpecial == 0) + if (nextSpecial == 0) { return Pair(Word(text.substring(0, 1)), text.substring(1)) - if (nextSpecial == -1) + } + if (nextSpecial == -1) { return Pair(Word(text), "") + } return Pair(Word(text.substring(0, nextSpecial)), text.substring(nextSpecial)) } diff --git a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt index b4b102a..0e08473 100644 --- a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt +++ b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt @@ -55,6 +55,16 @@ class TestItalics { assertInlineFormat("left italics middle bold right", "left *italics* middle **bold** right") } + @Test + fun testFreestandingStar() { + assertInlineFormat("left * right", "left * right") + } + + @Test + fun testFreestandingStarInItalics() { + assertInlineFormat("left * right", "*left * right*") + } + @Test fun testNested() { assertInlineFormat("a bold b", "*a **bold** b*") -- cgit