Files
Docwell/Server/internal/convert/blocks_test.go
T
tajniak81andClaude Opus 5.5 3dea2bb2f4
CI / go (Server) (push) Failing after 8m51s
CI / go (Web-App) (push) Successful in 11m46s
CI / mobile (push) Failing after 3m44s
CI / docker (push) Successful in 54m15s
Docs / build (push) Successful in 12m28s
Docs / deploy (push) Skipped
CI / frontend (push) Failing after 7m47s
CI / desktop (ubuntu-latest) (push) Successful in 1h27m22s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add Word export, column dragging and the rest of editor parity
Word export: pages download as .docx from the page menu and from
GET /pages/{id}/export?format=docx. The file keeps headings, alignment,
lists, to-dos, quotes, callouts, code, tables with a repeating header
row, text formatting, colours, highlights, links and the page's images.
Word import now reads callouts, to-dos and shaded highlights, so a page
exported to Word imports back as it was. The user guide says how to move
pages to Notion, whose Import reads Word and Markdown files.

Editor:
- Columns move like rows: a grip on the column's top edge drags it, and
  the table toolbar and Ctrl+Alt+Left/Right move it from the keyboard.
  Row and column moving share tablemove.ts.
- Ctrl+Shift+C copies a link to the block the cursor is in.
- Headings 4 to 6 sit under "Other headings" in the slash menu; a search
  still finds them directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:01:06 +01:00

