Skip to content

wingio/syntakts

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