Use cases

When the speed matters, and what to run for common jobs. What each flag and function does is on The command fhl and The library.

When the speed matters

fasthighlight speeds up the highlighting of code; the rest of the Markdown is converted by goldmark. So the difference shows in two cases:

For anything else, such as converting a README now and then, any highlighter is fast enough. The numbers are under Speed on the top page.

With the command fhl

Convert a folder

Turn a folder of Markdown into HTML in one run, for example before publishing.

fhl -o dist/ docs/

dist/ gets the same folder layout as docs/, with a highlight.css in each folder. In CI, add --force: without a terminal, fhl stops instead of overwriting.

More: Outputs, Overwriting, fhl --help

Check in CI

Fail a CI job when a code block has no language name, or a language fhl does not color.

fhl --check --strict docs/

--check writes nothing. With --strict, the exit status is 2 if any block is not colored, and each one is listed with its file and line. Mark plain text as ```text: such blocks are not counted.

More: Results and exit status, fhl --help

A documentation site

Turn a folder of Markdown into a static site, and publish the output folder as it is (for example, on GitHub Pages).

fhl --static-site --raw-html unsafe --theme light -o dist/ docs/

Pages get a minimal <head>, headings get ids, links to .md files point to the pages, and images are copied. Write an index.md and your own links between pages; fhl adds no navigation and no styles beyond the code colors. To style the site, add your CSS to dist/highlight.css after converting. Drop --raw-html unsafe unless you write HTML in the Markdown yourself.

More: A static site (what to prepare and what to watch out for), fhl --help

Light or dark code colors

Pick the code colors for a site that is light only, dark only, or follows the viewer's device.

fhl --theme light docs/              # light colors only
fhl --theme dark:tokyo-night docs/   # Tokyo Night, dark colors only
fhl --theme both:github docs/        # GitHub, light or dark by the viewer's device

both follows the viewer's device setting. If your site has its own light and dark switch, add --theme-selector dark:':root[data-theme="dark"]' --theme-selector light:':root[data-theme="light"]' (or your own selectors): the switch then wins over the device setting. To ignore the device setting, see Themes and CSS.

More: Themes and CSS, fhl --list-themes

Static site builds

Use fhl from a static site build in Node, Python, Ruby or any other language, as an external command.

fhl -o build/html/ content/

Run it once for the whole folder rather than once per file: each start takes a few milliseconds. The output is body-only HTML, so put it into your own templates, and load highlight.css from them.

More: The HTML, fhl --help

Only the code colors

Keep your build's own Markdown converter (markdown-it, a Python library, your own code), and take only the colored code blocks from fhl: one line of JSON (JSON Lines) per block, in order.

fhl --code-blocks page.md

Each line has the block's line number and language, and its HTML in three parts (open, code, close), so you can put the colored code into the code block your converter wrote. Load highlight.css (fhl --print-css > highlight.css) for the colors.

More: Code blocks only, fhl --help

Single HTML

Make one Markdown file into one HTML file that opens as it is, to send to someone.

fhl --single-html README.md

The CSS is in <head>, and nothing else is needed. Headings get ids, so a table of contents in the Markdown ([Setup](#setup)) works.

More: The HTML, fhl --help

Code files

Turn a source file into colored HTML.

fhl main.go
fhl --lang go main.txt   # when the file name does not tell the language

The language comes from the file name (fhl --list-langs shows the patterns), or from --lang.

More: Inputs, fhl --help

Pipes

Use fhl between other commands.

cat note.md | fhl -o - -

Body-only HTML goes to standard output; no CSS is written.

More: Outputs, fhl --help

AI agents

Have an AI agent convert documents and read the result.

fhl -o dist/ docs/ --json

The result, including code blocks that were not colored and any errors, is JSON on standard output.

More: AI agents, fhl --help

With the library

Your own goldmark

Add highlighting to your goldmark configuration.

md := goldmark.New(goldmark.WithExtensions(extension.GFM, &markdown.Extension{}))

Import the languages too (lang/all, or each language).

More: Add to your own goldmark, pkg.go.dev

A web server

Convert Markdown on each request.

c := markdown.NewConverter()
err := c.Convert(src, w)

Large documents get their code blocks highlighted in parallel automatically. A Converter must not be used from several goroutines at the same time; use one per goroutine, or a goldmark.Markdown with markdown.Extension, which can be shared.

More: Parallel highlighting, pkg.go.dev

One piece of code

Color a code snippet without Markdown.

err := fasthighlight.Highlight(w, "go", code)

Without the markdown package, goldmark is not compiled into your program.

More: Highlight one piece of code, pkg.go.dev

Warn before converting

Find the code blocks that will not be colored, before converting.

c := markdown.NewConverter()
doc := c.Parse("guide.md", src)
r := markdown.Scan(doc)
// report r.NotBuiltIn, r.Unsupported and r.NoLanguage
err := c.Render(w, doc)

For a single language name, fasthighlight.LookupSupported tells whether it is supported and which package to import.

More: Find code blocks that will not be colored, pkg.go.dev

A smaller program

Keep the program small by importing only the languages you use.

import _ "github.com/toshi-kuji/fasthighlight/lang/golang"

lang/all imports all the languages.

More: Choose the languages