Documentation
¶
Index ¶
- Constants
- Variables
- func AvailableThemes() []string
- func Highlight(code, lang string) string
- func HighlightTheme(code, lang, theme string) string
- func HighlightThemeLines(code, lang, theme, lines string) string
- func NormalizeLanguage(lang string) string
- func ParseLineRanges(spec string) [][2]int
- func ShippedLexer(lang string) (string, bool)
- func SimilarThemes(name string, n int) []string
- func ThemeCSS(t Theme) string
- type Bundle
- type Rule
- type Table
- type Theme
Constants ¶
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.
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 ¶
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 ¶
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
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 ¶
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 ¶
NormalizeLanguage normalizes common language aliases to chroma lexer names.
func ParseLineRanges ¶
ParseLineRanges parses a highlight spec such as "1,3-5" into 1-based inclusive ranges. Malformed parts are skipped.
func ShippedLexer ¶
ShippedLexer returns the canonical lexer name a language string selects, and whether the runtime tokenizer ships it.
func SimilarThemes ¶
SimilarThemes returns up to n theme names that look like name, for hints.
func ThemeCSS ¶
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 ¶
BuildBundle exports the named languages and everything they call into.
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 ¶
ExportLexer turns the named chroma lexer into a Table.
type Theme ¶
Theme names the chroma style used in each colour mode.
func ResolveTheme ¶
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.