diff --git a/library/build.gradle.kts b/library/build.gradle.kts index b327332..20409e8 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -27,12 +27,11 @@ kotlin { linuxX64() mingwX64() @OptIn(ExperimentalWasmDsl::class) - wasmWasi { - nodejs() - } js { browser() nodejs() + binaries.library() + generateTypeScriptDefinitions() } sourceSets { diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt index f1e8785..0bc0ba1 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt @@ -1,71 +1,74 @@ package dev.usbharu.markdown -import kotlin.collections.List +import kotlin.js.JsExport +@JsExport sealed class AstNode { open fun print(): String { return toString() } -} -data class RootNode(val node: AstNode) : AstNode() { - override fun print(): String { - return node.print() + data class RootNode(val node: AstNode) : AstNode() { + override fun print(): String { + return node.print() + } + } + + data class BodyNode(val body: List) : AstNode() { + override fun print(): String { + return body.joinToString("\n") { it.print() } + } + } + + sealed class BlockNode : AstNode() + + data class HeaderNode(val header: Int, val headerTextNode: HeaderTextNode?) : BlockNode() { + override fun print(): String { + return "#".repeat(header) + " " + headerTextNode?.print().orEmpty() + } + } + + data class HeaderTextNode(val text: String) : BlockNode() { + override fun print(): String { + return text + } + } + + sealed interface QuotableNode + data class QuoteNode(val nodes: List) : AstNode(), QuotableNode + data object SeparatorNode : BlockNode() { + override fun print(): String { + return "---" + } + } + + sealed class ListNode : BlockNode() + data class DiscListNode(val node: InlineNode, val childList: List) : ListNode() + data class DecimalListNode(val node: InlineNode, val childList: List) : ListNode() + data class ParagraphNode(val nodes: List) : ListNode() { + override fun print(): String { + return nodes.joinToString("\n") { it.print() } + } + } + + sealed class InlineNode : AstNode(), QuotableNode + data class ItalicNode(val nodes: MutableList) : InlineNode() { + override fun print(): String { + return nodes.joinToString("", prefix = "*", postfix = "*") { it.print() } + } + } + + data class BoldNode(val nodes: MutableList) : InlineNode() { + override fun print(): String { + return nodes.joinToString("", prefix = "**", postfix = "**") { it.print() } + } + } + + data class StrikeThroughNode(val nodes: List) : InlineNode() + data class PlainText(val text: String) : InlineNode() { + override fun print(): String { + return text + } } } -data class BodyNode(val body: List) : AstNode() { - override fun print(): String { - return body.joinToString("\n") { it.print() } - } -} - -sealed class BlockNode : AstNode() - -data class HeaderNode(val header: Int, val headerTextNode: HeaderTextNode?) : BlockNode() { - override fun print(): String { - return "#".repeat(header) + " " + headerTextNode?.print().orEmpty() - } -} - -data class HeaderTextNode(val text: String) : BlockNode() { - override fun print(): String { - return text - } -} - -sealed interface QuotableNode -data class QuoteNode(val nodes: List) : AstNode(), QuotableNode -data object SeparatorNode : BlockNode() { - override fun print(): String { - return "---" - } -} -sealed class ListNode : BlockNode() -data class DiscListNode(val node: InlineNode, val childList: List) : ListNode() -data class DecimalListNode(val node: InlineNode, val childList: List) : ListNode() -data class ParagraphNode(val nodes: List) : ListNode() { - override fun print(): String { - return nodes.joinToString("\n") { it.print() } - } -} - -sealed class InlineNode : AstNode(), QuotableNode -data class ItalicNode(val nodes: MutableList) : InlineNode() { - override fun print(): String { - return nodes.joinToString("", prefix = "*", postfix = "*") { it.print() } - } -} - -data class BoldNode(val nodes: MutableList) : InlineNode() { - override fun print(): String { - return nodes.joinToString("", prefix = "**", postfix = "**") { it.print() } - } -} - -data class StrikeThroughNode(val nodes: List) : InlineNode() -data class PlainText(val text: String) : InlineNode() { - override fun print(): String { - return text - } -} \ No newline at end of file diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt index 8887aae..2d1ec3f 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt @@ -1,7 +1,10 @@ package dev.usbharu.markdown +import dev.usbharu.markdown.Token.* import kotlin.collections.List +import kotlin.js.JsExport +@JsExport class Lexer { fun lex(input: String): List { val tokens = mutableListOf() diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt index d661a2c..2372b97 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt @@ -1,7 +1,11 @@ package dev.usbharu.markdown +import dev.usbharu.markdown.AstNode.* +import dev.usbharu.markdown.Token.* import kotlin.collections.List +import kotlin.js.JsExport +@JsExport class Parser { fun parse(tokens: List): AstNode { val iterator = PeekableTokenIterator(tokens) @@ -18,7 +22,7 @@ class Parser { Exclamation -> TODO() is Header -> header(next, iterator) is Html -> TODO() - is dev.usbharu.markdown.List -> TODO() + is Token.List -> TODO() ParenthesesEnd -> TODO() ParenthesesStart -> TODO() is Quote -> TODO() diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt index fee0355..c9025c2 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt @@ -1,40 +1,44 @@ package dev.usbharu.markdown -sealed class Token() +import kotlin.js.JsExport -data class Text(var text: String) : Token() -data class Break(var count: Int) : Token() -data class Header(var count: Int) : Token() -data class Quote(var count: Int) : Token() -data class Separator(var count: Int, val char: Char) : Token() -data class Whitespace(var count: Int, val whitespace: Char) : Token() -abstract class List(val type: ListType) : Token() { - enum class ListType { - DISC, - DECIMAL +@JsExport +sealed class Token { + data class Text(var text: String) : Token() + data class Break(var count: Int) : Token() + data class Header(var count: Int) : Token() + data class Quote(var count: Int) : Token() + data class Separator(var count: Int, val char: Char) : Token() + data class Whitespace(var count: Int, val whitespace: Char) : Token() + abstract class List(val type: ListType) : Token() { + enum class ListType { + DISC, + DECIMAL + } } + + data object DiscList : List(ListType.DISC) + data class DecimalList(val number: Char) : List(ListType.DECIMAL) + data class CheckBox(val checked: Boolean) : Token() + data object SquareBracketStart : Token() + data object SquareBracketEnd : Token() + data object ParenthesesStart : Token() + data object ParenthesesEnd : Token() + data class Url(var url: String) : Token() + data class Asterisk(var count: Int, var char: Char) : Token() + data object Exclamation : Token() + data class UrlTitle(val title: String) : Token() + data class InlineCodeBlock(val text: String) : Token() + data class CodeBlock(val text: String) : Token() + data class CodeBlockLanguage(val language: String, val filename: String) : Token() + data class Strike(val strike: String) : Token() + + abstract class Html() : Token() + data class StartTagStart(var tag: String, val void: Boolean) : Html() + data class EndTagStart(var tag: String) : Html() + data class TagEnd(var tag: String) : Html() + data class AttributeName(val name: String) : Html() + data class AttributeValue(val value: String) : Html() + data class HtmlValue(val value: String) : Html() } -data object DiscList : List(ListType.DISC) -data class DecimalList(val number: Char) : List(ListType.DECIMAL) -data class CheckBox(val checked: Boolean) : Token() -data object SquareBracketStart : Token() -data object SquareBracketEnd : Token() -data object ParenthesesStart : Token() -data object ParenthesesEnd : Token() -data class Url(var url: String) : Token() -data class Asterisk(var count: Int, var char: Char) : Token() -data object Exclamation : Token() -data class UrlTitle(val title: String) : Token() -data class InlineCodeBlock(val text: String) : Token() -data class CodeBlock(val text: String) : Token() -data class CodeBlockLanguage(val language: String, val filename: String) : Token() -data class Strike(val strike: String) : Token() - -abstract class Html() : Token() -data class StartTagStart(var tag: String, val void: Boolean) : Html() -data class EndTagStart(var tag: String) : Html() -data class TagEnd(var tag: String) : Html() -data class AttributeName(val name: String) : Html() -data class AttributeValue(val value: String) : Html() -data class HtmlValue(val value: String) : Html() \ No newline at end of file diff --git a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt index 2f752d9..241b925 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt @@ -1,5 +1,6 @@ package dev.usbharu.markdown +import dev.usbharu.markdown.Token.* import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals diff --git a/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt b/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt index 1d8ad7d..f90b0d7 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt @@ -1,5 +1,7 @@ package dev.usbharu.markdown +import dev.usbharu.markdown.AstNode.* +import dev.usbharu.markdown.Token.* import kotlin.test.Test import kotlin.test.assertEquals