docwell-server converts between page documents and Markdown or HTML (a new convert package), so every client and the API get the same results. Pages export as a download from the page menu; spaces import one page per .md, .txt or .html file, taking a leading level-1 heading as the title. Import keeps only what the editor schema knows, which drops scripts, styles, event handlers and javascript: links; tables become one paragraph per row until the editor has tables. The UI package gains DwMenu, a small accessible action menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
// 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 & <scope>"}]},
|
|
{"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 & \<scope\>` + "\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",
|
|
"\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 <b>", mustParse(t, sample), opts)
|
|
for _, want := range []string{
|
|
"<title>A <b></title>",
|
|
"<h2>Plan & <scope></h2>",
|
|
"<strong>bold</strong>",
|
|
`<a href="https://docwell.test/x">a link</a>`,
|
|
`<ol start="3">`,
|
|
`<pre><code class="language-go">x := 1 < 2</code></pre>`,
|
|
`<img src="https://api.docwell.test/api/v1/files/abc" alt="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(`<div onclick="x()"><h1>Hi <script>alert(1)</script></h1>
|
|
<p>Text <a href="javascript:evil()">bad</a> <a href="https://ok.test">ok</a><img src="x" onerror="alert(1)"></p>
|
|
<style>p{}</style><iframe src="https://evil.test"></iframe>
|
|
<table><tr><td>a</td><td><b>b</b></td></tr></table><h5>deep</h5></div>`)
|
|
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**") || !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)
|
|
}
|
|
}
|