248 lines
8.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"archive/zip"
"bytes"
"encoding/xml"
"errors"
"io"
"strings"
"testing"
)
const phaseA = `{"type":"doc","content":[
{"type":"heading","attrs":{"level":5,"textAlign":"center"},"content":[{"type":"text","text":"Small"}]},
{"type":"paragraph","attrs":{"textAlign":"right"},"content":[
{"type":"text","text":"red","marks":[{"type":"textColor","attrs":{"color":"red"}}]},
{"type":"text","text":" and "},
{"type":"text","text":"marked","marks":[{"type":"highlight","attrs":{"color":"green"}}]}]},
{"type":"taskList","content":[
{"type":"taskItem","attrs":{"checked":true},"content":[{"type":"paragraph","content":[{"type":"text","text":"done"}]}]},
{"type":"taskItem","attrs":{"checked":false},"content":[{"type":"paragraph","content":[{"type":"text","text":"todo"}]}]}]},
{"type":"callout","attrs":{"emoji":"🔥","color":"red"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Careful"}]}]},
{"type":"callout","attrs":{"emoji":"💡","color":"blue"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Plain"}]}]},
{"type":"table","content":[
{"type":"tableRow","content":[
{"type":"tableHeader","content":[{"type":"paragraph","content":[{"type":"text","text":"Name"}]}]},
{"type":"tableHeader","content":[{"type":"paragraph","content":[{"type":"text","text":"Role"}]}]}]},
{"type":"tableRow","content":[
{"type":"tableCell","content":[{"type":"paragraph","content":[{"type":"text","text":"Ada | A"}]}]},
{"type":"tableCell","attrs":{"colspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Lead"}]},{"type":"paragraph","content":[{"type":"text","text":"and more"}]}]}]}]}
]}`
func TestPhaseABlocksMarkdown(t *testing.T) {
md := Markdown(mustParse(t, phaseA), Options{})
for _, want := range []string{
"##### Small\n",
"- [x] done\n- [ ] todo\n",
"> [!CAUTION]\n> 🔥 Careful\n",
"> [!NOTE]\n> Plain\n",
"| Name | Role |\n| --- | --- |\n| Ada \\| A | Lead and more |\n",
} {
if !strings.Contains(md, want) {
t.Errorf("markdown lacks %q:\n%s", want, md)
}
}
// And back: the same kinds of blocks come out again.
doc, err := FromMarkdown(md)
if err != nil {
t.Fatal(err)
}
j := asJSON(t, doc)
for _, want := range []string{
`"level":5`,
`{"type":"taskItem","attrs":{"checked":true}`,
`{"type":"callout","attrs":{"color":"red","emoji":"🔥"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Careful"}]}]}`,
`{"type":"callout","attrs":{"color":"blue","emoji":"💡"}`,
`"type":"tableHeader"`,
`"text":"Ada | A"`,
} {
if !strings.Contains(j, want) {
t.Errorf("import lacks %s:\n%s", want, j)
}
}
}
func TestPhaseABlocksHTML(t *testing.T) {
h := HTML(mustParse(t, phaseA), Options{})
for _, want := range []string{
`<h5 style="text-align:center">Small</h5>`,
`<p style="text-align:right"><span data-color="red" style="color:#c0392b">red</span> and <mark data-color="green"`,
`<li data-type="taskItem" data-checked="true"><input type="checkbox" disabled checked> <p>done</p>`,
`<aside data-type="callout" data-emoji="🔥" data-color="red"`,
`<tr><th><p>Name</p>`,
} {
if !strings.Contains(h, want) {
t.Errorf("html lacks %q:\n%s", want, h)
}
}
// Docwell's own HTML reads back as the document it came from.
doc, err := FromHTML(h)
if err != nil {
t.Fatal(err)
}
if got, want := Markdown(doc, Options{}), Markdown(mustParse(t, phaseA), Options{}); got != want {
t.Errorf("html round trip:\n%s\nwant\n%s", got, want)
}
j := asJSON(t, doc)
for _, want := range []string{`"textAlign":"right"`, `{"type":"textColor","attrs":{"color":"red"}}`, `{"type":"highlight","attrs":{"color":"green"}}`} {
if !strings.Contains(j, want) {
t.Errorf("html import lacks %s:\n%s", want, j)
}
}
}
func TestNotionCalloutsAndTodos(t *testing.T) {
md := notionAsides("Intro\n\n<aside>\n⚠️ Mind the gap\n\n</aside>\n\nAfter\n")
doc, err := FromMarkdown(md)
if err != nil {
t.Fatal(err)
}
if j := asJSON(t, doc); !strings.Contains(j, `{"type":"callout","attrs":{"color":"blue","emoji":"⚠️"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Mind the gap"}]}]}`) {
t.Errorf("aside = %s", j)
}
doc, err = FromHTML(`<figure class="callout"><div style="font-size:1.5em"><span class="icon">📌</span></div><div style="width:100%"><p>Pinned</p></div></figure>` +
`<ul class="to-do-list"><li><div class="checkbox checkbox-on"></div> <span class="to-do-children-checked">Bought milk</span></li>` +
`<li><div class="checkbox checkbox-off"></div> <span>Eggs</span></li></ul>`)
if err != nil {
t.Fatal(err)
}
j := asJSON(t, doc)
for _, want := range []string{
`{"type":"callout","attrs":{"color":"blue","emoji":"📌"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Pinned"}]}]}`,
`{"type":"taskItem","attrs":{"checked":true},"content":[{"type":"paragraph","content":[{"type":"text","text":"Bought milk"}]}]}`,
`{"type":"taskItem","attrs":{"checked":false}`,
} {
if !strings.Contains(j, want) {
t.Errorf("notion html lacks %s:\n%s", want, j)
}
}
}
func TestLeadingEmoji(t *testing.T) {
for in, want := range map[string]string{
"💡 idea": "💡",
"⚠️ careful": "⚠️",
"👩‍💻 dev": "👩‍💻",
"🇵🇱 flag": "🇵🇱",
"plain": "",
"💡nospace": "",
} {
if got, _ := leadingEmoji(in); got != want {
t.Errorf("leadingEmoji(%q) = %q, want %q", in, got, want)
}
}
}
func TestPhaseABlocksPDF(t *testing.T) {
out, err := PDF("Blocks", mustParse(t, phaseA), PDFOptions{})
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(out, []byte("%PDF-")) {
t.Fatal("not a PDF")
}
}
func TestNearestPalette(t *testing.T) {
for in, want := range map[string]string{"FF0000": "red", "0070C0": "blue", "000000": "", "auto": "", "FFFFFF": "", "00B050": "green"} {
if got := nearestPalette(in); got != want {
t.Errorf("nearestPalette(%q) = %q, want %q", in, got, want)
}
}
}
func TestDOCXRoundTrip(t *testing.T) {
src := mustParse(t, phaseA)
data, err := DOCX("Blocks", src, DOCXOptions{})
if err != nil {
t.Fatal(err)
}
doc, _, err := FromDOCX(data)
if err != nil {
t.Fatal(err)
}
title, body := Title(doc)
if title != "Blocks" {
t.Errorf("title = %q", title)
}
if got, want := Markdown(body, Options{}), Markdown(src, Options{}); got != want {
t.Errorf("word round trip:\n%s\nwant\n%s", got, want)
}
j := asJSON(t, body)
for _, want := range []string{
`"textAlign":"right"`, `"textAlign":"center"`,
`{"type":"textColor","attrs":{"color":"red"}}`, `{"type":"highlight","attrs":{"color":"green"}}`,
`{"type":"callout","attrs":{"color":"red","emoji":"🔥"}`, `"type":"tableHeader"`,
} {
if !strings.Contains(j, want) {
t.Errorf("word import lacks %s:\n%s", want, j)
}
}
}
func TestDOCXListsCodeLinksAndImages(t *testing.T) {
src := mustParse(t, sample)
data, err := DOCX("", src, DOCXOptions{
Options: Options{FileURL: func(p string) string { return "https://api.docwell.test" + p }},
Image: func(string) ([]byte, error) { return pngOf(t, 4, 2), nil },
})
if err != nil {
t.Fatal(err)
}
doc, assets, err := FromDOCX(data)
if err != nil {
t.Fatal(err)
}
md := Markdown(doc, Options{})
for _, want := range []string{
`## Plan & \<scope\>`,
"[a link](https://docwell.test/x)",
"- one\n\n 1. nested\n- two",
"```\nx := 1 < 2\n```",
"> quoted",
"![diagram](asset:0)",
} {
if !strings.Contains(md, want) {
t.Errorf("markdown lacks %q:\n%s", want, md)
}
}
if strings.Contains(md, "javascript:") {
t.Error("unsafe link kept")
}
if len(assets) != 1 {
t.Errorf("assets = %d", len(assets))
}
}
func TestDOCXIsWellFormed(t *testing.T) {
data, err := DOCX("A & <b>", mustParse(t, phaseA), DOCXOptions{})
if err != nil {
t.Fatal(err)
}
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatal(err)
}
for _, f := range zr.File {
if !strings.HasSuffix(f.Name, ".xml") && !strings.HasSuffix(f.Name, ".rels") {
continue
}
rc, _ := f.Open()
d := xml.NewDecoder(rc)
for {
if _, err := d.Token(); err != nil {
if !errors.Is(err, io.EOF) {
t.Errorf("%s: %v", f.Name, err)
}
break
}
}
_ = rc.Close()
}
}