znak

Highlight

A syntax highlighting library that uses Tree-sitter for incredibly quick parsing and highlighting.

Usage

You can use the highlight module from the github.com/noclaps/znak package:

import (
	"os"

	"github.com/noClaps/znak/highlight"
)

func main() {
	themeFile, err := os.ReadFile("path/to/theme.json")
	theme, err := highlight.NewTheme(themeFile)

	code = `fmt.Println("Hello world")`
	language = "go" // You can use any language supported by Highlight
	highlightedHtml, err := highlight.Highlight(code, language, theme)

	fmt.Println(highlightedHtml) // <pre class="ts-highlight" ... </pre>
}

You can use highlight.NewTheme() to create a new theme from a JSON string, or create a theme using highlight.Theme{}.

Themes

A theme is a JSON file with the following properties:

All of the color values are CSS colors, so you can use hex (#rrggbbaa), OKLCH (oklch(lightness# chroma hue / alpha)), etc.

You can look at theme.json for an example theme. You can also add:

{
  "$schema": "https://github.com/noClaps/znak/raw/refs/heads/main/theme-schema.json"
}

to the top of your theme file for completions and descriptions of the different properties.