views

package
v0.0.0-...-9268282 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package views parses fenced code blocks and connects them to custom view renderers.

Blocks preserves source order and normalizes CRLF line endings. Hash names an artifact by its language and source, while Path gives its relay URL. Figure emits accessible markup with the rendered image and an inline source fallback. Renderer maps each lowercased language to the first registered view that supports it.

Index

Constants

This section is empty.

Variables

View Source
var HashPattern = regexp.MustCompile(`^[0-9a-f]{64}$`)

HashPattern is the shape of a block hash in a path.

View Source
var NamePattern = regexp.MustCompile(`^[a-z0-9-]{1,32}$`)

NamePattern is the shape of a custom view name.

Functions

func ClosesFence

func ClosesFence(line, marker string) bool

ClosesFence reports whether a line closes the fence opened with marker: a run of at least as many of the same character and nothing else.

func Figure

func Figure(view, lang, source string) string

Figure is the markup for a block a custom view renders. The image remains transparent and follows the page color scheme, while the native details disclosure keeps the source available without requiring script.

func Hash

func Hash(lang, source string) string

Hash names a block: the SHA-256 of the language, a newline and the source, in hex. The same block in two texts has the same hash.

func OpenFence

func OpenFence(line string) (marker, lang string, ok bool)

OpenFence reports whether a line opens a fenced code block: three or more backticks or tildes, with the language as the first word after them. The marker returned is the fence run, so the closing line must use the same character.

func Path

func Path(view, hash, extension string) string

Path is the address an artifact is served at.

Types

type Block

type Block struct {
	Index  int    `json:"index"`
	Lang   string `json:"lang"`
	Source string `json:"source"`
}

Block is one fenced code block. Index counts every fenced block in the text from zero, whether or not it carries a language.

func Blocks

func Blocks(content string) []Block

Blocks lists the fenced code blocks of a text in order. The source is the lines between the fences joined by newlines, without a trailing newline.

func Matching

func Matching(blocks []Block, languages []string) []Block

Matching keeps the blocks whose language is one of the given languages.

type Renderer

type Renderer struct {
	Languages map[string]string
}

Renderer decides how a fenced block is shown: the view that renders the language, if any. Languages maps a lowercase language to a view name.

func NewRenderer

func NewRenderer(views []View) Renderer

NewRenderer indexes views by the languages they render. The first view that names a language renders it.

func (Renderer) Block

func (r Renderer) Block(lang, source string) (string, bool)

Block returns the figure markup for a block when a view renders its language.

type View

type View struct {
	Name      string   `json:"name"`
	Languages []string `json:"languages"`
}

View is the summary a renderer needs: the name and the languages.

Jump to

Keyboard shortcuts

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