fhlfhl 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.
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.
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.
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.
| 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 |
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 |
highlight.css is written once per folder that gets HTML, and shared by the HTML in it. It is not written with --single-html or -o -. With --static-site, it is written once, at the top of the output (A static site).-o docs/dist/ docs/): it is skipped when the input folder is read, so earlier output is not converted or copied again.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 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 |
--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/
<!DOCTYPE html>, <html lang="en"> (change it with --html-lang), the charset, the viewport, a <title> (the first heading, or the file name) and a link to highlight.css.highlight.css is written once, at the top of the output, and every page links to it with a relative path, so the site works in any folder (such as https://user.github.io/project/).--single-html too), so you can link to sections: ## Results and exit status becomes results-and-exit-status. A second heading with the same text gets -1 added. Letters and digits of other scripts are kept, as on GitHub: ## インストール becomes インストール. A heading with no letters or digits at all becomes heading.[setup](guide.md#setup) becomes guide.html#setup. Links that start with a scheme (https:), / or # are left alone, and so are links to Markdown files that do not become pages: outside the folder (../README.md), in a folder whose name starts with a dot, or missing.. are skipped.<head> content. Add your styles to the highlight.css at the top of the output after converting: every page already links to it. Converting again writes it anew (after asking, or with --force), so add them again each time (this site's build script appends its style sheet this way). For other <head> content, edit the pages after converting, for example with a short script.--static-site takes exactly one folder. Files outside it are not copied; a link or image that points outside it will not work on the server.index.md, so that it becomes index.html..md files, or to .html files. Both work: links to .md files are rewritten.--raw-html unsafe.--theme, code blocks switch to dark colors when the viewer's device prefers dark (both:default). For a light-only site, add --theme light. See Themes and CSS.highlight.css is not copied, and fhl reports an error: for example guide.html next to guide.md, or your own highlight.css at the top of the folder.[Install](#install), or [インストール](#インストール) for a heading in Japanese. Punctuation is dropped from ids (使い方(基本) becomes 使い方基本).-o DIR/. Without -o, pages are written next to the Markdown files, highlight.css at the top of the folder, and nothing is copied. Converting again overwrites only after asking (or with --force); files whose content did not change are left alone.fhl --static-site --check docs/ writes nothing and only lists code blocks that will not be colored.If an output file already exists:
fhl asks [y/N/a] for each file: y overwrites, N (default) skips it, a overwrites the rest too.fhl stops with an error that mentions --force.--force overwrites without asking.fhl cannot read is an error, even with --force: fhl reads an existing file before overwriting it, and does not overwrite what it cannot see. Make it readable, or remove it.Before converting, fhl compares every input and every file it would write.
guide.md and guide.markdown both make guide.html), none of them is written, and each gets an error. The other inputs are converted.fhl -o README.md README.md), it is an error, even with --force.--- lines at the top of a document is skipped when its first line is a YAML key (title: …). Otherwise the --- is Markdown (a thematic break) and everything is converted, as it is without a closing ---.\n, \r\n and \r are all read; the output always uses \n.| 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.
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 |
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) |
- for standard input. Only fenced code blocks are written; indented code blocks are not.-q leaves it out), so standard output is only JSON. The exit status is as above: --strict exits with 2 if a block is not colored (plain blocks do not count).--print-css.Converter.CodeBlocks returns the same blocks without starting a program.reason can also be not-built-in (a supported language whose package is not imported) with the library; fhl includes every language, so it does not occur there.fhl is made to be run by AI agents. To use it in an agent's work:
Tell the agent to run fhl --help first. The help has every flag and example.
Have it add --json, and read the result from standard output instead of the messages on standard error:
fhl -o dist/ docs/ --json
{
"files": [{"input": "docs/intro.md", "output": "dist/intro.html", "blocks": 5, "uncolored": 1}],
"styles": ["dist/highlight.css"],
"skipped": [],
"uncolored": [{"file": "docs/intro.md", "line": 10, "lang": "", "reason": "no-language"}],
"errors": [],
"summary": {"files": 1, "blocks": 5, "uncolored": 1, "skipped": 0, "errors": 0, "seconds": 0.01}
}
reason is unsupported or no-language.
Without a terminal, fhl never waits for an answer: it stops with an error instead of overwriting. Add --force when overwriting is intended.
The output is body-only HTML and a highlight.css whose colors are CSS variables, each class with a comment that says what it is. An agent can read the CSS and style the page around it.
Use the exit status: 0, 1 or 2 (above).