motorino/internal/render/markdown.go
tgiachi 917683afa4 fix: close the minor findings of the first review
Require refuses an empty value. Heading ids: an explicit {#id} must be sound
and unique, generated ids avoid the explicit ones and each other, and a heading
with no ASCII letters gets a fallback. Fenced code blocks keep their
placeholders and a backslash keeps one anywhere. The em dash rule covers the
header of the manifesto pages and the HTML entities. The feed makes src links
absolute, base_url is required, and the news page must be a folder index. A
category written twice in a post counts once, a block name in a news title is no
longer taken for a placeholder, and build refuses extra arguments.
2026-11-15 10:32:19 +01:00

108 lines
3 KiB
Go

// 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
}