dx/code – Code blocks for documentation
The dx/code shortcode provides a consistent, documentation‑friendly way to display code examples.
It wraps Hugo’s built‑in syntax highlighting (Chroma) with a clean visual frame, optional copy‑to‑clipboard support, and a small API designed specifically for writing docs.
Why use dx/code?
- Consistent styling across docs pages
- Optional copy button for readers
- Optional line numbers (via Chroma)
- Can load examples from files, avoiding Hugo shortcode escaping
- Namespaced (
dx/) to avoid conflicts with themes like Blowfish
Usage
Parameters
lang(optional)
Language for syntax highlighting (default:text)copy(optional)true | false(default:true)
Show or hide the copy buttonlinenos(optional)true | false(default:false)
Enable line numberstitle(optional)
Small title shown above the code blockfile(optional)
Path to a file containing the code to display- First checks page bundle resources
- Falls back to
readFile(site‑relative)
Examples
Basic usage (inline code)
| |
hugo server -D
Disable copy button and enable line numbers
| |
| |
Recommended: load code from a file
When documenting Hugo shortcodes, loading code from a file avoids the
{{< ... >}} escape pattern entirely.
Example structure:
| |
Contents of photo-basic.txt:
| |
Render it with:
| |
{{< dx/photo src="test.jpg" caption_pos="below" >}}Caption{{< /dx/photo >}}Notes
- Highlight colors are controlled by Hugo’s Chroma configuration
(
markup.highlight.styleinhugo.toml) - With
noClasses = true, Chroma emits inline styles and requires no extra CSS - For light/dark theme switching, set
noClasses = falseand load Chroma CSS themes