fasthighlightfasthighlight

fasthighlight

Fast syntax highlighting written in Go, without regular expressions. Add it to goldmark to highlight code blocks in Markdown, or use the command fhl from a terminal, a script or a program in another language to turn Markdown into HTML, up to a basic static site.

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

Or download a single binary for Linux, macOS or Windows from the releases page. No Go needed.

With an AI agent, paste:

Install fhl from https://github.com/toshi-kuji/fasthighlight/releases/latest and run `fhl --help`.

More on agents

GitHub · Contents · ✨DEMO

Two ways to use it

Library fasthighlight Command fhl
For Go developers building an app Developers using other programming languages, AI agents, anyone with a terminal
How Call it from your program Run a command
Main job Build HTML conversion into your app Turn Markdown and code files into HTML
Good at Web servers, blogs, documentation services Static site builds, converting many files at once, automation

The library is a part you build into Go apps. fhl is a document tool you run as a command: from a terminal, a script or a program in another language. Both run the same engine.

The command

fhl README.md          # README.html and highlight.css next to README.md
fhl -o dist/ docs/     # every .md in docs/, into dist/

More about fhl. All flags and examples: fhl --help.

The library

import (
	"github.com/toshi-kuji/fasthighlight/markdown"

	_ "github.com/toshi-kuji/fasthighlight/lang/golang" // register the languages you use (here, Go)
	// _ "github.com/toshi-kuji/fasthighlight/lang/all"  // or all the languages at once
)

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

More about the library. API reference: pkg.go.dev.

What you can do with it

What it is not for

Questions

