package doc import ( "strings" "testing" ) // fakeResolver resolves every target except those starting with "missing" and // those ending in an attachment extension, so the tests can exercise all four // render paths without a real archive. The hrefs it hands back are deliberately // arbitrary: what is under test is the renderer, not the resolution rules. type fakeResolver struct{} func (fakeResolver) Resolve(fromDir, dest string) Target { switch { case strings.HasPrefix(dest, "http"): return Target{Href: dest, IsExternal: true} case strings.HasPrefix(dest, "missing"): return Target{Href: "/p/" + dest, Missing: true} case strings.HasSuffix(dest, ".png"), strings.HasSuffix(dest, ".pdf"), strings.HasSuffix(dest, ".base"): return Target{Href: "/assets/" + dest} } id, frag, _ := strings.Cut(dest, "#") href := "/p/" + id if frag != "" { href += "#" + strings.ToLower(frag) } return Target{Href: href, PageID: id, Kind: "markdown"} } func renderMD(t *testing.T, src string) Result { t.Helper() return NewRenderer().Render([]byte(src), "specs", fakeResolver{}) } func TestWikilink(t *testing.T) { cases := []struct { name string src string wantHTML []string wantAbsent []string wantLinked []string }{ { name: "plain", src: "see [[lsm-tree]] for details", wantHTML: []string{`lsm-tree`}, wantLinked: []string{"lsm-tree"}, }, { name: "aliased", src: "see [[lsm-tree|LSM trees]]", wantHTML: []string{`href="/p/lsm-tree">LSM trees`}, wantLinked: []string{"lsm-tree"}, }, { name: "path qualified shows the last segment", src: "[[reports/watchlist]]", wantHTML: []string{`href="/p/reports/watchlist">watchlist`}, wantLinked: []string{"reports/watchlist"}, }, { name: "heading reference", src: "[[ai-too-expensive#Zillow|AI Chernobyl]]", wantHTML: []string{`href="/p/ai-too-expensive#zillow">AI Chernobyl`}, wantLinked: []string{"ai-too-expensive"}, }, { name: "image embed renders inline", src: "![[diagram.png]]", wantHTML: []string{`wiki.base`}, wantAbsent: []string{"lsm-tree`}, wantLinked: []string{"lsm-tree"}, }, { name: "unresolved link is visibly broken, never dropped", src: "[[missing-page]]", wantHTML: []string{`missing-page`}, wantAbsent: []string{"[[page]]", }, wantAbsent: []string{`class="wikilink"`}, }, { name: "inside a fenced block it is literal text", src: "```\n[[page]] and ![[transclusions]]\n```\n", wantHTML: []string{ "[[page]] and ![[transclusions]]", }, wantAbsent: []string{`class="wikilink"`, "LSM`}, wantLinked: []string{"lsm-tree"}, }, { name: "label is HTML-escaped", src: "[[lsm-tree|]]", wantAbsent: []string{"