Documentation
¶
Index ¶
- Constants
- Variables
- type BaseTransformer
- type CodeToANSIOptions
- type CodeToHTMLOptions
- type CodeToJSONOptions
- type CodeToPlainTextOptions
- type CodeToSVGOptions
- type CodeToTokensOptions
- type ColorDepth
- type Diagnostic
- type Element
- type Highlighter
- func (h *Highlighter) Close(ctx context.Context) error
- func (h *Highlighter) CodeToANSI(ctx context.Context, code string, opts ast.CodeToANSIOptions) (string, error)
- func (h *Highlighter) CodeToHTML(ctx context.Context, code string, opts ast.CodeToHTMLOptions) (string, error)
- func (h *Highlighter) CodeToHighlightedTokens(ctx context.Context, code string, opts ast.CodeToTokensOptions) (*ast.TokensResult, error)
- func (h *Highlighter) CodeToJSON(ctx context.Context, code string, opts ast.CodeToJSONOptions) ([]byte, error)
- func (h *Highlighter) CodeToPlainText(ctx context.Context, code string, opts ast.CodeToPlainTextOptions) (string, error)
- func (h *Highlighter) CodeToSVG(ctx context.Context, code string, opts ast.CodeToSVGOptions) (string, error)
- func (h *Highlighter) CodeToTokens(ctx context.Context, code string, opts ast.CodeToTokensOptions) (*ast.TokensResult, error)
- func (h *Highlighter) DetectLanguage(filename string) (string, bool)
- func (h *Highlighter) DetectLanguageByContent(firstLine string) (string, bool)
- func (h *Highlighter) GetThemeColors(themeName string) (ThemeColors, error)
- func (h *Highlighter) LoadLanguage(name string, data []byte) error
- func (h *Highlighter) LoadTheme(name string, data []byte) error
- func (h *Highlighter) LoadedLanguages() []string
- func (h *Highlighter) LoadedThemes() []string
- func (h *Highlighter) RegisterAlias(alias, target string)
- func (h *Highlighter) RegisterExtension(ext, lang string)
- type LineRange
- type Node
- type Option
- func WithAlias(alias, target string) Option
- func WithCompilationCacheDir(dir string) Option
- func WithDefaults(defaults CodeToHTMLOptions) Option
- func WithExtension(ext, lang string) Option
- func WithFS(fsys fs.FS) Option
- func WithGrammar(name string, data []byte) Option
- func WithGrammarFS(fsys fs.FS) Option
- func WithMaxLineLength(n int) Option
- func WithMinContrast(ratio float64) Option
- func WithPoolSize(n int) Option
- func WithRegexInterruption(enabled bool) Option
- func WithTheme(name string, data []byte) Option
- func WithThemeFS(fsys fs.FS) Option
- func WithTimeoutMs(ms int) Option
- type StyleClassMap
- type Text
- type ThemeColors
- type ThemedToken
- type TokenStyle
- type TokensResult
- type Transformer
Constants ¶
const ( ColorDepthTruecolor = ast.ColorDepthTruecolor ColorDepth256 = ast.ColorDepth256 ColorDepth16 = ast.ColorDepth16 ColorDepth8 = ast.ColorDepth8 )
Re-export ANSI color depth constants.
Variables ¶
var ( ErrLanguageNotFound = registry.ErrLanguageNotFound ErrThemeNotFound = registry.ErrThemeNotFound ErrGrammarCycle = grammar.ErrGrammarCycle ErrGrammarDepth = grammar.ErrGrammarDepth )
var ( Range = ast.Range Lines = ast.Lines NewStyleClassMap = ast.NewStyleClassMap )
Re-export ast functions.
Functions ¶
This section is empty.
Types ¶
type BaseTransformer ¶
type BaseTransformer = ast.BaseTransformer
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToANSIOptions ¶
type CodeToANSIOptions = ast.CodeToANSIOptions
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToHTMLOptions ¶
type CodeToHTMLOptions = ast.CodeToHTMLOptions
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToJSONOptions ¶
type CodeToJSONOptions = ast.CodeToJSONOptions
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToPlainTextOptions ¶
type CodeToPlainTextOptions = ast.CodeToPlainTextOptions
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToSVGOptions ¶
type CodeToSVGOptions = ast.CodeToSVGOptions
Re-export ast types so consumers can write nuri.Element, etc.
type CodeToTokensOptions ¶
type CodeToTokensOptions = ast.CodeToTokensOptions
Re-export ast types so consumers can write nuri.Element, etc.
type ColorDepth ¶
type ColorDepth = ast.ColorDepth
Re-export ast types so consumers can write nuri.Element, etc.
type Diagnostic ¶
type Diagnostic = ast.Diagnostic
Re-export ast types so consumers can write nuri.Element, etc.
type Highlighter ¶
type Highlighter struct {
// contains filtered or unexported fields
}
Highlighter is the main entry point for syntax highlighting. It owns WASM resources and must be closed when no longer needed.
func New ¶
func New(ctx context.Context, opts ...Option) (*Highlighter, error)
New compiles the WASM engine, instantiates a pool of WASM instances, and builds the grammar/theme registry. It does real work and may fail.
func (*Highlighter) Close ¶
func (h *Highlighter) Close(ctx context.Context) error
Close releases all WASM resources. Safe to call multiple times.
func (*Highlighter) CodeToANSI ¶
func (h *Highlighter) CodeToANSI( ctx context.Context, code string, opts ast.CodeToANSIOptions, ) (string, error)
CodeToANSI tokenizes source code, resolves colors from the theme, and renders the result as ANSI escape sequences for terminal display.
func (*Highlighter) CodeToHTML ¶
func (h *Highlighter) CodeToHTML( ctx context.Context, code string, opts ast.CodeToHTMLOptions, ) (string, error)
CodeToHTML tokenizes source code, resolves colors from the theme, and renders the result as an HTML string.
func (*Highlighter) CodeToHighlightedTokens ¶
func (h *Highlighter) CodeToHighlightedTokens( ctx context.Context, code string, opts ast.CodeToTokensOptions, ) (*ast.TokensResult, error)
CodeToHighlightedTokens tokenizes source code and resolves each token's color from the specified theme, returning colored tokens without HTML. This is equivalent to CodeToTokens (which already resolves colors).
func (*Highlighter) CodeToJSON ¶
func (h *Highlighter) CodeToJSON( ctx context.Context, code string, opts ast.CodeToJSONOptions, ) ([]byte, error)
CodeToJSON tokenizes source code, resolves colors from the theme, and returns the result as JSON bytes.
func (*Highlighter) CodeToPlainText ¶
func (h *Highlighter) CodeToPlainText( ctx context.Context, code string, opts ast.CodeToPlainTextOptions, ) (string, error)
CodeToPlainText tokenizes source code and returns the concatenated token content with no formatting.
func (*Highlighter) CodeToSVG ¶
func (h *Highlighter) CodeToSVG( ctx context.Context, code string, opts ast.CodeToSVGOptions, ) (string, error)
CodeToSVG tokenizes source code, resolves colors from the theme, and renders the result as a self-contained SVG document.
func (*Highlighter) CodeToTokens ¶
func (h *Highlighter) CodeToTokens( ctx context.Context, code string, opts ast.CodeToTokensOptions, ) (*ast.TokensResult, error)
CodeToTokens tokenizes source code and resolves each token's color from the specified theme. When opts.Themes is non-empty the code is tokenized once and every theme is resolved from the same token stream (opts.Theme is ignored); see CodeToTokensOptions.Themes.
func (*Highlighter) DetectLanguage ¶
func (h *Highlighter) DetectLanguage(filename string) (string, bool)
DetectLanguage resolves a language name from a filename or path. It checks exact filenames first (e.g. "Makefile"), then file extensions. Returns the language name usable with CodeToHTML/CodeToANSI/CodeToTokens, and true if a match was found.
func (*Highlighter) DetectLanguageByContent ¶
func (h *Highlighter) DetectLanguageByContent(firstLine string) (string, bool)
DetectLanguageByContent resolves a language from the first line of content. Useful for shebang detection (e.g. "#!/usr/bin/env python3").
func (*Highlighter) GetThemeColors ¶
func (h *Highlighter) GetThemeColors(themeName string) (ThemeColors, error)
GetThemeColors returns the UI colors for a loaded theme. Consumers building code block chrome (title bars, copy buttons, terminal frames) use these to style their wrappers consistently with the highlighted code.
func (*Highlighter) LoadLanguage ¶
func (h *Highlighter) LoadLanguage(name string, data []byte) error
LoadLanguage registers a grammar from raw JSON bytes.
func (*Highlighter) LoadTheme ¶
func (h *Highlighter) LoadTheme(name string, data []byte) error
LoadTheme registers a theme from raw JSON bytes.
func (*Highlighter) LoadedLanguages ¶
func (h *Highlighter) LoadedLanguages() []string
LoadedLanguages returns the names of all currently cached grammars.
func (*Highlighter) LoadedThemes ¶
func (h *Highlighter) LoadedThemes() []string
LoadedThemes returns the names of all currently cached themes.
func (*Highlighter) RegisterAlias ¶
func (h *Highlighter) RegisterAlias(alias, target string)
RegisterAlias maps a language alias to a canonical name.
func (*Highlighter) RegisterExtension ¶
func (h *Highlighter) RegisterExtension(ext, lang string)
RegisterExtension maps a file extension (without dot) to a language name, overriding any existing mapping for that extension.
type Option ¶
type Option func(*options)
Option configures a Highlighter.
func WithAlias ¶
WithAlias registers a language alias at construction time (e.g. "sh" -> "shellscript").
func WithCompilationCacheDir ¶
WithCompilationCacheDir enables an on-disk cache for the AOT-compiled onig.wasm module, cutting process cold-start time (useful for CLI/SSG invocations). The directory is created if needed and may be shared between processes; it only grows when the embedded WASM module changes.
func WithDefaults ¶
func WithDefaults(defaults CodeToHTMLOptions) Option
WithDefaults sets default CodeToHTMLOptions applied to every CodeToHTML call. Per-call options override these defaults (non-zero values win).
func WithExtension ¶
WithExtension maps a file extension (without dot) to a language name at construction time. Overrides any existing mapping for that extension.
func WithFS ¶
WithFS sets both the grammar and theme filesystems from a single fs.FS that contains "grammars/" and "themes/" subdirectories. This is the intended way to use the bundle packages.
func WithGrammar ¶
WithGrammar registers a custom grammar from JSON bytes at construction time.
func WithGrammarFS ¶
WithGrammarFS sets the filesystem for grammar JSON files.
func WithMaxLineLength ¶
WithMaxLineLength sets the byte-length threshold for per-line pre-filtering. Lines exceeding this are emitted as a single unstyled token with a "too_long" diagnostic. 0 means no limit (the default).
func WithMinContrast ¶
WithMinContrast sets the minimum WCAG 2.1 contrast ratio between syntax token foreground colors and the editor background. Tokens that fail the check are adjusted at theme load time (zero per-token cost). The default is 5.5 (WCAG AA enhanced). Set to 0 to disable and preserve raw theme colors.
func WithPoolSize ¶
WithPoolSize sets the maximum number of WASM instances in the pool. Defaults to runtime.NumCPU().
Instances are created lazily on demand, and the pool is LIFO: a borrow returns the most-recently-used instance. Each instance keeps a process-lifetime cache of compiled regex scanners for every grammar context it has tokenized (unbounded by design — Shiki/vscode-textmate never evict either), so a sequential workload keeps reusing one warm instance, and a workload with W concurrent tokenizations keeps a warm working set of W instances. Memory scales with instances actually created × distinct grammars used — sizing above your expected concurrency costs nothing until that concurrency materializes.
func WithRegexInterruption ¶
WithRegexInterruption toggles WASM-level regex interruption (default true). When enabled, regex execution compiled into the WASM runtime checks for context cancellation, so a runaway pattern can be stopped mid-search at the cost of some throughput. When disabled, only the Go-side soft per-line timeout (checked between scan positions) and Oniguruma's internal match stack limit bound a pathological regex.
func WithThemeFS ¶
WithThemeFS sets the filesystem for theme JSON files.
func WithTimeoutMs ¶
WithTimeoutMs sets the per-line soft timeout in milliseconds. Lines whose tokenization exceeds this are stopped early; partial tokens are preserved and a "timeout" diagnostic is recorded. 0 means no timeout (the default).
type StyleClassMap ¶
type StyleClassMap = ast.StyleClassMap
Re-export ast types so consumers can write nuri.Element, etc.
type ThemeColors ¶
type ThemeColors = ast.ThemeColors
Re-export ast types so consumers can write nuri.Element, etc.
type ThemedToken ¶
type ThemedToken = ast.ThemedToken
Re-export ast types so consumers can write nuri.Element, etc.
type TokenStyle ¶
type TokenStyle = ast.TokenStyle
Re-export ast types so consumers can write nuri.Element, etc.
type TokensResult ¶
type TokensResult = ast.TokensResult
Re-export ast types so consumers can write nuri.Element, etc.
type Transformer ¶
type Transformer = ast.Transformer
Re-export ast types so consumers can write nuri.Element, etc.
Directories
¶
| Path | Synopsis |
|---|---|
|
bundle
|
|
|
cmd
|
|
|
benchfmt
command
|
|
|
demo/ansi
command
|
|
|
demo/html
command
|
|
|
demo/json
command
|
|
|
demo/plaintext
command
|
|
|
demo/svg
command
|
|
|
internal
|
|
|
assetfs
Package assetfs provides a filesystem wrapper that transparently decompresses gzip stored assets.
|
Package assetfs provides a filesystem wrapper that transparently decompresses gzip stored assets. |
|
resources
|
|
|
tools
|
|
|
devtool
command
|
|