// SPDX-License-Identifier: AGPL-3.0-or-later package convert import ( "encoding/json" "strings" "testing" ) const sample = `{"type":"doc","content":[ {"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Plan & "}]}, {"type":"paragraph","content":[ {"type":"text","text":"Ship "}, {"type":"text","text":"bold","marks":[{"type":"bold"}]}, {"type":"text","text":" and "}, {"type":"text","text":"a link","marks":[{"type":"link","attrs":{"href":"https://docwell.test/x"}}]}, {"type":"text","text":" and "}, {"type":"text","text":"bad","marks":[{"type":"link","attrs":{"href":"javascript:alert(1)"}}]}, {"type":"text","text":" *stars*"}]}, {"type":"bulletList","content":[ {"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"one"}]}, {"type":"orderedList","attrs":{"start":3},"content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"nested"}]}]}]}]}, {"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"two"}]}]}]}, {"type":"codeBlock","attrs":{"language":"go"},"content":[{"type":"text","text":"x := 1 < 2"}]}, {"type":"blockquote","content":[{"type":"paragraph","content":[{"type":"text","text":"quoted"}]}]}, {"type":"image","attrs":{"src":"/api/v1/files/abc","alt":"diagram"}}, {"type":"horizontalRule"} ]}` func mustParse(t *testing.T, raw string) Node { t.Helper() doc, err := Parse(json.RawMessage(raw)) if err != nil { t.Fatal(err) } return doc } var opts = Options{FileURL: func(p string) string { return "https://api.docwell.test" + p }} func TestMarkdown(t *testing.T) { md := MarkdownPage("Title", mustParse(t, sample), opts) for _, want := range []string{ "# Title\n", `## Plan & \` + "\n", `Ship **bold** and [a link](https://docwell.test/x) and bad \*stars\*` + "\n", "- one\n\n 3. nested\n- two\n", "```go\nx := 1 < 2\n```\n", "> quoted\n", "![diagram](https://api.docwell.test/api/v1/files/abc)\n", "---\n", } { if !strings.Contains(md, want) { t.Errorf("markdown lacks %q:\n%s", want, md) } } if strings.Contains(md, "javascript:") { t.Error("unsafe links must be dropped") } } func TestHTML(t *testing.T) { h := HTMLPage("A ", mustParse(t, sample), opts) for _, want := range []string{ "A <b>", "

Plan & <scope>

", "bold", `a link`, `
    `, `
    x := 1 < 2
    `, `diagram`, } { if !strings.Contains(h, want) { t.Errorf("html lacks %q:\n%s", want, h) } } if strings.Contains(h, "javascript:") { t.Error("unsafe links must be dropped") } } func TestMarkdownRoundTrip(t *testing.T) { doc := mustParse(t, sample) back, err := FromMarkdown(Markdown(doc, Options{})) if err != nil { t.Fatal(err) } if got, want := Markdown(back, Options{}), Markdown(doc, Options{}); got != want { t.Errorf("round trip changed the document:\n--- got\n%s\n--- want\n%s", got, want) } } func TestFromHTMLSanitizes(t *testing.T) { doc, err := FromHTML(`

    Hi

    Text bad ok

    ab
    deep
    `) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(doc) s := string(raw) for _, bad := range []string{"alert", "evil", "onclick", "onerror", "iframe", "p{}"} { if strings.Contains(s, bad) { t.Errorf("import kept %q: %s", bad, s) } } title, rest := Title(doc) if title != "Hi" { t.Errorf("title = %q", title) } if md := Markdown(rest, Options{}); !strings.Contains(md, "[ok](https://ok.test)") || !strings.Contains(md, "| a | **b** |\n| --- | --- |") || !strings.Contains(md, "##### deep") { t.Errorf("content = %s", md) } } func TestEmpty(t *testing.T) { doc, err := Parse(nil) if err != nil || doc.Type != "doc" { t.Fatal(doc, err) } if md := Markdown(doc, Options{}); md != "\n" { t.Errorf("empty markdown = %q", md) } back, _ := FromMarkdown("") if len(back.Content) != 1 || back.Content[0].Type != "paragraph" { t.Errorf("empty import = %+v", back) } }