Is it a library or a command? Both. The core is the Go library, and fhl is a command built on it (it converts with the library's markdown.Extension). They are one module in one repository, with the same version.

Can I install only the command? Yes. go install github.com/toshi-kuji/fasthighlight/cmd/fhl@latest builds just fhl. Go downloads the whole module (with the library source) to build it. The binary on the releases page is a single file with the library built in; no Go needed.

Does the library bring the command's code into my program? No. go get github.com/toshi-kuji/fasthighlight downloads the whole module as one zip, but Go compiles only the packages you import. fhl is a main package, so it cannot be imported.

Do languages I don't use end up in my program? Only the languages you import. lang/all registers all of them; lang/golang and the others register one each. Choosing languages

Does it work with goldmark? Yes. Add markdown.Extension to your own goldmark: goldmark.New(goldmark.WithExtensions(extension.GFM, &markdown.Extension{})).

Then why goldmark at all? Don't they do the same job? No. goldmark parses Markdown and writes headings, tables and the rest as HTML. fasthighlight colors only the contents of code blocks; it has no Markdown parser of its own.

If I only highlight code (Highlight), is goldmark compiled in? No. The fasthighlight package and the language packages do not depend on goldmark; only the markdown package does.

Does fhl use goldmark? Yes. fhl sets up goldmark with GFM and markdown.Extension itself, so that --raw-html can change how raw HTML is written.

If I install fhl, can I use goldmark too? fhl has goldmark built in, so it needs nothing else to convert Markdown. It uses goldmark with GFM only; other goldmark extensions (such as footnotes) cannot be turned on from fhl. Installing fhl does not make goldmark available to your Go programs; for that, go get it in your module.

What happens to code blocks in languages it does not support? They are escaped, without colors. fhl --check lists where they are, except blocks marked as plain text (text and the like), which ask for no colors.

Speed

Each supported language has its own hand-written tokenizer, which reads the code character by character instead of trying a grammar of regular expressions at each position. This is a choice of speed over the reach of shared grammars; how it works and what it costs.

The numbers below come from our own measurements. Where the difference shows, and where it does not: When the speed matters. To try your own documents, run fhl: it prints the number of files and the time at the end, for example fhl: 128 files, 412 code blocks (3 uncolored) in 0.21s.

Converting a whole Markdown document

Implementation input.md (30 KB, 6 code blocks) multilang.md (40 KB, 35 code blocks: one in each of the 34 supported languages, and one plain text)
fasthighlight 0.90–0.96 ms 1.98–2.05 ms
Chroma (all languages) 5.59–5.61 ms 54.5–57.6 ms
Node: markdown-it + highlight.js 2.40–2.52 ms 23.4–23.6 ms
Node: markdown-it + Shiki 9.66–9.77 ms 83.7–85.0 ms

Highlighting code only

fasthighlight syntect giallo
22 snippets all three support (warm, total) about 0.85 ms about 24 ms about 39 ms
37 snippets fasthighlight and giallo support (warm, total) about 1.4 ms - about 63 ms
Initialization - about 0.7 ms about 45 ms
First use of the 21 languages all three support (total) about 1.5 ms about 96 ms about 510 ms
First use of the 34 languages fasthighlight and giallo support (total) about 2.9 ms - about 910 ms

Supported languages

fasthighlight supports 34 languages.

Language Names in code blocks File names
Bash bash, sh, ksh, zsh, shell *.sh, *.ksh, *.bash, *.ebuild, *.eclass, .env, .env.*, *.env, *.exheres-0, *.exlib, *.zsh, *.zshrc, .bashrc, bashrc, .bash_*, bash_*, zshrc, .zshrc, APKBUILD, PKGBUILD
Bash Session bash-session, console, shell-session *.sh-session
C c *.c, *.h, *.idc, *.x[bp]m
C# csharp, c# *.cs
C++ cpp, c++ *.cpp, *.hpp, *.c++, *.h++, *.cc, *.hh, *.cxx, *.hxx, *.C, *.H, *.cp, *.CPP, *.tpp
CSS css *.css
Diff diff, udiff *.diff, *.patch
Docker docker, dockerfile, containerfile Dockerfile, Dockerfile.*, *.Dockerfile, *.docker, Containerfile, Containerfile.*, *.Containerfile
Go go, golang *.go
GraphQL graphql, graphqls, gql *.graphql, *.graphqls
HTML html *.html, *.htm, *.xhtml, *.xslt
HTTP http
INI ini, cfg, dosini *.ini, *.cfg, *.inf, *.service, *.socket, *.container, *.network, *.build, *.pod, *.kube, *.volume, *.image, .gitconfig, .editorconfig, pylintrc, .pylintrc
JSON json, jsonl *.json, *.jsonl, *.jsonc, *.json5, *.avsc, .luaurc
Java java *.java
JavaScript js, javascript *.js, *.jsm, *.mjs, *.cjs
Kotlin kotlin *.kt, *.kts
Makefile make, makefile, mf, bsdmake *.mak, *.mk, Makefile, makefile, Makefile.*, GNUmakefile, BSDmakefile, Justfile, justfile, .justfile
PHP php, php3, php4, php5 *.php, *.php[345], *.inc
PowerShell powershell, posh, ps1, psm1, psd1, pwsh *.ps1, *.psm1, *.psd1
Protocol Buffer protobuf, proto *.proto
Python python, py, sage, python3, py3, starlark *.py, *.pyi, *.pyw, *.jy, *.sage, *.sc, SConstruct, SConscript, *.bzl, BUCK, BUILD, BUILD.bazel, WORKSPACE, WORKSPACE.bzlmod, WORKSPACE.bazel, MODULE.bazel, REPO.bazel, *.star, *.tac
R splus, s, r *.S, *.R, *.r, .Rhistory, .Rprofile, .Renviron
Ruby rb, ruby, duby *.rb, *.rbw, Rakefile, *.rake, *.gemspec, *.rbx, *.duby, Gemfile, *.gemfile, Vagrantfile, Appraisals, .pryrc, *.json.jbuilder
Rust rust, rs *.rs, *.rs.in
SCSS scss *.scss
SQL sql *.sql
Swift swift *.swift
TOML toml *.toml, Pipfile, poetry.lock, uv.lock
TypeScript ts, tsx, typescript *.ts, *.tsx, *.mts, *.cts
XML xml *.xml, *.xsl, *.rss, *.xslt, *.xsd, *.wsdl, *.wsf, *.svg, *.qrc, *.csproj, *.vcxproj, *.fsproj
YAML yaml *.yaml, *.yml
markdown md, mkd *.md, *.mkd, *.markdown
react jsx, react *.jsx, *.react

Names, aliases and file name patterns are the same as Chroma's. The highlighting follows Chroma 2.27.0, with a few deliberate fixes.

Code blocks marked as plain text (text, plaintext, plain or no-highlight, the names of Chroma's plain text lexer) are escaped without colors, and are not reported as uncolored, since that is what they ask for. Write ```text for output, logs and anything else that should stay plain.

Read more

This site is built from Markdown with fhl in CI: with each release, a GitHub Actions workflow runs the build script and publishes the result on GitHub Pages. Its code blocks are highlighted by fasthighlight. See the source on GitHub.