code

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package code provides syntax-highlighted source code widgets.

Highlighting is performed server-side via Chroma (Apache 2.0). The generated markup uses CSS classes that map to Material design color tokens, so themes switch with the rest of the page without re-rendering.

This package is not part of the default widget library because Chroma's lexer set adds several MB to the binary. Import it explicitly:

import "github.com/microbus-io/bespa/code"

See ATTRIBUTIONS.md for upstream licensing.

Index

Constants

This section is empty.

Variables

View Source
var Any = factory.Any

Function aliases

View Source
var Bytes = factory.Bytes
View Source
var HTML = factory.HTML
View Source
var HTMLUnsafe = factory.HTMLUnsafe
View Source
var Many = factory.Many
View Source
var Tag = factory.Tag
View Source
var Text = factory.Text

Functions

This section is empty.

Types

type BytesWidget

type BytesWidget = widget.BytesWidget

type CodeBlockWidget

type CodeBlockWidget struct {
	*widget.WidgetBase[*CodeBlockWidget]
	// contains filtered or unexported fields
}

CodeBlockWidget renders a block of source code with syntax highlighting.

func (*CodeBlockWidget) Draw

func (wgt *CodeBlockWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*CodeBlockWidget) WithFrame

func (wgt *CodeBlockWidget) WithFrame(on bool) *CodeBlockWidget

WithFrame toggles the surrounding border and tinted background. On by default. Pass false for a bare, transparent code block — useful when embedding inside a Card or other container that already provides the frame.

func (*CodeBlockWidget) WithLanguage

func (wgt *CodeBlockWidget) WithLanguage(name string) *CodeBlockWidget

WithLanguage sets the language used to tokenize the code. Use any name recognized by Chroma (e.g. "go", "javascript", "python", "rust", "sql", "json", "yaml", "html", "css", "bash"). The full list lives at https://github.com/alecthomas/chroma/tree/master/lexers/embedded. An empty string falls back to auto-detection.

func (*CodeBlockWidget) WithLineNumbers

func (wgt *CodeBlockWidget) WithLineNumbers(on bool) *CodeBlockWidget

WithLineNumbers toggles line-number rendering in the left gutter. Off by default.

func (*CodeBlockWidget) WithMaxHeight

func (wgt *CodeBlockWidget) WithMaxHeight(css string) *CodeBlockWidget

WithMaxHeight caps the visible height of the code block at the given CSS length, e.g. "300px", "50vh" or "calc(100vh - 50px)". Content beyond the cap scrolls vertically. Empty removes the cap.

func (*CodeBlockWidget) WithMaxRows

func (wgt *CodeBlockWidget) WithMaxRows(rows int) *CodeBlockWidget

WithMaxRows caps the visible height of the code block at the given number of lines. Content beyond that scrolls vertically. Set to 0 to remove the cap.

type CodeFactory

type CodeFactory struct{}

CodeFactory aggregates the widget constructors of this package.

func (CodeFactory) CodeBlock

func (f CodeFactory) CodeBlock(code string) *CodeBlockWidget

CodeBlock creates a new widget that renders a syntax-highlighted code block. Highlighting runs server-side on every Draw; for cheap, plain <pre><code> output without highlighting use basic.PlainCodeBlock. Without WithLanguage the lexer is auto-detected from the code; if detection fails, the code renders unhighlighted.

type InputWidget

type InputWidget = widget.InputWidget

type Widget

type Widget = widget.Widget

Type aliases

Jump to

Keyboard shortcuts

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