feat: add mdast spec to lettertrees and a placeholder renderer

This commit is contained in:
Infi 2023-01-15 20:35:25 +01:00
parent 9d897b6722
commit db4015f4f6
7 changed files with 200 additions and 27 deletions

View File

@ -25,6 +25,7 @@ import chat.revolt.api.RevoltAPI
import chat.revolt.api.internals.ULID
import chat.revolt.api.schemas.AutumnResource
import chat.revolt.components.generic.RemoteImage
import chat.revolt.lettertrees.Renderer
import chat.revolt.api.schemas.Message as MessageSchema
fun viewAttachmentInBrowser(ctx: android.content.Context, attachment: AutumnResource) {
@ -94,7 +95,7 @@ fun Message(
message.content?.let {
Text(
text = it
text = Renderer.annotateMarkdown(it),
)
}

View File

@ -28,13 +28,21 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.7.0'
implementation "androidx.compose.ui:ui:$compose_libraries_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

View File

@ -1,24 +0,0 @@
package chat.revolt.lettertrees
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("chat.revolt.lettertrees.test", appContext.packageName)
}
}

View File

@ -0,0 +1,26 @@
package chat.revolt.lettertrees
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
object Renderer {
fun annotateMarkdown(text: String): AnnotatedString {
// TODO this is all placeholder code
val boldRegex = Regex("\\*\\*(.*?)\\*\\*")
return buildAnnotatedString {
append(text)
boldRegex.findAll(text).forEach { match ->
addStyle(
style = SpanStyle(fontWeight = FontWeight.Bold),
start = match.groups[1]!!.range.first,
end = match.groups[1]!!.range.last + 1
)
}
toAnnotatedString()
}
}
}

View File

@ -0,0 +1,91 @@
package chat.revolt.lettertrees.ast
/*
* SPECS:
* - Unist, the universal syntax tree @ https://github.com/syntax-tree/unist
* - Mdast, the markdown syntax tree @ https://github.com/syntax-tree/mdast
*/
open class Literal(
/**
* The value of a node.
*/
open val value: String = ""
) : Node()
open class Parent(
/**
* List representing the children of a node.
*/
val children: List<Node> = emptyList()
) : Node()
open class Root(
override val type: String = "root"
) : Parent()
open class Paragraph(
override val type: String = "paragraph"
) : Parent()
open class Text(
override val type: String = "text",
override val value: String = ""
) : Literal()
open class Heading(
override val type: String = "heading",
val depth: Int = 1
) : Parent()
open class ThematicBreak(
override val type: String = "thematicBreak"
) : Node()
open class Blockquote(
override val type: String = "blockquote"
) : Parent()
open class MdList(
override val type: String = "list",
val ordered: Boolean = false,
val start: Int = 1,
val spread: Boolean = false
) : Parent()
open class ListItem(
override val type: String = "listItem",
val spread: Boolean = false
) : Parent()
open class Code(
override val type: String = "code",
val lang: String? = null,
val meta: String? = null
) : Literal()
open class Emphasis(
override val type: String = "emphasis"
) : Parent()
open class Strong(
override val type: String = "strong"
) : Parent()
open class Delete(
override val type: String = "delete"
) : Parent()
open class InlineCode(
override val type: String = "inlineCode"
) : Literal()
open class Break(
override val type: String = "break"
) : Node()
open class Link(
override val type: String = "link",
val title: String? = null,
val url: String = ""
) : Parent()

View File

@ -0,0 +1,67 @@
package chat.revolt.lettertrees.ast
/*
* SPECS:
* - Unist, the universal syntax tree @ https://github.com/syntax-tree/unist
* - Mdast, the markdown syntax tree @ https://github.com/syntax-tree/mdast
*/
class Point(
/**
* Line number in the document, starting at 1.
*/
val line: Int = 1,
/**
* Column on line in the document, starting at 1.
*/
val column: Int = 1,
/**
* Character offset in the document, starting at 0.
*/
val offset: Int = 0
)
class Position(
/**
* Place of the first character of the parsed source region.
*/
val start: Point = Point(),
/**
* Place of the first character after the parsed source region.
*/
val end: Point = Point(),
/**
* Start column at each index (plus start line) in the source region,
* for elements that span multiple lines.
*/
val indent: List<Int> = emptyList()
)
open class Node(
/**
* The variant of the node.
*/
open val type: String = "",
/**
* Information from the ecosystem.
*/
val data: Map<String, Any> = emptyMap(),
/**
* Location of the node in a source document.
* Must not be present if a node is generated.
*/
val position: Position = Position()
)
open class UnistParent(
/**
* List representing the children of a node.
*/
val children: List<Node> = emptyList()
) : Node()

View File

@ -0,0 +1,4 @@
package chat.revolt.lettertrees.parser
class Parser {
}