package importer import ( "strings" "testing" ) func TestToMarkdown(t *testing.T) { for _, c := range []struct{ name, in, want string }{ {"paragraphs and inline", `
Some bold, italic and code with a link.
Second.
`, "Some **bold**, *italic* and `code` with a [link](/about/).\n\nSecond.\n"}, {"headings and rule", `Text
Four
`, "> Said *this*.\n>\n> And that.\n"}, {"code block", `Said this.
And that.
fmt.Println("a ` + "```" + ` b")`, "````go\nfmt.Println(\"a ``` b\")\n````\n"},
{"figure", `
1. Not a list, a *star*, snake_case and _edge_ [brackets]
`, "1\\. Not a list, a \\*star\\*, snake_case and \\_edge\\_ \\[brackets\\]\n"}, {"break and strike", `Line one
line two gone
| Plan | Price |
|---|---|
| Basic | $5 |
Hi
\n", "Hi\n"}, } { got := ToMarkdown(c.in) if got.Markdown != c.want || len(got.Lossy) != 0 { t.Errorf("%s:\ngot %q (lossy %v)\nwant %q", c.name, got.Markdown, got.Lossy, c.want) } } } func TestLossy(t *testing.T) { for _, c := range []struct{ in, why string }{ {`a
| a |
Red
`, "inline styles"}, {`E = mc2
`, "superscript"}, } { got := ToMarkdown(c.in) if !strings.Contains(strings.Join(got.Lossy, ","), c.why) { t.Errorf("%s: lossy %v, want %q", c.in, got.Lossy, c.why) } } } func TestSafeImportPaths(t *testing.T) { for in, want := range map[string]string{"../../x": "x", "/a//b/": "a/b", `..\..\etc`: "etc", "news/2026": "news/2026"} { if got := safeDir(in); got != want { t.Errorf("safeDir(%q) = %q, want %q", in, got, want) } } if s := slugify("../../../x/README"); strings.Contains(s, "/") || strings.Contains(s, "..") { t.Errorf("slugify kept a path: %q", s) } if s := slugify("Café Ñandú 日本"); s != "café-ñandú-日本" { t.Errorf("letters lost: %q", s) } }