The command fhl

fhl turns Markdown (GFM) and code files into HTML with highlighted code blocks. It writes body-only HTML and a highlight.css with the code colors, so that you or an AI agent can put the HTML into your own page and style it.

This page covers the basics. All flags and examples: run fhl --help.

Install

go install github.com/toshi-kuji/fasthighlight/cmd/fhl@latest

Or download an archive for your system (Linux, macOS or Windows; amd64 or arm64) from the releases page, and put fhl on your PATH. It is a single binary with no dependencies; Go is not needed.

With an AI agent

Paste this into your agent:

Install the command fhl: download the archive for this OS and CPU from
https://github.com/toshi-kuji/fasthighlight/releases/latest,
extract it, and put fhl on the PATH. Then run `fhl --version` and
`fhl --help`, and use the help to do what I ask next.

For how an agent reads the results, see AI agents.

All the supported languages are built in. fhl --version prints the version; fhl and the library share one version.

Use

fhl README.md                      # README.html and highlight.css next to README.md
fhl docs/                          # an .html next to each .md in docs/, and a highlight.css per folder
fhl -o dist/ docs/                 # write into dist/, keeping the folder layout
fhl --single-html README.md        # one standalone HTML file: the CSS in <head>, heading ids
fhl -o - README.md                 # body-only HTML to standard output
cat note.md | fhl -o note.html -   # read standard input
fhl main.go                        # a code file becomes one highlighted code block

Flags can go before, after or between the inputs (fhl docs/ --json works). Everything after -- is an input.

Inputs

Input Handling
.md, .markdown, .mkd converted as Markdown
other files converted as one code block. The language comes from --lang, or from the file name (*.go is Go); otherwise it is an error
folders every Markdown file inside, including subfolders. Names starting with . (such as .git) are skipped
- standard input, as Markdown (or as code with --lang). Needs -o

Outputs

The HTML file is named after the input, with .html (README.md becomes README.html).

-o Where
none next to each input
-o FILE that file (one input only)
-o DIR/ inside that folder, keeping the folder layout of the inputs. A value ending with /, or an existing folder, is a folder
-o - standard output (one input only). Body-only HTML, no highlight.css (get it with --print-css); with --single-html, a standalone HTML

The HTML

By default, fhl writes only what goes inside <body>: no <!DOCTYPE>, <head> or link to the CSS. Load highlight.css from your own page or template.

With --single-html, the file opens as it is. It has <!DOCTYPE html>, <meta charset="utf-8">, a <title> (the first heading, or the file name) and the CSS in a <style>, and nothing else. Headings get ids, as with --static-site (Heading ids), so that a table of contents can link to them.

Code blocks look like this:

<pre class="highlight" data-lang="go"><code class="language-go"><span class="nx">fmt</span>…

data-lang on <pre> and the language- class on <code> (the class CommonMark recommends, which tools that turn HTML back into Markdown read) hold the language name as written in the Markdown: ```golang gives language-golang. They are there for unsupported languages too, and missing when there is no language name. Elements get no style attributes, except the column alignment of GFM tables. Token classes are explained in Themes and CSS.

Raw HTML in Markdown

--raw-html chooses what happens to HTML tags written in the Markdown.

Value Result <b>bold</b> shows as
escape (default) written as visible text. HTML blocks are wrapped in a <pre> without a class, to keep their lines the text <b>bold</b>
omit dropped (<!-- raw HTML omitted -->). Text between inline tags stays bold (not bold)
unsafe written as is; <script> runs. Only for Markdown you trust bold

A static site

--static-site turns one folder of Markdown into a basic static site: pages that open in a browser as they are, in the same folder layout, ready to put on any web server or GitHub Pages. It is a starting point: the styles and the page layout are yours to add.

fhl --static-site -o dist/ docs/
fhl --static-site --theme light --html-lang ja -o dist/ docs/

What it does

What it does not do

Before you run it

Overwriting

If an output file already exists:

Collisions

Before converting, fhl compares every input and every file it would write.

Other behavior

Flags you will use most

Flag Meaning
-o PATH where to write (above)
--single-html a standalone HTML with the CSS in <head>, and ids on headings
--static-site turn one folder into a static site (A static site)
--html-lang LANG the lang of <html> with --static-site (default en)
--theme MODE:THEME code colors: MODE is light, dark or both, and THEME is one of fhl --list-themes (default both:default; see Themes and CSS)
--raw-html MODE escape (default), omit or unsafe
--lang NAME read code files and standard input as this language
--force overwrite without asking
--check write nothing; only report code blocks that are not colored
--strict exit with 2 if a code block is not colored
--json print the result as JSON on standard output
--list-langs, --list-themes print the languages or themes (JSON with --json)
--theme-selector MODE:SELECTOR with --theme both, also use the MODE colors (light or dark) under the CSS selector, over the device setting; give it once for each selector, such as dark:':root[data-theme="dark"]' (Themes and CSS)
--no-frame leave out the background and text color of pre.highlight in the CSS
--code-blocks write only the code blocks, colored, as one line of JSON each (Code blocks only)
--print-css write only the CSS of --theme to standard output, without converting anything (fhl --print-css > highlight.css)

A value follows its flag after a space or = (--theme light and --theme=light are the same). All flags and examples: fhl --help.

Results and exit status

At the end, fhl prints one summary line to standard error, after the code blocks that are not colored, one line each as file:line:. A conversion lists up to three for each reason (no language name, unsupported language) and counts the rest; --check lists them all. Blocks marked as plain text (text and the like) are not listed or counted, so write ```text for output, logs and anything else that should stay plain.

docs/intro.md:10: code block has no language name
docs/intro.md:31: code block has no language name
docs/usage.md:7: code block has no language name
fhl: 13 more code blocks have no language name (fhl --check lists all)
docs/setup.md:42: code block language "hs" is not supported (see fhl --list-langs)
fhl: 128 files, 412 code blocks (17 uncolored) in 0.21s

-q prints errors only. Errors say what happened and what to do next, in one line:

fhl: README.html already exists; use --force to overwrite it
Exit status Meaning
0 success (uncolored code blocks and files skipped with N are not errors)
1 an error (a bad flag, an unreadable input, an unwritable output, an overwrite without a terminal, a collision). Other files are still converted
2 with --strict, a code block is not colored

Code blocks only

If your build already converts Markdown (markdown-it, a Python library, your own code) and only needs the code colors, give fhl the same Markdown with --code-blocks. It writes each fenced code block, in order, as one line of JSON (JSON Lines) on standard output, and converts nothing else:

fhl --code-blocks page.md
{"file":"page.md","line":6,"lang":"go","open":"<pre class=\"highlight\" data-lang=\"go\"><code class=\"language-go\">","code":"<span class=\"nx\">x</span> <span class=\"o\">:=</span> …","close":"</code></pre>","colored":true}
{"file":"page.md","line":24,"lang":"cobol","open":"…","code":"…","close":"</code></pre>","colored":false,"reason":"unsupported"}
Field Meaning
file the input (- for standard input)
line the line of the opening fence, counted as your editor does (front matter included), to match the block with the one your converter found
lang the language name after the fence (empty if none)
open, code, close the HTML: open + code + close is the same block a conversion writes; code alone is what goes inside <code>
colored whether the code got colors
reason when not colored: no-language, unsupported or plain (text and the like)

AI agents

fhl is made to be run by AI agents. To use it in an agent's work: