The codeblock shortcode provides a fully styled code display with:
- Syntax highlighting
- A language label
- A “Copy” button
It is used to show clean code examples in your blog.
Usage
GO
1
2
3
| {{< codeblock lang="javascript" >}}
console.log("Hello World");
{{< /codeblock >}}
|
Parameters
| Parameter | Required | Default | Description |
|---|
| lang | Yes | - | Programming language used for syntax highlighting |
| Inner content | Yes | - | The code to be displayed inside the block |
Table: Codeblock Parameters
Config
The Sans theme uses the monokai highlighter by default. You can change the code highlighter and other params in hugo.toml.
TOML
1
2
3
4
| [markup.highlight]
style = "monokai"
lineNos = true
lineNumbersInTable = true
|
Features
- Language label in the top bar
- Copy-to-clipboard button
- No extra spacing added
- Supports all Hugo-highlight languages
Examples
JavaScript Example
JAVASCRIPT
1
2
3
4
5
6
| {{< codeblock lang="javascript" >}}
function hello() {
console.log("Hello World");
}
hello();
{{< /codeblock >}}
|
Python Example
PYTHON
1
2
3
4
5
6
| {{< codeblock lang="python" >}}
def greet(name):
return f"Hello, {name}"
print(greet("World"))
{{< /codeblock >}}
|
HTML Example
HTML
1
2
3
4
5
| {{< codeblock lang="html" >}}
<div class="card">
<p>Hello!</p>
</div>
{{< /codeblock >}}
|
Showing Another Shortcode Inside Codeblock
GO
1
2
3
4
5
| {{< codeblock lang="go" >}}
{{< admonition type="info" >}}
This is inside an admonition block.
{{< /admonition >}}
{{< /codeblock >}}
|