mdcode

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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), 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.

Jump to

Keyboard shortcuts

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