diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-24 15:54:19 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-24 15:54:19 +0100 |
commit | 4c0ac30dc82677b2b2db36d114b452fdea537a37 (patch) | |
tree | d29b45e396fab55ac119298143f4b7abb2cc9c8d /src/main/kotlin/moe/nea/blog | |
parent | be66c61d729fe642e3dd3ed2214990f9f2273c5e (diff) | |
download | blog-infra-4c0ac30dc82677b2b2db36d114b452fdea537a37.tar.gz blog-infra-4c0ac30dc82677b2b2db36d114b452fdea537a37.tar.bz2 blog-infra-4c0ac30dc82677b2b2db36d114b452fdea537a37.zip |
Add italics parsing unit tests
Diffstat (limited to 'src/main/kotlin/moe/nea/blog')
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt | 6 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 7 |
2 files changed, 7 insertions, 6 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt index 30b5c38..d3c9d5d 100644 --- a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt @@ -2,7 +2,7 @@ package moe.nea.blog.md object ItalicsParser : InlineParser { override fun detect(lookback: MarkdownFormat, rest: String): Boolean { - return lookback is Whitespace && "\\*+[^ ].*".toRegex().matches(rest) + return (lookback is Whitespace || lookback is Begin) && "\\*+[^ ].*".toRegex().matches(rest) } override val specialSyntax: Set<Char> @@ -48,10 +48,6 @@ object ItalicsParser : InlineParser { if (secondStarCount == 1) firstElement = Italics(firstElement) if (secondStarCount == firstStarCount) { - if (isBold) - firstElement = Bold(firstElement) - if (isItalics) - firstElement = Italics(firstElement) return Pair(firstElement, remainingText) } diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt index f8095ad..144eeda 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -58,6 +58,10 @@ class MarkdownParser(source: String) { if (text[0] == ' ') return Pair(Whitespace(), text.substring(1)) val nextSpecial = text.indexOfFirst { it in inlineParsers.flatMap { it.specialSyntax } || it == ' ' } + if (nextSpecial == 0) + return Pair(Word(text.substring(0, 1)), text.substring(1)) + if (nextSpecial == -1) + return Pair(Word(text), "") return Pair(Word(text.substring(0, nextSpecial)), text.substring(nextSpecial)) } @@ -103,7 +107,8 @@ class MarkdownParser(source: String) { } fun collapseInlineFormat(sequence: List<MarkdownFormat>, trimWhitespace: Boolean): MarkdownFormat { - return FormatSequence(collapseMarkdownFormats(expandMarkdownFormats(sequence), trimWhitespace)) + val formats = collapseMarkdownFormats(expandMarkdownFormats(sequence), trimWhitespace) + return formats.singleOrNull() ?: FormatSequence(formats) } fun readDocument(): Document { |