motorino/internal/mdx/mdx_test.go

111 lines
3.7 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package mdx
import (
"strings"
"testing"
)
// 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)
}
}
// 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")
}
}