{% extends layout %} {% import '_layout/icons.twig' as icons %} {% block title %}Code Highlighting{% endblock %} {% block description %}How syntax highlighting works on this site, with worked examples in bash, HTML, CSS, YAML, Python, JavaScript, JSON, and INI/env.{% endblock %} {% block robots %}index, follow{% endblock %} {% block tags %}highlighting, reference{% endblock %} {% block canonical %}{{ request_path|default('/') }}{% endblock %} {% block og_type %}article{% endblock %} {% block og_title %}{{ block('title') }}{% endblock %} {% block og_description %}{{ block('description') }}{% endblock %} {% block og_url %}{{ block('canonical') }}{% endblock %} {% block twitter_card %}summary{% endblock %} {% block twitter_title %}{{ block('title') }}{% endblock %} {% block twitter_description %}{{ block('description') }}{% endblock %} {% block blog_content %}
Every <pre><code> block on this site gets colored automatically via vendored highlight.js — see {{ icons.book() }}Styling for the mechanism and {{ icons.book() }}Upgrading highlight.js for what's vendored. This post is a plain reference: how to write a code block, and one worked example in each language this site highlights.
Write a normal code block — nothing extra required, the language is auto-detected:
<pre><code>your code here</code></pre>
Auto-detection is restricted to the languages this site actually uses (see hljs.configure(...) in novaconium/pages/_layout/syntax-highlight.twig) so it doesn't misfire trying to match dozens of unrelated bundled languages against a short snippet. If a snippet is ambiguous, or ever gets detected as the wrong language, force it with an explicit language-<name> class instead of relying on auto-detection:
<pre><code class="language-yaml">your code here</code></pre>
A code block written in Twig template syntax ({{ '{%' }} ... {{ '%}' }}, {{ '{{' }} ... {{ '}}' }}) has no highlight.js grammar to match — mark those class="nohighlight" instead, the same way the two snippets above are marked (they're showing literal HTML as plain text, not being highlighted as HTML themselves). See {{ icons.book() }}the Twig Syntax Guide for Twig's own syntax, written the same way.
#!/bin/bash
for f in App/pages/blog/*/index.twig; do
echo "Post: $f"
done
<article class="post">
<h1>Hello, World!</h1>
<p>A short excerpt.</p>
</article>
.post-list li {
border-bottom: 1px solid var(--border-color);
padding: 0.75rem 0;
}
site:
name: Novaconium Website
theme: dark
tags:
- php
- twig
- highlighting
def excerpt(text, length=140):
return text[:length].rsplit(" ", 1)[0] + "..."
function toggleTheme() {
const html = document.documentElement;
const next = html.dataset.theme === "light" ? "dark" : "light";
html.setAttribute("data-theme", next);
}
{
"title": "Code Highlighting",
"tags": ["highlighting", "reference"],
"published": true
}
; App/config.php equivalent, .ini-style
[matomo]
url = https://matomo.example.com/
site_id = 1
YAML, JSON, and INI aren't part of highlight.js's core bundle the way PHP/Bash/CSS/Python/JavaScript/XML are — they're vendored as three separate files under public/vendor/highlightjs/languages/, loaded after the core bundle. See {{ icons.book() }}Upgrading highlight.js for how to add another language the same way.