A Kotlin DSL for effortless creation of beautiful Markdown pages, supporting both basic and extended syntax (Coming soon™️).
repositories {
mavenCentral()
}
dependencies {
// include for Common module
implementation("io.github.milis92.kotlin_markdown:basic:$latest_version_here")
// include for JVM target
implementation("io.github.milis92.kotlin_markdown:basic-jvm:$latest_version_here")
}
Basic implementation follows official Markdown syntax and should be supported by almost all Markdown processors.
Text
markdown {
line("This is a simple line of text")
}
markdown {
line {
"""
|A Kotlin DSL for effortless creation of beautiful Markdown pages,
|supporting both basic and extended syntax (Coming soon™️).
""".trimMargin()
}
}
markdown {
line("This is a" + "simple".bold() + "line".italic() + "of code".codeSpan())
//Or you can use spans directly if you want to have full lines emphasised:
bold("This is a bold text")
italic("This is an italic text")
codeSpan("This is a code span")
}
markdown {
bold("This is a bold text", EmphasisMarker.Underscore)
italic("This is an italic text", EmphasisMarker.Asterisks)
}
Paragraph
Every paragraph is consisted of multiple lines
and each line (except the last one) will be terminated
by a line break (2 space characters + new empty line)
markdown {
paragraph {
line { "First paragraph" }
line { "Second paragraph" }
}
}
Horizontal rules
markdown {
horizontalRule()
}
markdown {
horizontalRule(style = HorizontalRuleStyle.Hyphen)
}
Headings
Markdown supports two styles of headings:
- ATX Styled Headings
- Setext Styled Headings
markdown {
heading("This is an ATX styled heading")
}
markdown {
underlinedHeading("This is an Setext styled heading")
}
Note Markdown headings support only single line text as headings, so content will be automatically stripped of any new lihnes
markdown {
heading("This is an ATX styled heading", H1)
underlinedHeading("This is an Setext styled heading", H2)
}
markdown {
heading("This is an" + bold("ATX") + "styled heading", H1)
underlinedHeading("This is an" + italic("Setext") + "styled heading", H2)
}
Blockquotes
markdown {
blockQuote("Simple single line blocquote")
}
Blockquote can hold any other markdown element, including blocquote as well
markdown {
blockQuote {
heading("Heading 1")
underlinedHeading("Underlined Heading")
horizontalRule()
orderedList(listOf("Item 1", "Item 2"))
unorderedList(listOf("Item 1", "Item 2"))
}
}
Code Blocks
markdown {
codeBlock {
@Language("kotlin")
val block =
"""
|val hello = "Hello World"
""".trimMargin()
block
}
}
Note that you can use IJ Language injection to get a bit of help from your IDE
Image and Url Spans
markdown {
link("Google", URI("https://www.google.com"))
}
markdown {
image("Google", URI("https://www.google.com"))
}
markdown {
line {
"This is a link: " + "Google".link(URI("https://www.google.com"))
}
}
markdown {
line {
"This is an image: " + "Google".image(URI("https://www.google.com"))
}
}
Lists
Markdown supports 2 types of lists:
- Ordered lists (list with numbers)
- Unordered lists (list with bullets)
markdown {
orderedList(listOf("Item 1", "Item 2"))
}
markdown {
unorderedList(listOf("Item 1", "Item 2"))
}
markdown {
//Works the same for unordered list
orderedList {
item {
line { "First paragraph" }
line { "Second paragraph" }
}
item("Second item")
}
}
markdown {
//Works the same for unordered list
orderedList {
item {
line { "First item" }
unorderedList {
item("First sub item")
item("Second sub item")
}
}
item("Second item")
}
}
You can easily export Markdown content to a file using line writer of your choosing.
For example:
val markdown = markdown {
//your markdown content
}
File("../README").writeText(markdown.content)
This library is licensed under the Apache Version 2.0 License - see the License file for details.