The editor gains the blocks and formatting it lacked next to AFFiNE: - Tables with a header row, resizable columns, a toolbar to add, delete, merge and split, and rows that move by a drag grip, toolbar buttons or Ctrl+Alt+Up/Down. - To-do lists (also from "[] "), callouts with an emoji and colour, and headings down to level 6 (Ctrl+Shift+0-6 for text and headings). - Text colour and highlights from a fixed palette that follows the theme, and left, centre and right alignment. - Optional line numbers on code blocks, with a default hosts can set. - PDF attachments show their pages inline; pdf.js loads only then, and the mobile bundle leaves it out. - The selection toolbar adds underline, colour, align, inline math, make a linked page and copy block link. A grip beside each block drags it and opens a menu: turn into, move, duplicate, copy, copy link, delete. - The slash menu adds Today, Tomorrow, Yesterday, Now and New page. The web app translates the new words and makes linked pages as children of the open page. The mobile bridge gets underline, to-do, callout and table commands, so its protocol moves to version 2 and the bundle is rebuilt. docwell-server's converters learn the new nodes: Markdown writes task lists, GitHub alerts and tables and reads them back; HTML keeps colours and alignment and reads its own output back; PDF draws tables, to-dos, callouts and coloured text; Word import keeps tables, alignment, H4-H6 and colours; Notion import turns databases into tables and keeps callouts and to-dos. Imported tables and H4-H6 headings no longer collapse into paragraphs and H3. 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** |\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)
|
|
}
|
|
}
|