aboutsummaryrefslogtreecommitdiff
path: root/src/Markdown/MarkdownProcessor.kt
blob: 13023a2ce326d3f1fb2b8d31ea256255166a1294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package org.jetbrains.dokka

import org.intellij.markdown.*
import org.intellij.markdown.ast.*
import org.intellij.markdown.parser.*
import org.intellij.markdown.parser.dialects.commonmark.CommonMarkMarkerProcessor

class MarkdownNode(val node: ASTNode, val parent: MarkdownNode?, val markdown: String) {
    val children: List<MarkdownNode> = node.children.map { MarkdownNode(it, this, markdown) }
    val type: IElementType get() = node.type
    val text: String get() = markdown.substring(node.startOffset, node.endOffset)
    fun child(type: IElementType): MarkdownNode? = children.firstOrNull { it.type == type }

    override fun toString(): String = StringBuilder { presentTo(this) }.toString()
}

fun MarkdownNode.visit(action: (MarkdownNode, () -> Unit) -> Unit) {
    action(this) {
        for (child in children) {
            child.visit(action)
        }
    }
}

public fun MarkdownNode.toTestString(): String {
    val sb = StringBuilder()
    var level = 0
    visit { node, visitChildren ->
        sb.append(" ".repeat(level * 2))
        node.presentTo(sb)
        level++
        visitChildren()
        level--
    }
    return sb.toString()
}

private fun MarkdownNode.presentTo(sb: StringBuilder) {
    sb.append(type.toString())
    sb.append(":" + text.replace("\n", "\u23CE"))
    sb.appendln()
}

fun parseMarkdown(markdown: String): MarkdownNode {
    if (markdown.isEmpty())
        return MarkdownNode(LeafASTNode(MarkdownElementTypes.MARKDOWN_FILE, 0, 0), null, markdown)
    return MarkdownNode(MarkdownParser(CommonMarkMarkerProcessor.Factory).buildMarkdownTreeFromString(markdown), null, markdown)
}