| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | const sample = `# Acme |
| 9 | |
| 10 | Acme is a customer. See [[Brightwater]] and [[Halden|the robots]]. |
| 11 | |
| 12 | - first item |
| 13 | - second item with [[Acme Ltd#^q7]] |
| 14 | continued |
| 15 | - nested item ^n1 |
| 16 | |
| 17 | ` + "```go\nfmt.Println(\"[[Not a link]]\")\n```" + ` |
| 18 | |
| 19 | > a quote |
| 20 | > over two lines |
| 21 | |
| 22 | --- |
| 23 | |
| 24 | A paragraph ^p1` |
| 25 | |
| 26 | func kinds(blocks []Block) string { |
| 27 | var names []string |
| 28 | for _, block := range blocks { |
| 29 | names = append(names, block.Kind) |
| 30 | } |
| 31 | return strings.Join(names, ",") |
| 32 | } |
| 33 | |
| 34 | func TestParseFindsSection32sBlocks(t *testing.T) { |
| 35 | blocks := Parse(sample) |
| 36 | want := "heading,paragraph,item,item,item,code,quote,rule,paragraph" |
| 37 | if got := kinds(blocks); got != want { |
| 38 | t.Fatalf("blocks %s, want %s", got, want) |
| 39 | } |
| 40 | if blocks[4].Level != 1 || blocks[4].Marker != "n1" || |
| 41 | blocks[8].Marker != "p1" { |
| 42 | t.Fatalf("nesting or markers lost: %+v", blocks) |
| 43 | } |
| 44 | if !strings.Contains(blocks[3].Source, "continued") { |
| 45 | t.Fatal("an item's continuation line left its item") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestJoinKeepsABodyItsBlocksCameFrom(t *testing.T) { |
| 50 | body := "# A\n\npara\n\n- one\n- two\n\nend" |
| 51 | if got := Join(Parse(body)); got != body { |
| 52 | t.Fatalf("round trip:\n%q\nwant\n%q", got, body) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestLinksSkipCodeAndCarryTheirBlock(t *testing.T) { |
| 57 | links := Links(sample) |
| 58 | var targets []string |
| 59 | for _, link := range links { |
| 60 | targets = append(targets, link.Target+"#"+link.Anchor+"|"+link.Shown) |
| 61 | } |
| 62 | got := strings.Join(targets, " ") |
| 63 | want := "Brightwater#| Halden#|the robots Acme Ltd#q7|" |
| 64 | if got != want { |
| 65 | t.Fatalf("links %q, want %q", got, want) |
| 66 | } |
| 67 | if links[2].Block != 3 { |
| 68 | t.Fatalf("the block link is in block %d", links[2].Block) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestEmbedsAreNotLinks(t *testing.T) { |
| 73 | if links := Links("see ![[fichas:card-12]] here"); len(links) != 0 { |
| 74 | t.Fatalf("an embed was read as a link: %+v", links) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | type fixed struct{} |
| 79 | |
| 80 | func (fixed) Resolve(target, anchor string) Resolution { |
| 81 | switch target { |
| 82 | case "Pending": |
| 83 | return Resolution{Href: "/new?title=Pending", Pending: true} |
| 84 | case "2026-02-30": |
| 85 | return Resolution{Broken: true} |
| 86 | } |
| 87 | return Resolution{Href: "/p/1", Title: target, |
| 88 | BlockGone: anchor == "gone"} |
| 89 | } |
| 90 | |
| 91 | func TestRenderEscapesEverythingAPersonTyped(t *testing.T) { |
| 92 | hostile := `<script>alert(1)</script> [x](javascript:alert(1)) ` + |
| 93 | `[[<img src=x onerror=alert(1)>]] ` + "`<b>`" + |
| 94 | ` [ok](https://example.com/"onmouseover=x)` |
| 95 | got := string(Render(Parse(hostile), fixed{})) |
| 96 | for _, bad := range []string{"<script", "<img", "<b>", |
| 97 | `href="javascript`, `"onmouseover`} { |
| 98 | if strings.Contains(got, bad) { |
| 99 | t.Fatalf("rendered %q:\n%s", bad, got) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestRenderDrawsLinksBlocksAndLists(t *testing.T) { |
| 105 | got := string(RenderPage(Parse(sample+"\n\n[[Pending]] [[2026-02-30]] "+ |
| 106 | "[[Acme#^gone]] **bold** *soft* [web](https://example.com)"), |
| 107 | fixed{})) |
| 108 | for _, want := range []string{ |
| 109 | `<h2>Acme</h2>`, |
| 110 | `<a href="/p/1" mapa-chip="team" aria-label="the robots, team page">`, |
| 111 | `<ul><li><p>first item</p></li><li>`, |
| 112 | `<ul><li id="b-n1"><p>nested item</p></li></ul>`, |
| 113 | `<pre><code>fmt.Println("[[Not a link]]")</code></pre>`, |
| 114 | `<p id="b-p1">A paragraph</p>`, |
| 115 | `mapa-chip="pending" aria-label="Pending, page not written yet">`, |
| 116 | `<span mapa-chip="broken">2026-02-30</span>`, |
| 117 | `mapa-chip="block gone"`, `block removed</span></a>`, |
| 118 | `<strong>bold</strong>`, `<em>soft</em>`, |
| 119 | `<a href="https://example.com" rel="noopener noreferrer">web</a>`, |
| 120 | } { |
| 121 | if !strings.Contains(got, want) { |
| 122 | t.Errorf("missing %q in:\n%s", want, got) |
| 123 | } |
| 124 | } |
| 125 | if strings.Contains(got, ">\n<") { |
| 126 | t.Errorf("text between blocks:\n%s", got) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // TestEmphasisIsPinned is spec §5's table: the one fix, and three cases |
| 131 | // that keep the HTML they had. |
| 132 | func TestEmphasisIsPinned(t *testing.T) { |
| 133 | for source, want := range map[string]string{ |
| 134 | "*a **b** c*": "<p><em>a <strong>b</strong> c</em></p>", |
| 135 | "***x***": "<p><em><strong>x</strong></em></p>", |
| 136 | "**a** ***b*** **c**": "<p><strong>a</strong> " + |
| 137 | "<em><strong>b</strong></em> <strong>c</strong></p>", |
| 138 | "**a *b* c**": "<p>**a <em>b</em> c**</p>", |
| 139 | } { |
| 140 | if got := string(Render(Parse(source), fixed{})); got != want { |
| 141 | t.Errorf("%q drew %q, want %q", source, got, want) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestListsChangeKindAndKeepNesting(t *testing.T) { |
| 147 | body := "- a\n1. b\n - c\n more\n 2) d\n- [x] e\n- [ ] f" |
| 148 | want := "<ul><li><p>a</p></li></ul><ol><li><p>b</p>" + |
| 149 | "<ul><li><p>c<br>more</p></li></ul><ol><li><p>d</p></li></ol>" + |
| 150 | `</li></ol><ul><li mapa-task="done"><p>e</p></li>` + |
| 151 | `<li mapa-task="open"><p>f</p></li></ul>` + EmptyParagraph |
| 152 | if got := string(RenderPage(Parse(body), fixed{})); got != want { |
| 153 | t.Fatalf("drew\n%s\nwant\n%s", got, want) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestRenderPageEndsInAParagraph(t *testing.T) { |
| 158 | for body, want := range map[string]string{ |
| 159 | "": EmptyParagraph, |
| 160 | "a": "<p>a</p>", |
| 161 | "---": "<hr>" + EmptyParagraph, |
| 162 | "# A\n\nb": "<h2>A</h2><p>b</p>", |
| 163 | "- ": "<ul><li><p>" + trailingBreak + "</p></li></ul>" + |
| 164 | EmptyParagraph, |
| 165 | } { |
| 166 | if got := string(RenderPage(Parse(body), fixed{})); got != want { |
| 167 | t.Errorf("%q drew %q, want %q", body, got, want) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestDates(t *testing.T) { |
| 173 | if !IsDate("2026-11-12") || IsDate("2026-02-30") || |
| 174 | !IsDateShaped("2026-02-30") || IsDateShaped("2026-11-12 x") { |
| 175 | t.Fatal("date shapes are wrong") |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestUnlinkedSkipsLinksAndCode(t *testing.T) { |
| 180 | block := Parse("Acme and [[Acme]] and `Acme` and [Acme](https://a.b)")[0] |
| 181 | got := strings.Join(Unlinked(block), "|") |
| 182 | if got != "Acme and | and | and " { |
| 183 | t.Fatalf("unlinked runs %q", got) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestReplaceTouchesOnlyTheNamedBlocks(t *testing.T) { |
| 188 | body := "one\n\n\n- two\n- three\n\nfour " |
| 189 | blocks := Parse(body) |
| 190 | got := Replace(body, blocks, map[int]string{1: "- 2", 3: ""}) |
| 191 | if got != "one\n\n\n- 2\n- three\n\n" { |
| 192 | t.Fatalf("got %q", got) |
| 193 | } |
| 194 | for _, block := range blocks { |
| 195 | if body[block.Start:block.End] != block.Source { |
| 196 | t.Fatalf("offsets of %q are wrong", block.Source) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestRetargetKeepsAnchorsShownTextAndCode(t *testing.T) { |
| 202 | body := "See [[Acme]], [[acme#^b1|the block]] and [[Other]].\n\n" + |
| 203 | "```\n[[Acme]]\n```" |
| 204 | got, changed := Retarget(body, func(target string) bool { |
| 205 | return strings.EqualFold(target, "acme") |
| 206 | }, "Acme Ltd") |
| 207 | want := "See [[Acme Ltd]], [[Acme Ltd#^b1|the block]] and " + |
| 208 | "[[Other]].\n\n```\n[[Acme]]\n```" |
| 209 | if got != want || changed != 2 { |
| 210 | t.Fatalf("got %q (%d)", got, changed) |
| 211 | } |
| 212 | } |
| 213 | |