summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/blog/md/LinkParser.kt
blob: c247e6f33d6d2afc572d15d33c831ef70c39e865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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))
    }
}