Documentation
¶
Overview ¶
Package mdcode colors the code blocks of a Markdown view with the highlighter of jamb's code editor:
markdown.View(readme).Code(mdcode.Highlight())
A code block is colored when the first word of its fence names a language that the highlighter knows, in any case: one of the 76 languages of @pierre/highlights, by its name or a common alias, such as go, golang, python, py, ts, rust, rs, sh or yml (see highlight.Languages). Blocks in other languages, and blocks without a fence, stay plain.
The package is separate from markdown so that only the apps that import it link the highlighter: its WebAssembly module (about 170 KB), the runtime that runs it (wazero, except in a js build, which uses the JavaScript host's WebAssembly), and the crypto packages that wazero imports.
A block is lexed the first time a view shows it, while the view renders: a block of a few kilobytes takes some microseconds, and a megabyte about 10 ms. Its colors are kept for later renders, so a view that renders again, or a preview that shows a new text with the same blocks, lexes nothing again. The first block of all also compiles the highlighter's module, once per process (the code editor keeps the compiled code in a cache directory, so a later run is faster).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Highlighter ¶
type Highlighter struct {
// contains filtered or unexported fields
}
Highlighter is a markdown.CodeHighlighter that colors code with the code editor's highlighter. Make one with Highlight and give it to markdown.ViewElement.Code; one highlighter can serve every view of an app. It is safe for concurrent use.
func Highlight ¶
func Highlight() *Highlighter
Highlight returns a highlighter for every language that the code editor's highlighter knows.
func (*Highlighter) HighlightCode ¶
func (h *Highlighter) HighlightCode(_ context.Context, lang, code string, _ func()) ([]markdown.CodeSpan, bool)
HighlightCode returns the colored ranges of code in the language lang. It lexes the code at once, so it never calls ready; it returns false for a language that the highlighter does not know. See markdown.CodeHighlighter.