Close the minor findings of the first review #7

Merged
squid merged 2 commits from fix/review-minors into main 2026-11-15 09:40:06 +00:00
15 changed files with 293 additions and 149 deletions
Showing only changes of commit 4ab8f8d122 - Show all commits

fix: find code blocks with the Markdown parser and make the escape work everywhere
All checks were successful
check / check (pull_request) Successful in 30s

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.
tgiachi 2026-11-15 10:38:56 +01:00

View file

@ -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

View file

@ -407,6 +407,23 @@ func TestBuildPlaceholdersAndCode(t *testing.T) {
mustContain(t, "people", got, "<code>en</code>", "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.", "<p>{{latest_news}}</p>", "<code>{{lang}}</code>")
if strings.Contains(got, "<ul>") {
t.Error("an escaped block placeholder was expanded")
}
}
// TestBuildAssetsCopied checks the assets listed in site.json.
func TestBuildAssetsCopied(t *testing.T) {
out, err := runBuild(t, baseFiles())

View file

@ -3,6 +3,7 @@
package build
import (
"regexp"
"strings"
"time"
@ -10,6 +11,10 @@ import (
"git.kmalloc.org/squid/motorino/internal/site"
)
// siteRelative matches a link that starts at the root of the site, such as
// href="/people.html", but not one that is relative to the protocol (href="//host/x").
var siteRelative = regexp.MustCompile(`\b(href|src)="/([^/])`)
// feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted
// newest first. Each item carries the full text with absolute links. The
// channel is described by the language l, and newsURL is the address of the
@ -21,7 +26,7 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit
var items strings.Builder
for _, p := range posts {
link := base + p.Path
text := strings.NewReplacer(`href="/`, `href="`+base+`/`, `src="/`, `src="`+base+`/`).Replace(p.HTML)
text := siteRelative.ReplaceAllString(p.HTML, `$1="`+base+`/$2`)
items.WriteString("<item>\n")
items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n")
items.WriteString("<link>" + link + "</link>\n")

View file

@ -49,6 +49,16 @@ func TestFeedItems(t *testing.T) {
}
}
// TestFeedLeavesProtocolRelativeLinks checks that "//host/x" is not given the site address.
func TestFeedLeavesProtocolRelativeLinks(t *testing.T) {
items := []renderedPost{{Post: testPost("a", "2026-11-14", "A", ""), HTML: `<img src="//cdn.example/x.png"> <a href="/p.html">p</a>`}}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
mustContain(t, "feed", got, `src="//cdn.example/x.png"`, `href="https://kmalloc.org/p.html"`)
if strings.Contains(got, "https://kmalloc.org//cdn") {
t.Error("a protocol-relative link got the site address")
}
}
// TestFeedWithoutPosts checks that an empty feed is still a valid channel.
func TestFeedWithoutPosts(t *testing.T) {
got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20)

View file

@ -81,7 +81,7 @@ func (b *builder) renderPage(p content.Page) error {
// markdown replaces the placeholders of a source, converts it to HTML and
// expands the generated lists. The name is used in error messages.
func (b *builder) markdown(name, source, lang string, ctx map[string]string) (string, []mdx.Heading, error) {
text, err := render.SubstituteMarkdown(name, source, ctx)
text, err := mdx.SubstituteMarkdown(name, source, ctx)
if err != nil {
return "", nil, err
}
@ -89,8 +89,13 @@ func (b *builder) markdown(name, source, lang string, ctx map[string]string) (st
if err != nil {
return "", nil, fmt.Errorf("%s: %w", name, err)
}
html, err = b.expandBlocks(name, html, lang, ctx)
return html, headings, err
if html, err = b.expandBlocks(name, html, lang, ctx); err != nil {
return "", nil, err
}
for i := range headings {
headings[i].Text = render.Unescape(headings[i].Text)
}
return render.Unescape(html), headings, nil
}
// codeElement matches a code element, where a placeholder is shown as written.

View file

@ -146,8 +146,8 @@ func loadPage(file, source, defaultLang, lang, rel string) (Page, error) {
// The body of the manifesto is the owner's text and is rewritten when the
// page is built; the keys of the header are not.
for _, key := range []string{"Title", "Description", "Note"} {
if err := mdx.RejectEmDash(source+" (key "+key+")", h.Get(key)); err != nil {
return Page{}, err
if mdx.HasEmDash(h.Get(key)) {
return Page{}, fmt.Errorf("%s: the key %s has an em dash, please remove it", source, key)
}
}
}

View file

@ -38,22 +38,30 @@ func NoEmDash(md string) (string, error) {
for _, p := range parentheticals {
md = strings.ReplaceAll(md, p.from, p.to)
}
if err := RejectEmDash("the body", md); err != nil {
return "", err
for i, line := range strings.Split(md, "\n") {
if HasEmDash(line) {
return "", fmt.Errorf("line %d of the body: no em dashes, please", i+1)
}
}
return md, nil
}
// emDashEntity matches the HTML entities of the em dash, which would put it
// on the page as well.
var emDashEntity = regexp.MustCompile(`(?i)&mdash;|&#8212;|&#x2014;`)
var emDashEntity = regexp.MustCompile(`(?i)&mdash;|&#0*8212;|&#x0*2014;`)
// HasEmDash reports whether text has an em dash, written as the character or as
// an HTML entity.
func HasEmDash(text string) bool {
return strings.Contains(text, emDash) || emDashEntity.MatchString(text)
}
// RejectEmDash returns an error naming the file and the line of the first em
// dash in text, written as the character or as an HTML entity, or nil when
// there is none.
func RejectEmDash(name, text string) error {
for i, line := range strings.Split(text, "\n") {
if strings.Contains(line, emDash) || emDashEntity.MatchString(line) {
if HasEmDash(line) {
return fmt.Errorf("%s:%d: no em dashes, please", name, i+1)
}
}

View file

@ -47,7 +47,7 @@ func TestNoEmDashLeftover(t *testing.T) {
// TestRejectEmDashEntities checks that the entities of the em dash are refused too.
func TestRejectEmDashEntities(t *testing.T) {
for _, in := range []string{"a &mdash; b", "a &MDASH; b", "a &#8212; b", "a &#x2014; b", "a &#X2014; b"} {
for _, in := range []string{"a &mdash; b", "a &MDASH; b", "a &#8212; b", "a &#x2014; b", "a &#X2014; b", "a &#08212; b", "a &#x02014; b"} {
if err := RejectEmDash("f.md", in); err == nil {
t.Errorf("RejectEmDash(%q) = nil, want an error", in)
}
@ -61,8 +61,17 @@ func TestRejectEmDashEntities(t *testing.T) {
// body, which is how the caller counts it.
func TestNoEmDashErrorSaysBody(t *testing.T) {
_, err := NoEmDash("fine\nsome words \u2014 and then more\n")
if err == nil || !strings.Contains(err.Error(), "the body:2") {
t.Errorf("NoEmDash() error = %v, want it to say the body:2", err)
if err == nil || !strings.Contains(err.Error(), "line 2 of the body") {
t.Errorf("NoEmDash() error = %v, want it to say line 2 of the body", err)
}
}
// TestHasEmDash checks the test that the header keys use.
func TestHasEmDash(t *testing.T) {
for in, want := range map[string]bool{"plain": false, "a \u2014 b": true, "a &mdash; b": true, "a &ndash; b": false, "": false} {
if got := HasEmDash(in); got != want {
t.Errorf("HasEmDash(%q) = %v, want %v", in, got, want)
}
}
}

View file

@ -5,6 +5,8 @@ package mdx
import (
"strings"
"testing"
"git.kmalloc.org/squid/motorino/internal/render"
)
// TestRenderParagraphAndList checks the basic blocks and inline markup.
@ -177,6 +179,68 @@ func TestRenderBadExplicitIDCannotInject(t *testing.T) {
}
}
// TestSubstituteMarkdownFences checks that placeholders in fenced code blocks
// stay as written, wherever CommonMark finds the block, and that an unknown one
// there is no error.
func TestSubstituteMarkdownFences(t *testing.T) {
ctx := map[string]string{"lang": "en"}
tests := []struct{ name, src, want string }{
{"backtick fence", "A {{lang}}\n\n```\n{{lang}} {{nope}}\n```\n\nB {{lang}}\n", "A en\n\n```\n{{lang}} {{nope}}\n```\n\nB en\n"},
{"tilde fence with info", "~~~go\n{{lang}}\n~~~\n{{lang}}\n", "~~~go\n{{lang}}\n~~~\nen\n"},
{"code span is substituted", "Value `{{lang}}`.\n", "Value `en`.\n"},
{"fence in a quote", "> ```\n> {{nope}}\n> ```\n\n{{lang}}\n", "> ```\n> {{nope}}\n> ```\n\nen\n"},
{"fence in a list item", "1. item\n\n ```\n {{nope}}\n ```\n\n{{lang}}\n", "1. item\n\n ```\n {{nope}}\n ```\n\nen\n"},
{"code span at the start of a line is not a fence", "```x``` and {{lang}}\n\nthen {{lang}}\n", "```x``` and en\n\nthen en\n"},
{"unclosed fence runs to the end", "{{lang}}\n\n```\n{{nope}}\n{{lang}}\n", "en\n\n```\n{{nope}}\n{{lang}}\n"},
{"a closer indented four spaces does not close", "```\n ```\n{{nope}}\n```\n{{lang}}\n", "```\n ```\n{{nope}}\n```\nen\n"},
{"a closer may be longer", "```\n{{nope}}\n`````\n{{lang}}\n", "```\n{{nope}}\n`````\nen\n"},
{"CRLF line endings", "{{lang}}\r\n\r\n```\r\n{{nope}}\r\n```\r\n{{lang}}\r\n", "en\r\n\r\n```\r\n{{nope}}\r\n```\r\nen\r\n"},
{"empty fence", "```\n```\n{{lang}}\n", "```\n```\nen\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SubstituteMarkdown("page.md", tt.src, ctx)
if err != nil {
t.Fatalf("SubstituteMarkdown() error = %v", err)
}
if got != tt.want {
t.Errorf("SubstituteMarkdown() = %q, want %q", got, tt.want)
}
})
}
}
// TestSubstituteMarkdownUnknownOutsideFence checks that an unknown placeholder
// outside a fence still stops the build and names the file.
func TestSubstituteMarkdownUnknownOutsideFence(t *testing.T) {
_, err := SubstituteMarkdown("news/x.md", "```\n{{a}}\n```\n\nbad {{nope}}\n", map[string]string{})
if err == nil || !strings.Contains(err.Error(), "news/x.md") || !strings.Contains(err.Error(), "nope") {
t.Errorf("SubstituteMarkdown() error = %v, want it to name news/x.md and nope", err)
}
}
// TestSubstituteMarkdownEscapeEverywhere checks that a backslash keeps a
// placeholder as written in text, in a code span and next to a block name, and
// that the written form comes back through render.Unescape.
func TestSubstituteMarkdownEscapeEverywhere(t *testing.T) {
src := "Text \\{{lang}} and `\\{{lang}}` and \\{{news_list}}.\n"
got, err := SubstituteMarkdown("page.md", src, map[string]string{"lang": "en"})
if err != nil {
t.Fatal(err)
}
html, _, err := Render(got)
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, "{{") {
t.Errorf("html %q still has braces before Unescape", html)
}
want := "<p>Text {{lang}} and <code>{{lang}}</code> and {{news_list}}.</p>\n"
if out := render.Unescape(html); out != want {
t.Errorf("Unescape(html) = %q, want %q", out, want)
}
}
// TestRenderHeadingTextIsPlain checks that the heading text drops the markup.
func TestRenderHeadingTextIsPlain(t *testing.T) {
_, headings, err := Render("## Free *your* mind\n")

View file

@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package mdx
import (
"strings"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"git.kmalloc.org/squid/motorino/internal/render"
)
// SubstituteMarkdown replaces the {{name}} placeholders of Markdown source like
// render.SubstituteEscaped does, but leaves the content of fenced code blocks
// as written, so an article can show a placeholder there. The blocks are found
// by the Markdown parser, so they are the ones CommonMark sees, in lists and in
// quotes too. Code spans are substituted: the pages use them to show a value in
// monospace. The result still has the markers of escaped placeholders; turn
// them back with render.Unescape after the Markdown is rendered.
func SubstituteMarkdown(name, md string, ctx map[string]string) (string, error) {
src := []byte(md)
doc := engine.Parser().Parse(text.NewReader(src))
type span struct{ start, stop int }
var fenced []span
_ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if block, ok := n.(*ast.FencedCodeBlock); ok && entering {
lines := block.Lines()
for i := 0; i < lines.Len(); i++ {
segment := lines.At(i)
fenced = append(fenced, span{segment.Start, segment.Stop})
}
}
return ast.WalkContinue, nil
})
var out strings.Builder
pos := 0
for _, s := range fenced {
if s.start < pos {
continue
}
replaced, err := render.SubstituteEscaped(name, md[pos:s.start], ctx)
if err != nil {
return "", err
}
out.WriteString(replaced)
out.WriteString(md[s.start:s.stop])
pos = s.stop
}
replaced, err := render.SubstituteEscaped(name, md[pos:], ctx)
if err != nil {
return "", err
}
out.WriteString(replaced)
return out.String(), nil
}

59
internal/render/escape.go Normal file
View file

@ -0,0 +1,59 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package render
import (
"fmt"
"regexp"
"strings"
)
// The markers that stand for an escaped placeholder while the Markdown is
// rendered. They are private use characters, which neither Markdown nor the
// templates treat in any special way.
const (
escapeOpen = ""
escapeClose = ""
)
// escaped matches a marker with the name of the placeholder it stands for.
var escaped = regexp.MustCompile(escapeOpen + `([a-z0-9_]+)` + escapeClose)
// SubstituteEscaped replaces the {{name}} placeholders of text like Substitute
// does, except those with a backslash right before them (an odd number of
// backslashes: two make a literal backslash). The backslash is dropped and the
// placeholder is replaced by a marker that Unescape turns back into the written
// placeholder once the Markdown has been rendered. An unknown placeholder is an
// error that names the file; an escaped one is not looked up.
func SubstituteEscaped(name, text string, ctx map[string]string) (string, error) {
var out strings.Builder
last := 0
for _, loc := range placeholder.FindAllStringSubmatchIndex(text, -1) {
start, end := loc[0], loc[1]
key := text[loc[2]:loc[3]]
slashes := 0
for i := start - 1; i >= last && text[i] == '\\'; i-- {
slashes++
}
if slashes%2 == 1 {
out.WriteString(text[last : start-1])
out.WriteString(escapeOpen + key + escapeClose)
} else {
value, ok := ctx[key]
if !ok {
return "", fmt.Errorf("%s: unknown placeholder {{%s}}", name, key)
}
out.WriteString(text[last:start])
out.WriteString(value)
}
last = end
}
out.WriteString(text[last:])
return out.String(), nil
}
// Unescape turns the markers left by SubstituteEscaped back into the
// placeholders as they were written.
func Unescape(s string) string {
return escaped.ReplaceAllString(s, "{{$1}}")
}

View file

@ -1,108 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package render
import (
"fmt"
"strings"
)
// SubstituteMarkdown replaces the {{name}} placeholders of Markdown text like
// Substitute does, with two exceptions that let an article show a placeholder
// as it is written: fenced code blocks are left untouched (an unknown
// placeholder in one is not an error), and a placeholder with a backslash in
// front of it is kept, backslash included, which Markdown then renders as the
// plain placeholder. Code spans are substituted: the pages use them to show a
// value in monospace. An unknown placeholder elsewhere is an error that names
// the file.
func SubstituteMarkdown(name, md string, ctx map[string]string) (string, error) {
var out strings.Builder
var text strings.Builder // the lines outside fences, waiting to be substituted
flush := func() error {
if text.Len() == 0 {
return nil
}
replaced, err := substituteUnescaped(name, text.String(), ctx)
if err != nil {
return err
}
out.WriteString(replaced)
text.Reset()
return nil
}
lines := strings.SplitAfter(md, "\n")
for i := 0; i < len(lines); i++ {
marker, length := fenceStart(lines[i])
if length == 0 {
text.WriteString(lines[i])
continue
}
if err := flush(); err != nil {
return "", err
}
out.WriteString(lines[i])
for i++; i < len(lines); i++ {
out.WriteString(lines[i])
if fenceEnds(lines[i], marker, length) {
break
}
}
}
if err := flush(); err != nil {
return "", err
}
return out.String(), nil
}
// fenceStart returns the fence character and its length when the line opens a
// fenced code block (three or more backticks or tildes, indented by at most
// three spaces), and a zero length otherwise.
func fenceStart(line string) (marker byte, length int) {
rest := strings.TrimLeft(line, " ")
if len(line)-len(rest) > 3 || rest == "" || (rest[0] != '`' && rest[0] != '~') {
return 0, 0
}
marker = rest[0]
for length < len(rest) && rest[length] == marker {
length++
}
if length < 3 {
return 0, 0
}
return marker, length
}
// fenceEnds reports whether the line closes a fence opened with length
// characters of marker: at least as many, and nothing else on the line.
func fenceEnds(line string, marker byte, length int) bool {
rest := strings.TrimSpace(line)
if len(rest) < length {
return false
}
return strings.Trim(rest, string(marker)) == ""
}
// substituteUnescaped is Substitute that leaves alone the placeholders that
// have a backslash right before them.
func substituteUnescaped(name, text string, ctx map[string]string) (string, error) {
var out strings.Builder
last := 0
for _, loc := range placeholder.FindAllStringSubmatchIndex(text, -1) {
start, end := loc[0], loc[1]
out.WriteString(text[last:start])
last = end
if start > 0 && text[start-1] == '\\' {
out.WriteString(text[start:end])
continue
}
key := text[loc[2]:loc[3]]
value, ok := ctx[key]
if !ok {
return "", fmt.Errorf("%s: unknown placeholder {{%s}}", name, key)
}
out.WriteString(value)
}
out.WriteString(text[last:])
return out.String(), nil
}

View file

@ -59,40 +59,42 @@ func TestSubstituteValueIsNotRescanned(t *testing.T) {
}
}
// TestSubstituteMarkdownFences checks that placeholders in fenced code blocks
// stay as written (an unknown one is no error there), while code spans are
// substituted: the pages use them to show a value in monospace.
func TestSubstituteMarkdownFences(t *testing.T) {
// TestSubstituteEscapedKeepsEscapes checks that a backslash before a placeholder
// keeps it as written: it becomes a marker that Unescape turns back into the
// placeholder after the Markdown is rendered. An unknown escaped one is no error.
func TestSubstituteEscapedKeepsEscapes(t *testing.T) {
got, err := SubstituteEscaped("page.md", "Write \\{{lang}} for {{lang}}, and \\{{nope}}.", map[string]string{"lang": "en"})
if err != nil {
t.Fatalf("SubstituteEscaped() error = %v", err)
}
if strings.Contains(got, "{{") || strings.Contains(got, "\\") {
t.Errorf("SubstituteEscaped() = %q, want no braces and no backslash left", got)
}
if want := "Write {{lang}} for en, and {{nope}}."; Unescape(got) != want {
t.Errorf("Unescape(%q) = %q, want %q", got, Unescape(got), want)
}
}
// TestSubstituteEscapedBackslashParity checks that an escaped backslash does not
// escape the placeholder: two backslashes are one literal backslash.
func TestSubstituteEscapedBackslashParity(t *testing.T) {
ctx := map[string]string{"lang": "en"}
src := "Outside {{lang}} and `{{lang}}`.\n\n```\n{{lang}} {{nope}}\n```\n\n~~~go\n{{lang}}\n~~~\n\nEnd {{lang}}.\n"
got, err := SubstituteMarkdown("page.md", src, ctx)
if err != nil {
t.Fatalf("SubstituteMarkdown() error = %v", err)
even, err := SubstituteEscaped("f", "\\\\{{lang}}", ctx)
if err != nil || even != "\\\\en" {
t.Errorf("two backslashes: got %q, %v, want the value after both backslashes", even, err)
}
want := "Outside en and `en`.\n\n```\n{{lang}} {{nope}}\n```\n\n~~~go\n{{lang}}\n~~~\n\nEnd en.\n"
if got != want {
t.Errorf("SubstituteMarkdown() = %q, want %q", got, want)
odd, err := SubstituteEscaped("f", "\\\\\\{{lang}}", ctx)
if err != nil || Unescape(odd) != "\\\\{{lang}}" {
t.Errorf("three backslashes: got %q, %v, want two backslashes then the placeholder", Unescape(odd), err)
}
}
// TestSubstituteMarkdownEscape checks that a backslash before a placeholder
// keeps it as written: Markdown then drops the backslash.
func TestSubstituteMarkdownEscape(t *testing.T) {
got, err := SubstituteMarkdown("page.md", "Write \\{{lang}} for {{lang}}, and \\{{nope}} is fine.\n", map[string]string{"lang": "en"})
if err != nil {
t.Fatalf("SubstituteMarkdown() error = %v", err)
}
if want := "Write \\{{lang}} for en, and \\{{nope}} is fine.\n"; got != want {
t.Errorf("SubstituteMarkdown() = %q, want %q", got, want)
}
}
// TestSubstituteMarkdownUnknownOutsideCode checks that an unknown placeholder
// outside a fence still stops the build and names the file.
func TestSubstituteMarkdownUnknownOutsideCode(t *testing.T) {
_, err := SubstituteMarkdown("news/x.md", "ok {{a}}\n\nbad {{nope}}\n", map[string]string{"a": "1"})
// TestSubstituteEscapedUnknown checks that an unknown placeholder still stops the
// build and names the file.
func TestSubstituteEscapedUnknown(t *testing.T) {
_, err := SubstituteEscaped("news/x.md", "ok {{a}} bad {{nope}}", map[string]string{"a": "1"})
if err == nil || !strings.Contains(err.Error(), "news/x.md") || !strings.Contains(err.Error(), "nope") {
t.Errorf("SubstituteMarkdown() error = %v, want it to name news/x.md and nope", err)
t.Errorf("SubstituteEscaped() error = %v, want it to name news/x.md and nope", err)
}
}

View file

@ -7,6 +7,7 @@ package site
import (
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
@ -82,8 +83,9 @@ func Load(dir string) (Config, map[string]Lang, error) {
// checkBaseURL returns an error unless the address of the site is absolute and
// has no trailing slash: page addresses and the feed are built by adding to it.
func checkBaseURL(base string) error {
if !(strings.HasPrefix(base, "https://") || strings.HasPrefix(base, "http://")) || strings.HasSuffix(base, "/") {
return fmt.Errorf("site.json: base_url must be an absolute address without a trailing slash, such as https://kmalloc.org")
u, err := url.Parse(base)
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || strings.HasSuffix(base, "/") {
return fmt.Errorf("site.json: base_url must be an absolute address with a host and no trailing slash, such as https://kmalloc.org")
}
return nil
}
@ -133,6 +135,9 @@ func loadLang(dir, code string) (Lang, error) {
if lang.Strings["lang_name"] == "" {
return Lang{}, fmt.Errorf("%s: missing lang_name", name)
}
if _, ok := lang.Strings["base_url"]; ok {
return Lang{}, fmt.Errorf("%s: base_url is set in site.json only", name)
}
if _, ok := raw["nav"]; !ok {
return Lang{}, fmt.Errorf("%s: missing nav", name)
}

View file

@ -155,7 +155,7 @@ func TestLoadRejectsBadLanguageCodes(t *testing.T) {
// TestLoadNeedsBaseURL checks that the address of the site is required and absolute,
// because the feed and the page addresses are built from it.
func TestLoadNeedsBaseURL(t *testing.T) {
for _, site := range []string{`{"languages": ["en", "it"]}`, `{"base_url": "kmalloc.org", "languages": ["en", "it"]}`, `{"base_url": "https://kmalloc.org/", "languages": ["en", "it"]}`} {
for _, site := range []string{`{"languages": ["en", "it"]}`, `{"base_url": "kmalloc.org", "languages": ["en", "it"]}`, `{"base_url": "https://kmalloc.org/", "languages": ["en", "it"]}`, `{"base_url": "https://", "languages": ["en", "it"]}`, `{"base_url": "https:///x", "languages": ["en", "it"]}`} {
files := validFiles()
files["site.json"] = site
_, _, err := Load(writeSite(t, files))
@ -165,6 +165,16 @@ func TestLoadNeedsBaseURL(t *testing.T) {
}
}
// TestLoadLanguageCannotOverrideBaseURL checks that the address of the site has one source.
func TestLoadLanguageCannotOverrideBaseURL(t *testing.T) {
files := validFiles()
files["lang/it.json"] = `{"lang_name": "Italiano", "base_url": "https://example.org", "nav": {}}`
_, _, err := Load(writeSite(t, files))
if err == nil || !strings.Contains(err.Error(), "lang/it.json") || !strings.Contains(err.Error(), "base_url") {
t.Errorf("Load() error = %v, want it to name lang/it.json and base_url", err)
}
}
// TestLoadNeedsLanguages checks that an empty language list is an error.
func TestLoadNeedsLanguages(t *testing.T) {
files := validFiles()