summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/blog/md/LinkParser.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/blog/md/LinkParser.kt')
-rw-r--r--src/main/kotlin/moe/nea/blog/md/LinkParser.kt21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/LinkParser.kt b/src/main/kotlin/moe/nea/blog/md/LinkParser.kt
new file mode 100644
index 0000000..c247e6f
--- /dev/null
+++ b/src/main/kotlin/moe/nea/blog/md/LinkParser.kt
@@ -0,0 +1,21 @@
+package moe.nea.blog.md
+
+object LinkParser : InlineParser {
+ override fun detect(lookback: MarkdownFormat, rest: String): Boolean {
+ return rest.startsWith("[")
+ }
+
+ override val specialSyntax: Set<Char>
+ get() = "()[]".toSet()
+
+ override fun parse(parser: MarkdownParser, text: String): Pair<MarkdownFormat, String> {
+ val (label, rest) = parser.parseInlineTextUntil(
+ text.substring(1),
+ Begin()
+ ) { lookback, remaining -> remaining.startsWith("]") } // TODO: better begin
+ require(rest.startsWith("](")) { TODO("unlabeled links") }
+ val urlEndIndex = rest.indexOf(')')
+ val url = rest.substring(2, urlEndIndex)
+ return Pair(Link(parser.collapseInlineFormat(label, false), url.trim()), rest.substring(urlEndIndex + 1))
+ }
+} \ No newline at end of file