syntaxhighlight

package
v0.5.0-beta.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultLightTheme = "github"
	DefaultDarkTheme  = "krate-dark"
)

Default theme names. A plain string theme in the config applies to both modes; the defaults give a light page a light block and a dark page a dark one.

View Source
const DefaultTheme = DefaultLightTheme

DefaultTheme is the chroma theme name used when a caller names none. The class-based HTML does not depend on the theme (colours live in ThemeCSS), so this only picks the style handed to the formatter.

Variables

View Source
var ShippedLanguages = []string{
	"TypeScript", "JavaScript", "react", "JSON", "CSS", "HTML", "Bash", "Go",
	"Python", "Rust", "YAML", "markdown", "TOML", "XML", "C", "plaintext",
}

ShippedLanguages are the chroma lexers the runtime tokenizer can run with exact parity: every one is checked against chroma on real-world code (see TestParityCorpus). A language outside this list is still highlighted by chroma at build time; only highlighting that happens in the browser or at request time needs one of these.

Functions

func AvailableThemes

func AvailableThemes() []string

AvailableThemes returns a list of built-in chroma theme names.

func Highlight

func Highlight(code, lang string) string

Highlight returns HTML with CSS class annotations for the given code and language, using the default theme. Callers that know the configured code theme should use HighlightTheme so the emitted classes match the CSS.

func HighlightTheme added in v0.4.0

func HighlightTheme(code, lang, theme string) string

HighlightTheme is Highlight with an explicit chroma theme. In class mode the markup is the same for every theme; the colours come from ThemeCSS.

func HighlightThemeLines

func HighlightThemeLines(code, lang, theme, lines string) string

HighlightThemeLines is HighlightTheme with the given lines (a spec such as "1,3-5") wrapped in <span class="hl"> so a stylesheet can mark them.

func NormalizeLanguage

func NormalizeLanguage(lang string) string

NormalizeLanguage normalizes common language aliases to chroma lexer names.

func ParseLineRanges

func ParseLineRanges(spec string) [][2]int

ParseLineRanges parses a highlight spec such as "1,3-5" into 1-based inclusive ranges. Malformed parts are skipped.

func ShippedLexer

func ShippedLexer(lang string) (string, bool)

ShippedLexer returns the canonical lexer name a language string selects, and whether the runtime tokenizer ships it.

func SimilarThemes

func SimilarThemes(name string, n int) []string

SimilarThemes returns up to n theme names that look like name, for hints.

func ThemeCSS

func ThemeCSS(t Theme) string

ThemeCSS returns one stylesheet for both modes: the token rules read custom properties, and the light and dark blocks set them under the same selectors as the component tokens (system preference, then an explicit data-theme). The HTML a highlighter produces is the same for every theme, so one span per token serves both modes.

Types

type Bundle

type Bundle struct {
	Tables []*Table
	// Keys maps a lower-case language name to the lower-case name of its table.
	Keys map[string]string
	// Fences holds the names chroma would resolve to some lexer, for bundles
	// that include a lexer choosing sublexers by name (markdown). Every name
	// maps to the table that handles it, or "" when that lexer is not in the
	// bundle.
	Fences map[string]string
}

A Bundle is a set of exported lexers plus the language names that select them, ready to ship to the tokenizer.

func BuildBundle

func BuildBundle(langs []string) (*Bundle, error)

BuildBundle exports the named languages and everything they call into.

func (*Bundle) JSON

func (b *Bundle) JSON() []byte

JSON encodes the bundle: {"tables":[...],"keys":{...}}.

type Rule

type Rule struct {
	Pattern string
	Emit    interface{}
	Mut     []int
}

Rule is one lexer rule. An empty Pattern matches the empty string.

type Table

type Table struct {
	Name     string
	Types    []string
	Rules    [][]*Rule // by state index; state 0 is root
	States   []string  // state names, for diagnostics
	EnsureNL bool
	// Frontmatter marks the markdown lexer, which lexes a leading YAML block
	// with the YAML lexer first.
	Frontmatter bool
	// Deps are the names of other tables this lexer calls into.
	Deps []string
	// Fences marks lexers that pick a sublexer by name at run time
	// (UsingByGroup); the tokenizer needs a name resolver for them.
	Fences bool
}

Table is the exported form of a lexer.

func ExportLexer

func ExportLexer(name string) (*Table, error)

ExportLexer turns the named chroma lexer into a Table.

func (*Table) JSON

func (t *Table) JSON() []byte

JSON encodes the table compactly: {"n":name,"t":[classes],"s":[[rules]],...} where each rule is [pattern] or [pattern, emitter, steps].

type Theme

type Theme struct {
	Light string
	Dark  string
}

Theme names the chroma style used in each colour mode.

func ResolveTheme

func ResolveTheme(light, dark string) (t Theme, unknown []string)

ResolveTheme builds the Theme for the configured names. Empty names take the defaults. Names chroma does not know are returned in unknown and replaced by the default for their side, so the caller can report them and still build.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL