Files
Docwell/Server/internal/convert/blocks_test.go
T
tajniak81andClaude Opus 5.5 d28f1e02db Add tables, to-dos, callouts and the rest of editor parity
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>
2026-11-14 12:20:08 +01:00

154 lines
5.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"bytes"
"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)
}
}
}