Documentation
¶
Overview ¶
Package mdcode colors the code blocks of a Markdown view with the tree-sitter grammars of jamb's code editor:
h := mdcode.Highlight(golang.Grammar, rust.Grammar, python.Grammar) markdown.View(readme).Code(h)
A code block is colored when the first word of its fence names one of the grammars: by the grammar's name (such as "go" or "python"), in any case, or by a common alias (see Highlight). 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 code editor's highlighting: the WebAssembly runtime (wazero) that runs the grammars, and the crypto packages that wazero imports. The grammars come from the packages of github.com/tyler-builds/jamb-grammars, each of which embeds its module in the app.
Each block is parsed once for each language and code, the first time a view shows it, and 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, parses nothing again. A block is plain for the moment its parse takes. The parses run on at most two goroutines of the highlighter at a time, each with one highlight session, which is closed as soon as its block is done; the goroutines end when there is nothing left to parse. A block that no view shows any more (the view went away, or its text changed) is dropped from the queue, or its parse is stopped. The first block of a language also compiles that grammar'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 parses code with tree-sitter grammars. 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(grammars ...highlight.Grammar) *Highlighter
Highlight returns a highlighter for the languages of the grammars. A fence names a grammar by its name, in any case, or by one of these aliases: golang for go; js, jsx, mjs, cjs and node for javascript; ts, mts and cts for typescript; py and python3 for python; sh, shell, zsh and console for bash; rs for rust; c++, cc, cxx, hpp and h++ for cpp; h for c; cs and c# for csharp; yml for yaml; md for markdown; htm and xhtml for html; and jsonc for json. An alias works only when its grammar is given.
func (*Highlighter) HighlightCode ¶
func (h *Highlighter) HighlightCode(ctx context.Context, lang, code string, ready func()) ([]markdown.CodeSpan, bool)
HighlightCode returns the colored ranges of code in the language lang, once they are parsed. The first time it is asked for a block, it queues the block and returns false, and it calls ready when the colors are there, unless ctx is done by then: when the contexts of all the requests for a block are done, the block is dropped. It returns false for good for a language that it has no grammar for. See markdown.CodeHighlighter.