From 4ab8f8d12223fca0db20ec5afee92217533d058b Mon Sep 17 00:00:00 2001 From: tgiachi Date: Sun, 15 Nov 2026 10:38:56 +0100 Subject: [PATCH] fix: find code blocks with the Markdown parser and make the escape work everywhere The hand written fence scanner took a line that starts with a code span for a fence and missed fences in quotes and lists. The code blocks are now the ones goldmark finds. A backslash before a placeholder is turned into a marker that survives the Markdown and the list expansion, so it works in a code span and next to a block name, and two backslashes are a literal one. The em dash rule reports the line of the body, refuses padded entities and checks the header keys by name, the feed leaves protocol-relative links alone, base_url needs a host and a language file cannot set it. --- README.md | 2 +- internal/build/build_test.go | 17 ++++++ internal/build/feed.go | 7 ++- internal/build/feed_test.go | 10 +++ internal/build/pages.go | 11 +++- internal/content/pages.go | 4 +- internal/mdx/emdash.go | 16 +++-- internal/mdx/emdash_test.go | 15 ++++- internal/mdx/mdx_test.go | 64 +++++++++++++++++++ internal/mdx/substitute.go | 58 ++++++++++++++++++ internal/render/escape.go | 59 ++++++++++++++++++ internal/render/markdown.go | 108 --------------------------------- internal/render/render_test.go | 58 +++++++++--------- internal/site/site.go | 9 ++- internal/site/site_test.go | 12 +++- 15 files changed, 297 insertions(+), 153 deletions(-) create mode 100644 internal/mdx/substitute.go create mode 100644 internal/render/escape.go delete mode 100644 internal/render/markdown.go diff --git a/README.md b/README.md index 9ad28fa..06b5921 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Pages: `Title`, `ID` (the same in every language, it links the translations), `D ### Placeholders -`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. Fenced code blocks are left as written, so an article can show a placeholder there; elsewhere a backslash before the placeholder (`\{{name}}`) keeps it as written, and Markdown drops the backslash. Code spans are substituted, which is how a page shows a value in monospace. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists. +`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. Fenced code blocks (found the way CommonMark finds them, in lists and quotes too) are left as written, so an article can show a placeholder there. Anywhere else a backslash before the placeholder (`\{{name}}`) keeps it as written, in a code span too; two backslashes are one literal backslash followed by the value. Code spans are otherwise substituted, which is how a page shows a value in monospace. The id of a heading is not checked against ids that a template writes itself, such as the `news` of the latest news block. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists. ## Development diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 9d88ad0..1d6623d 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -407,6 +407,23 @@ func TestBuildPlaceholdersAndCode(t *testing.T) { mustContain(t, "people", got, "en", "escaped {{lang}}.", "{{lang}} {{nope}}", "And en.") } +// TestBuildEscapedPlaceholders checks the escape with block placeholders: an +// escaped one inside a sentence or alone in a paragraph is shown, not expanded +// and not an error, and a code span can show a placeholder as written. +func TestBuildEscapedPlaceholders(t *testing.T) { + files := baseFiles() + files["content/en/people.md"] = page("people", "People", "", "See \\{{news_list}} here.\n\n\\{{latest_news}}\n\nAnd `\\{{lang}}`.\n") + out, err := runBuild(t, files) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + got := readOut(t, out, "people.html") + mustContain(t, "people", got, "See {{news_list}} here.", "

{{latest_news}}

", "{{lang}}") + if strings.Contains(got, "