Import (Import on a space's page list, or POST /spaces/{id}/import):
- Word (.docx, up to 25 MB): Title and heading styles, bulleted and
numbered lists with nesting, quotes, code styles, bold, italic,
underline, strikethrough, links (including HYPERLINK fields) and
images, which are stored as attachments of the new page. Tables come in
as one paragraph per row, as in HTML import.
- Notion export (.zip, Markdown & CSV or HTML, up to 100 MB and 2000
pages, split exports included): rebuilds the page tree, points links
between the pages (relative and notion.so ones) at the new pages, and
stores images and files as attachments. A database becomes a page that
lists its rows, with the row pages under it. The response lists the
pages made at the parent's level.
- Every file is read before any page is made, so a bad file leaves
nothing behind. Archives are capped by bytes actually unpacked, not by
the sizes they declare.
PDF export (page menu, or ?format=pdf): A4 with the Go fonts (Latin,
Polish, Greek, Cyrillic), a bookmark per heading and page numbers.
Images on the page are drawn in (JPEG as is; PNG, GIF and WebP redrawn);
images on other sites are not fetched. Diagrams become a link to their
file and Mermaid charts show their source. Emoji are left out, since the
fonts have none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
263 lines
11 KiB
Go
263 lines
11 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package convert
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func zipOf(t *testing.T, files map[string][]byte) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
for name, data := range files {
|
|
w, err := zw.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = w.Write(data)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func pngOf(t *testing.T, w, h int) []byte {
|
|
t.Helper()
|
|
img := image.NewNRGBA(image.Rect(0, 0, w, h))
|
|
for x := range w {
|
|
img.Set(x, x%h, color.NRGBA{R: 200, A: 255})
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := png.Encode(&buf, img); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func asJSON(t *testing.T, n Node) string {
|
|
t.Helper()
|
|
b, err := json.Marshal(n)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
const docxNS = `xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ` +
|
|
`xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" ` +
|
|
`xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" ` +
|
|
`xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"`
|
|
|
|
func TestFromDOCX(t *testing.T) {
|
|
doc := `<?xml version="1.0" encoding="UTF-8"?><w:document ` + docxNS + `><w:body>
|
|
<w:p><w:pPr><w:pStyle w:val="Title"/></w:pPr><w:r><w:t>Quarterly plan</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Goals</w:t></w:r></w:p>
|
|
<w:p><w:r><w:t xml:space="preserve">Plain </w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>bold</w:t></w:r><w:r><w:rPr><w:i/><w:b w:val="0"/></w:rPr><w:t xml:space="preserve"> italic</w:t></w:r><w:r><w:rPr><w:strike/></w:rPr><w:t xml:space="preserve"> gone</w:t></w:r></w:p>
|
|
<w:p><w:hyperlink r:id="rLink"><w:r><w:t>a link</w:t></w:r></w:hyperlink><w:r><w:t xml:space="preserve"> and </w:t></w:r><w:hyperlink r:id="rBad"><w:r><w:t>a bad one</w:t></w:r></w:hyperlink></w:p>
|
|
<w:p><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> HYPERLINK "https://docwell.test/field" </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t>field link</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p>
|
|
<w:p><w:pPr><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>one</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:numPr><w:ilvl w:val="1"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>one-a</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>two</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>first</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:pStyle w:val="Quote"/></w:pPr><w:r><w:t>Said someone</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:pStyle w:val="Code"/></w:pPr><w:r><w:t>x := 1</w:t></w:r></w:p>
|
|
<w:p><w:pPr><w:pStyle w:val="Code"/></w:pPr><w:r><w:t>y := 2</w:t></w:r></w:p>
|
|
<w:p><w:r><w:drawing><wp:inline><wp:docPr id="1" name="Picture 1" descr="A chart"/><a:graphic><a:graphicData><a:blip r:embed="rImg"/></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p>
|
|
<w:tbl><w:tr><w:tc><w:p><w:r><w:t>Name</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>Role</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:p><w:r><w:t>Ada</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>Lead</w:t></w:r></w:p></w:tc></w:tr></w:tbl>
|
|
<w:p><w:r><w:t>Zażółć gęślą jaźń</w:t></w:r></w:p>
|
|
</w:body></w:document>`
|
|
rels := `<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
<Relationship Id="rLink" Type="hyperlink" Target="https://docwell.test/x" TargetMode="External"/>
|
|
<Relationship Id="rBad" Type="hyperlink" Target="javascript:alert(1)" TargetMode="External"/>
|
|
<Relationship Id="rImg" Type="image" Target="media/image1.png"/>
|
|
</Relationships>`
|
|
styles := `<?xml version="1.0"?><w:styles ` + docxNS + `>
|
|
<w:style w:type="paragraph" w:styleId="Title"><w:name w:val="Title"/></w:style>
|
|
<w:style w:type="paragraph" w:styleId="Heading2"><w:name w:val="heading 2"/></w:style>
|
|
<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/></w:style>
|
|
<w:style w:type="paragraph" w:styleId="Code"><w:name w:val="Code Block"/></w:style>
|
|
</w:styles>`
|
|
numbering := `<?xml version="1.0"?><w:numbering ` + docxNS + `>
|
|
<w:abstractNum w:abstractNumId="10"><w:lvl w:ilvl="0"><w:numFmt w:val="bullet"/></w:lvl><w:lvl w:ilvl="1"><w:numFmt w:val="bullet"/></w:lvl></w:abstractNum>
|
|
<w:abstractNum w:abstractNumId="11"><w:lvl w:ilvl="0"><w:numFmt w:val="decimal"/></w:lvl></w:abstractNum>
|
|
<w:num w:numId="1"><w:abstractNumId w:val="10"/></w:num>
|
|
<w:num w:numId="2"><w:abstractNumId w:val="11"/></w:num>
|
|
</w:numbering>`
|
|
img := pngOf(t, 4, 3)
|
|
data := zipOf(t, map[string][]byte{
|
|
"word/document.xml": []byte(doc), "word/_rels/document.xml.rels": []byte(rels),
|
|
"word/styles.xml": []byte(styles), "word/numbering.xml": []byte(numbering), "word/media/image1.png": img,
|
|
})
|
|
got, assets, err := FromDOCX(data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
title, body := Title(got)
|
|
if title != "Quarterly plan" {
|
|
t.Errorf("title = %q", title)
|
|
}
|
|
md := Markdown(body, Options{})
|
|
for _, want := range []string{
|
|
"## Goals",
|
|
"Plain **bold** *italic* ~~gone~~",
|
|
"[a link](https://docwell.test/x) and a bad one",
|
|
"[field link](https://docwell.test/field)",
|
|
"- one\n\n - one-a\n- two",
|
|
"1. first",
|
|
"> Said someone",
|
|
"```\nx := 1\ny := 2\n```",
|
|
"",
|
|
"Name · Role\n\nAda · Lead",
|
|
"Zażółć gęślą jaźń",
|
|
} {
|
|
if !strings.Contains(md, want) {
|
|
t.Errorf("Markdown lacks %q:\n%s", want, md)
|
|
}
|
|
}
|
|
if len(assets) != 1 || assets[0].Name != "image1.png" || !bytes.Equal(assets[0].Data, img) {
|
|
t.Errorf("assets = %+v", assets)
|
|
}
|
|
|
|
if _, _, err := FromDOCX([]byte("not a zip")); err == nil {
|
|
t.Error("a non-zip file was read")
|
|
}
|
|
if _, _, err := FromDOCX(zipOf(t, map[string][]byte{"x.txt": nil})); err == nil {
|
|
t.Error("a zip without word/document.xml was read")
|
|
}
|
|
}
|
|
|
|
func TestReadNotion(t *testing.T) {
|
|
const (
|
|
rootID = "0123456789abcdef0123456789abcdef"
|
|
childID = "fedcba9876543210fedcba9876543210"
|
|
dbID = "aaaabbbbccccddddeeeeffff00001111"
|
|
rowID = "11112222333344445555666677778888"
|
|
)
|
|
inner := zipOf(t, map[string][]byte{
|
|
"Team wiki " + rootID + ".md": []byte("\uFEFF# Team wiki\n\nSee [Onboarding](Team%20wiki%20" + rootID + "/Onboarding%20" + childID + ".md) " +
|
|
"and [the same](https://www.notion.so/Onboarding-" + childID + "?pvs=4).\n\n\n\n[gone](missing.pdf)\n"),
|
|
"Team wiki " + rootID + "/Onboarding " + childID + ".md": []byte("# Onboarding\n\nBack to [wiki](../Team%20wiki%20" + rootID + ".md)\n"),
|
|
"Team wiki " + rootID + "/diagram.png": pngOf(t, 2, 2),
|
|
"Team wiki " + rootID + "/Tasks " + dbID + ".csv": []byte("Name,Status\nShip it,Done\n"),
|
|
"Team wiki " + rootID + "/Tasks " + dbID + "_all.csv": []byte("Name,Status\nShip it,Done\n"),
|
|
"Team wiki " + rootID + "/Tasks " + dbID + "/Ship it " + rowID + ".html": []byte(
|
|
`<html><head><style>body{}</style></head><body><article><header><h1 class="page-title">Ship it</h1></header>` +
|
|
`<div class="page-body"><p>Row body</p><figure><a href="https://example.test/i.png"><img src="https://example.test/i.png"></a></figure></div></article></body></html>`),
|
|
})
|
|
pages, assets, err := ReadNotion(zipOf(t, map[string][]byte{"Export-1-Part-1.zip": inner}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
byTitle := map[string]int{}
|
|
for i, p := range pages {
|
|
byTitle[p.Title] = i
|
|
if p.Parent >= i {
|
|
t.Errorf("%q comes before its parent", p.Title)
|
|
}
|
|
}
|
|
if len(pages) != 4 {
|
|
t.Fatalf("pages = %+v", pages)
|
|
}
|
|
root, child, db, row := byTitle["Team wiki"], byTitle["Onboarding"], byTitle["Tasks"], byTitle["Ship it"]
|
|
if pages[root].Parent != -1 || pages[child].Parent != root || pages[db].Parent != root || pages[row].Parent != db {
|
|
t.Errorf("tree = %+v", pages)
|
|
}
|
|
// Markdown export drops page: links, so look at the document itself.
|
|
j := asJSON(t, pages[root].Doc)
|
|
if strings.Count(j, `"href":"`+PageRef(child)+`"`) != 2 || !strings.Contains(j, `"src":"asset:0"`) || !strings.Contains(j, "gone") {
|
|
t.Errorf("root = %s", j)
|
|
}
|
|
if strings.Contains(j, "missing.pdf") {
|
|
t.Errorf("a link to a missing file was kept: %s", j)
|
|
}
|
|
if j := asJSON(t, pages[child].Doc); !strings.Contains(j, `"href":"`+PageRef(root)+`"`) {
|
|
t.Errorf("child = %s", j)
|
|
}
|
|
if md := Markdown(pages[db].Doc, Options{}); !strings.Contains(md, "**Name** · **Status**\n\nShip it · Done") {
|
|
t.Errorf("database = %s", md)
|
|
}
|
|
if md := Markdown(pages[row].Doc, Options{}); !strings.Contains(md, "Row body\n\n") {
|
|
t.Errorf("row = %s", md)
|
|
}
|
|
if len(assets) != 1 || assets[0].Name != "diagram.png" {
|
|
t.Errorf("assets = %+v", assets)
|
|
}
|
|
|
|
if _, _, err := ReadNotion(zipOf(t, map[string][]byte{"a.png": nil})); err != ErrNotNotion {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRewriteURLs(t *testing.T) {
|
|
doc, err := FromMarkdown("[x](asset:0) [y](https://k.test)\n\n\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The safety check drops asset: links on a plain import.
|
|
if strings.Contains(asJSON(t, doc), "asset:") {
|
|
t.Fatalf("asset: URL kept: %s", asJSON(t, doc))
|
|
}
|
|
doc = Node{Type: "doc", Content: []Node{
|
|
{Type: "paragraph", Content: []Node{{Type: "text", Text: "x", Marks: []Mark{{Type: "link", Attrs: map[string]any{"href": "asset:0"}}}}}},
|
|
{Type: "image", Attrs: map[string]any{"src": "asset:1"}},
|
|
{Type: "image", Attrs: map[string]any{"src": "asset:2"}},
|
|
}}
|
|
out := RewriteURLs(doc, func(u string) string {
|
|
return map[string]string{"asset:0": "/api/v1/files/a", "asset:1": "/api/v1/files/b"}[u]
|
|
})
|
|
j := asJSON(t, out)
|
|
if !strings.Contains(j, `"href":"/api/v1/files/a"`) || !strings.Contains(j, `"src":"/api/v1/files/b"`) || strings.Contains(j, "asset:2") {
|
|
t.Errorf("rewritten = %s", j)
|
|
}
|
|
if strings.Contains(asJSON(t, doc), "/api/v1/files") {
|
|
t.Error("RewriteURLs changed its input")
|
|
}
|
|
}
|
|
|
|
func TestPDF(t *testing.T) {
|
|
doc, err := FromMarkdown("## Plan\n\nZażółć **gęślą** *jaźń* `code` [link](/p/abc) 😀\n\n" +
|
|
"- one\n - nested\n- two\n\n1. first\n\n> quoted\n\n```go\nfunc main() {}\n```\n\n---\n\n\n\n\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
doc.Content = append(doc.Content,
|
|
Node{Type: "mermaid", Attrs: map[string]any{"code": "graph TD; A-->B"}},
|
|
Node{Type: "blockMath", Attrs: map[string]any{"latex": `E = mc^2`}},
|
|
Node{Type: "drawio", Attrs: map[string]any{"src": "/api/v1/files/d"}},
|
|
)
|
|
img := pngOf(t, 300, 200)
|
|
var asked []string
|
|
out, err := PDF("Ünïcode title", doc, PDFOptions{BaseURL: "https://docwell.test", Image: func(src string) ([]byte, error) {
|
|
asked = append(asked, src)
|
|
if src == "/api/v1/files/img" {
|
|
return img, nil
|
|
}
|
|
return nil, ErrTooLarge
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.HasPrefix(out, []byte("%PDF-")) || !bytes.Contains(out, []byte("/Subtype /Image")) {
|
|
t.Fatalf("not a PDF with an image (%d bytes)", len(out))
|
|
}
|
|
if !bytes.Contains(out, []byte("https://docwell.test/p/abc")) {
|
|
t.Error("the page link is not absolute")
|
|
}
|
|
if len(asked) != 2 {
|
|
t.Errorf("images asked = %v", asked)
|
|
}
|
|
// An empty page still renders.
|
|
if _, err := PDF("", Node{Type: "doc"}, PDFOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|