summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt')
-rw-r--r--src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt b/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt
index 0c894fe..b7a0e9b 100644
--- a/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt
+++ b/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt
@@ -1,12 +1,15 @@
package moe.nea.blog.md
+import moe.nea.blog.util.indentSize
+
object CodeBlockParser : BlockParser {
override fun detect(line: String): Boolean {
return line.startsWith("```")
}
override fun parse(parser: MarkdownParser): MarkdownBlock {
- val language = parser.consumeLine()!!.removePrefix("```")
+ val tags = parser.consumeLine()!!.removePrefix("```").trim().split(" +".toRegex())
+ val language = tags.firstOrNull() ?: ""
val lines = mutableListOf<String>()
while (true) {
val line = parser.consumeLine() ?: error("Unfinished code block")
@@ -14,7 +17,10 @@ object CodeBlockParser : BlockParser {
break
lines.add(line)
}
- return CodeBlock(language, lines)
+ val commonIndent =
+ if (tags.contains("notrim")) 0
+ else lines.minOfOrNull { it.indentSize() ?: Int.MAX_VALUE } ?: 0
+ return CodeBlock(language, lines.map { it.drop(commonIndent) })
}
override val prio: Int