Add copy-to-clipboard button to code blocks

Injects a hover-revealed copy button into every <pre><code> block
site-wide via a single layout partial, reading textContent so escaped
samples copy as literal text. Ships copy/check icons and matching Sass.

Passes icon markup to JS via <template> elements instead of Twig's
|escape('js'), which calls mb_ord() and fatals without mbstring — same
class of bug as the existing |slice/mb_substr gotcha, now documented in
AGENTS.md.

Closes the "Copy-to-clipboard button on code blocks" backlog item in
novaconium/ISSUES.md.
This commit is contained in:
code
2026-07-14 03:03:29 +00:00
parent fe5f5ee131
commit 672f997d8b
8 changed files with 190 additions and 41 deletions
+13
View File
@@ -238,3 +238,16 @@ site behavior for the next person.
for the full story). Truncate strings in PHP instead, guarded with
`function_exists('mb_substr')` falling back to `substr()`, and pass the
already-truncated value into the template.
- Same class of bug, different filter: don't use Twig's `|escape('js')` (or
the `'js'` arg to `|e`) either — it calls `Twig\Runtime\mb_ord()`
(`novaconium/vendor/twig/src/Extension/EscaperExtension.php`), which
hard-requires `mbstring` the same way `|slice` does, and fatals
identically without it. Hit for real when
`novaconium/pages/_layout/code-copy.twig` used it to pass SVG icon
markup into an inline `<script>` as a JS string literal. Fixed by not
needing string-escaped markup in JS at all: render the markup as plain
HTML into a `<template>` element (default autoescaping, no mbstring
dependency) and read it in JS via that template element's `.innerHTML`
getter instead. Prefer that pattern — or a `data-*` attribute if the
value is plain text, not markup — over `|escape('js')` any time a Twig
value needs to reach JS.