Code Blocks
Commands, configuration files and source code go in an ordinary Markdown
fence. Hugo’s built-in Chroma highlighter colors it while the site builds, so
the page ships highlighted HTML and the browser runs no highlighter. Attributes
on the fence’s opening line add what readers of technical docs rely on: a
filename above the code, a copy button that copies exactly the right text, line
numbers, folding for long files, and links to individual lines. Fences for
diagrams and data, such as mermaid, echarts and filetree, are separate
components with their own pages.
Shortest form
Every fence gets the full frame and a copy button, even without attributes; a
block without a title simply has no title bar. The copy button sits at the top
right and appears when the pointer is over the block or focus is inside it, and
on touch screens it is always visible. The language is not displayed, but site
CSS can read it from the block’s data-td-language attribute.
The language tag is a Chroma lexer name, and Chroma has a lexer for most
languages and file formats. A diff fence, for example, shows added and
removed lines in their own colors, with no extra component:
Filename titles
title adds a title bar above the code, usually the file’s name or path, so
readers know where the snippet belongs. It also becomes the block’s accessible
name.
filename is an alias for title. If a fence has both, the build warns and
uses filename.
Line numbers, start line and highlighting
lineNos="inline" puts line numbers in the code column, and lineNoStart
sets the first number, which helps when the snippet is an excerpt of a longer
file. hl_lines highlights the lines you want the reader to look at. It counts
the lines inside the fence from 1, whatever lineNoStart says.
lineNos="table" puts the numbers in a column of their own, so a reader who
selects the code by hand does not pick up the numbers. The copy button leaves
them out in both modes.
Hugo’s other highlighting options, such as tabWidth for the number of
spaces a tab expands to, pass straight through to Chroma. style has no
effect by default: OINK highlights with CSS classes so that code colors follow
light and dark mode, and a Chroma style applies only after you switch to
inline styles with params.highlight_classes: false.
Wrapping long lines
Long lines scroll sideways by default. wrap=true wraps them on screen
instead, which suits long URLs and checksums; the source and the copied text
keep their original lines.
Wrapping does not combine with lineNos="table", because a wrapped line would
no longer line up with its number in the separate column. With both, the build
warns and turns wrapping off; use lineNos="inline" if you need both.
Folding long code
collapse=N shows the first N lines with a “Show all M lines” button under
them, where M is the block’s total line count, so a complete configuration file does not push the explanation off the
screen.
Folding only hides lines on screen. The page contains the whole file, so it is complete without JavaScript, for screen readers and on paper. A block no longer than N lines gets no button. Folding works together with wrapping and never cuts a wrapped line in half.
What gets copied
The copy button copies the whole block, except for terminal sessions. In a
console or shell-session fence it copies only the commands: it keeps the
lines that start with a prompt, strips the prompt, and drops the output. The
block below copies two commands, without $ and without the output lines:
Write copy="all" to copy prompts and output as well. Other lexers such as
bash or sh cannot tell a prompt from a command or its output, so on them
copy="command" warns and copies everything. When a command continues over
several lines, start each continuation line with its prompt, usually >;
otherwise it counts as output and is left out.
A session block with no prompt at all cannot be copied: the button shows an error icon and logs an error to the browser console, and the clipboard keeps its previous content rather than receiving the whole block.
copy=false removes the button, for a counter-example that nobody should
paste:
params.ui.code_copy: false removes the button from every block that does
not set copy itself (see Configuration). The
button is an icon only; it changes icon and announces a localized status on
success or failure. Copied text keeps indentation, blank lines and Unicode,
leaves out line numbers, and ends with exactly one newline.
One-line commands
A single command the reader is meant to paste can be a compact command strip
with its copy button in a column of its own: add {class="td-code-command"}
to a one-line fence. A block with more than one line, line numbers or
collapse warns and keeps the regular frame. See
Component presentation for examples.
Line links and stable IDs
To link to “line 4” from your text, give the fence an id and set
anchorLineNos=true. Each line number then becomes a link to #<id>-<line>:
Jump to line 4.
Write the id yourself whenever you plan to share a link. Without one, the
theme generates an ID that is unique on the page but depends on the fence’s
position, so adding a fence above it changes the ID and breaks old links. An
id must not contain spaces or control characters, and must not repeat
another ID on the page. An empty or invalid id warns and is replaced by a
generated one; a duplicate id warns but is not renamed.
Numbered examples
In a book or a long manual, num and caption turn a fence into a numbered
example that xref can refer to and that appears in
the book’s list of examples. You write the number yourself; the theme never
counts. The anchor defaults to eg-<num>.
See Example 4-1.
num and caption go together: a caption without a number, or a number
without a caption, warns and is dropped. A numbered example cannot also be a
tab, so num with tab warns and ignores tab. Numbering for figures, tables
and equations is covered in Publishing books.
A set of fences as tabs
Consecutive fences that each have a tab attribute become one tab set, which
suits the same command for different package managers. A group on the first
fence lets readers share a link to a tab, keeps groups with the same name in
sync, and remembers the reader’s choice.
Tabs has the group syntax, URL hashes, synchronization and tabs that hold prose.
Common errors
- A shortcode inside a fence still runs. A fence does not stop Hugo from
parsing shortcodes, so writing
{{< tabs >}}in a code block executes it. To show a shortcode as text, put a comment marker inside each delimiter:{{</* tabs */>}}, or{{%/* steps */%}}for the percent form. Every shortcode shown on this page is written that way. - Fences inside fences need a longer outer fence: four backticks outside, three inside, as in every “Source” block on this page. Add another backtick when the inner block contains fences of its own.
- Attributes go on the opening line, after the language. Tables and
images take their attribute line below the block, fences do not; a
{…}line under a fence shows up as text. - Fences in list items must be indented to the item’s text (three spaces
after
1.), or the fence ends the list.
Output
The copy and fold buttons appear only once the page script is running, and a page with no copy or fold control does not load that script at all. Print shows every block in full, without buttons and with its title bar, and long blocks may break across pages. The page’s Markdown output keeps each fence as written, attributes included.
Parameter reference
OINK’s attributes, inside the {…} after the language on the opening line:
tab, ,- Tab label, see Tabs; mutually exclusive with
num class, ,- Appended to the
.td-coderoot element;td-code-commandmakes a command strip
title, filename and label already give the block an accessible name and
role="group", so aria-label, aria-labelledby or role next to any of
them warns and is ignored; those three pass through only on a block with
neither a title nor a label. Unknown, unsafe and theme-reserved attributes
warn and are ignored, and the warning lists the allowed names.
The same line also takes Chroma’s options, which the theme hands to Hugo unchanged:
Limits
- Chroma is the only highlighter. There is no Shiki, Twoslash, in-browser
highlighting or runnable playground; for patches, use a
difffence. copy="command"works only with theconsoleandshell-sessionlexers; anywhere else it warns and copies everything.- A generated ID is not a permanent link. Write
idfor any line or block you link to from elsewhere. mermaid,math,chem,markmap,plantuml,echarts,infographic,checksums,filetree,galleryandmapfences are not code blocks. Each renders as its own component, without the code frame or copy button.
Related
- Tabs — the full rules for assembling adjacent fences
- Include — pull a real file from the repository in as a code block
- Publishing books — numbered examples, cross references, the list of examples
- Print — what long code looks like on paper