-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMarkdownParser.kt
More file actions
61 lines (51 loc) · 1.88 KB
/
MarkdownParser.kt
File metadata and controls
61 lines (51 loc) · 1.88 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
package ch.tiim.markdown_widget
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension
import com.vladsch.flexmark.ext.tables.TablesExtension
import com.vladsch.flexmark.ext.wikilink.WikiLink
import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension
import com.vladsch.flexmark.html.HtmlRenderer
import com.vladsch.flexmark.parser.Parser
import com.vladsch.flexmark.util.ast.Node
import com.vladsch.flexmark.util.data.MutableDataSet
import com.vladsch.flexmark.util.misc.Extension
import org.jetbrains.annotations.NotNull
import java.util.*
class MarkdownParser(private val theme:String) {
val parser: Parser
val renderer: HtmlRenderer
init {
val options = MutableDataSet()
// uncomment to set optional extensions
options.set(Parser.EXTENSIONS,
Arrays.asList(
TablesExtension.create(),
StrikethroughExtension.create(),
TaskListExtension.create(),
WikiLinkExtension.create(),
YamlFrontMatterExtension.create(),
) as @NotNull Collection<Extension>
);
// uncomment to convert soft-breaks to hard breaks
options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");
parser = Parser.builder(options).build()
renderer = HtmlRenderer.builder(options).build()
}
fun parse(md: String): String {
val document: Node = parser.parse(md)
val html = renderer.render(document)
return """
<!DOCTYPE html>
<html>
<head>
<style>
$theme
</style>
</head>
<body>
$html
</body>
</html>"""
}
}