motorino/internal/mdx/mdx_test.go
tgiachi 4ab8f8d122
All checks were successful
check / check (pull_request) Successful in 30s
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.
2026-11-15 10:38:56 +01:00

253 lines
9.5 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package mdx
import (
"strings"
"testing"
"git.kmalloc.org/squid/motorino/internal/render"
)
// TestRenderParagraphAndList checks the basic blocks and inline markup.
func TestRenderParagraphAndList(t *testing.T) {
html, _, err := Render("Hello **world** and [a link](https://kmalloc.org/).\n\n- one\n- two\n")
if err != nil {
t.Fatal(err)
}
for _, want := range []string{
"<p>Hello <strong>world</strong> and <a href=\"https://kmalloc.org/\">a link</a>.</p>",
"<li>one</li>", "<li>two</li>",
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
}
// TestRenderHeadingIDs checks that ids come from Slug and are unique.
func TestRenderHeadingIDs(t *testing.T) {
src := "## 01. First\n\n## Our commitments\n\n## Same\n\n## Same\n"
html, headings, err := Render(src)
if err != nil {
t.Fatal(err)
}
wantIDs := []string{"01", "commitments", "same", "same-2"}
if len(headings) != len(wantIDs) {
t.Fatalf("got %d headings, want %d: %+v", len(headings), len(wantIDs), headings)
}
for i, id := range wantIDs {
if headings[i].ID != id || headings[i].Level != 2 {
t.Errorf("heading %d = %+v, want level 2 and id %q", i, headings[i], id)
}
if !strings.Contains(html, `id="`+id+`"`) {
t.Errorf("html does not contain id=%q", id)
}
}
if headings[0].Text != "01. First" {
t.Errorf("heading text = %q, want %q", headings[0].Text, "01. First")
}
}
// TestRenderRawHTMLPasses checks that raw HTML blocks and comments survive,
// because pages use them for tables with data-label cells and hidden notes.
func TestRenderRawHTMLPasses(t *testing.T) {
src := "Intro.\n\n<table class=\"data\">\n<tr><td data-label=\"Kept\">14 days</td></tr>\n</table>\n\n<!-- TODO check: not shown -->\n\nEnd.\n"
html, _, err := Render(src)
if err != nil {
t.Fatal(err)
}
for _, want := range []string{
`<table class="data">`, `<td data-label="Kept">14 days</td>`, "<!-- TODO check: not shown -->",
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
}
// TestRenderTitleHasNoID checks that a level 1 heading gets no id, so it cannot
// collide with a level 2 heading that has the same words.
func TestRenderTitleHasNoID(t *testing.T) {
html, headings, err := Render("# Services\n\n## Services\n")
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, `<h1 id=`) {
t.Errorf("html %q gives the h1 an id", html)
}
if !strings.Contains(html, `<h2 id="services">`) {
t.Errorf("html %q lost the id of the h2", html)
}
if len(headings) != 2 || headings[0].ID != "" || headings[1].ID != "services" {
t.Errorf("headings = %+v, want the h1 without id and the h2 as services", headings)
}
}
// TestRenderExplicitHeadingID checks that "{#id}" after a heading sets its id
// and is not part of its text, and that other headings still get theirs.
func TestRenderExplicitHeadingID(t *testing.T) {
html, headings, err := Render("## Request an account {#account}\n\n## Other\n\n## Request an account\n")
if err != nil {
t.Fatal(err)
}
for _, want := range []string{`<h2 id="account">Request an account</h2>`, `<h2 id="other">Other</h2>`, `<h2 id="request-an-account">Request an account</h2>`} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
if len(headings) != 3 || headings[0].ID != "account" || headings[0].Text != "Request an account" {
t.Errorf("headings = %+v", headings)
}
}
// TestRenderRejectsRepeatedExplicitID checks that two headings cannot ask for the same id.
func TestRenderRejectsRepeatedExplicitID(t *testing.T) {
for _, src := range []string{"## Foo {#a}\n\n## Bar {#a}\n"} {
_, _, err := Render(src)
if err == nil || !strings.Contains(err.Error(), "id") {
t.Errorf("Render(%q) error = %v, want an error about the repeated id", src, err)
}
}
}
// TestRenderGeneratedIDAvoidsLaterExplicit checks that a generated id never takes
// one that a later heading asks for by name, in either order of the two.
func TestRenderGeneratedIDAvoidsLaterExplicit(t *testing.T) {
_, headings, err := Render("## Foo\n\n## Bar {#foo-2}\n\n## Foo\n")
if err != nil {
t.Fatal(err)
}
var ids []string
for _, h := range headings {
ids = append(ids, h.ID)
}
if got, want := strings.Join(ids, ","), "foo,foo-2,foo-3"; got != want {
t.Errorf("ids = %s, want %s", got, want)
}
}
// TestRenderSuffixClash checks that a numbered suffix never repeats an id.
func TestRenderSuffixClash(t *testing.T) {
_, headings, err := Render("## A\n\n## A\n\n## A 2\n")
if err != nil {
t.Fatal(err)
}
seen := map[string]bool{}
for _, h := range headings {
if seen[h.ID] {
t.Errorf("id %q is used twice: %+v", h.ID, headings)
}
seen[h.ID] = true
}
}
// TestRenderEmptySlugFallback checks that a heading with no ASCII letters or digits still gets an id.
func TestRenderEmptySlugFallback(t *testing.T) {
html, headings, err := Render("## \u4f60\u597d\n\n## \u4e16\u754c\n")
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, `id=""`) {
t.Errorf("html %q has an empty id", html)
}
if len(headings) != 2 || headings[0].ID != "section" || headings[1].ID != "section-2" {
t.Errorf("headings = %+v, want section and section-2", headings)
}
}
// TestRenderBadExplicitIDCannotInject checks that a malformed {#id} never ends
// up as an id with quotes, spaces or angle brackets, whether goldmark leaves it
// as text or the id check refuses it; and that sound ids pass.
func TestRenderBadExplicitIDCannotInject(t *testing.T) {
for _, src := range []string{"## A {#a\"b}\n", "## A {#a b}\n", "## A {#<x>}\n"} {
html, headings, err := Render(src)
if err != nil {
continue
}
for _, h := range headings {
if !explicitID.MatchString(h.ID) {
t.Errorf("Render(%q) gave the id %q", src, h.ID)
}
}
if strings.Contains(html, `id="a"b`) || strings.Contains(html, `id="a b`) {
t.Errorf("Render(%q) = %q, the id broke out of its attribute", src, html)
}
}
if _, _, err := Render("## A {#v1.2}\n\n## B {#01}\n\n## C {#a_b:c-d}\n"); err != nil {
t.Errorf("Render(valid ids) error = %v", err)
}
}
// 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")
if err != nil {
t.Fatal(err)
}
if len(headings) != 1 || headings[0].Text != "Free your mind" {
t.Errorf("headings = %+v, want one with plain text %q", headings, "Free your mind")
}
}