53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
var reHeadingTag = regexp.MustCompile(`<h([1-6])\b`)
|
|
|
|
func TestTranscriptHeadingsNestUnderThePage(t *testing.T) {
|
|
for src, want := range map[string]string{
|
|
"# A\n\n## B\n\n#### C\n\n## D": `<h2 class="md-h1">A</h2> <h3 class="md-h2">B</h3> <h4 class="md-h4">C</h4> <h3 class="md-h2">D</h3>`,
|
|
"### A\n\n### B\n\n#### C": `<h2 class="md-h3">A</h2> <h2 class="md-h3">B</h2> <h3 class="md-h4">C</h3>`,
|
|
"```\n# not a heading\n```\n\n## A": `<h2 class="md-h2">A</h2>`,
|
|
} {
|
|
var got []string
|
|
for _, line := range strings.Split(Render(src), "\n") {
|
|
if strings.HasPrefix(line, "<h") {
|
|
got = append(got, line)
|
|
}
|
|
}
|
|
if g := strings.Join(got, " "); g != want {
|
|
t.Errorf("Render(%q) headings:\n got %s\nwant %s", src, g, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPagesHaveOneH1AndNoSkippedLevels(t *testing.T) {
|
|
for _, path := range []string{
|
|
"/", "/browse", "/browse?view=table", "/browse?collection=sidtoday-archive",
|
|
"/browse?collection=sidtoday-archive&view=card", "/browse?collection=foia",
|
|
"/browse/agency/NSA", "/search?q=prism", "/search?q=zzzqqq", "/doc/prism-slides",
|
|
"/doc/prism-slides/versions/lemonde", "/glossary", "/articles", "/about", "/pinned",
|
|
} {
|
|
body := get(t, path).Body.String()
|
|
h1, prev := 0, 0
|
|
for _, m := range reHeadingTag.FindAllStringSubmatch(body, -1) {
|
|
level, _ := strconv.Atoi(m[1])
|
|
if level == 1 {
|
|
h1++
|
|
}
|
|
if level > prev+1 {
|
|
t.Errorf("%s: <h%d> follows <h%d>", path, level, prev)
|
|
}
|
|
prev = level
|
|
}
|
|
if h1 != 1 {
|
|
t.Errorf("%s: %d <h1>, want 1", path, h1)
|
|
}
|
|
}
|
|
}
|