// 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{
"
Hello world and a link.
",
"one", "two",
} {
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\n\n\n\nEnd.\n"
html, _, err := Render(src)
if err != nil {
t.Fatal(err)
}
for _, want := range []string{
``, `| 14 days | `, "",
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, 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")
}
}