Skip to content

wingio/syntakts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d2efac8 · Jul 20, 2024

History

40 Commits
Nov 16, 2023
Dec 3, 2023
Jul 20, 2024
Jul 19, 2024
Nov 10, 2023
Jul 20, 2024
Jul 20, 2024
Jul 20, 2024
Jul 20, 2024
Oct 10, 2023
Oct 10, 2023
Nov 21, 2023
Jul 19, 2024
Nov 10, 2023
Nov 10, 2023
Nov 10, 2023
Nov 21, 2023

Repository files navigation

Syntakts

Simple to use text parser and syntax highlighter for Kotlin Multiplatform

Maven Central GitHub Repo stars GitHub Sponsors


Setup

implementation("xyz.wingio.syntakts:syntakts-core:$syntaktsVersion")
// implementation("xyz.wingio.syntakts:syntakts-compose:$syntaktsVersion")
// implementation("xyz.wingio.syntakts:syntakts-compose-material3:$syntaktsVersion")

Use

Syntakts can be set up through a simple DSL:

val mySyntakts = syntakts<Unit> {
  rule("@([A-z])") { result, context ->
    append(result.groupValues[1]) {
      color = Color.Yellow
    }
  }
}

We also provide MarkdownSyntakts and BasicMarkdownSyntakts, which has some default markdown rules

Context

Syntakts allows you to pass any class as context, this can be used to pass additional information for rendering. If you don't need to use context you can set it to Unit

Example:

data class Context(
  val userMap = mapOf("1234" to "Wing")
)

val mySytankts = syntakts<Context> {
  rule("<@([0-9]+)>") { result, context ->
    val username = context.userMap[result.groupValues[1]] ?: "Unknown"
    append("@$username") {
      color = Color.Yellow
    }
  }
}

Displaying

Compose

Artifact: syntakts-compose

Syntakts uses AnnotatedStrings in order to display rendered text in Compose

Note

When creating a Syntakts instance in a composable we reccommend replacing syntakts {} with rememberSyntakts {}

Example:

@Composable
fun SomeScreen() {
  val syntakts = rememberSyntakts<Unit> { /* */ }

  Text(
    text = syntakts.rememberRendered("some input")
  )
}

Android

Artifact: syntakts-android

Syntakts uses SpannableStrings in order to display rendered text on Android

Example:

val syntakts = syntakts<Unit> { /* */ }

findViewById<TextView>(R.id.my_text_view).render("some input", syntakts)

Clickable

Syntakts for Compose includes a ClickableText component that is neccessary in order to handle clickable text. The syntakts-compose-material3 includes this component as well but adds support for Material 3 theming

Syntakts for Android requires that the TextView have its movementMethod set to our ClickableMovementMethod

Attribution

Syntakts was heavily inspired by SimpleAST, an unfortunately abandoned library that was once used in Discords android app