Files
Docwell/Server/internal/convert/columns.go
T
tajniak81andClaude Opus 5.5 e441880f2f
CI / desktop (ubuntu-latest) (push) Successful in 1h15m48s
CI / mobile (push) Failing after 3m6s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / go (Media) (push) Successful in 10m21s
CI / go (Server) (push) Failing after 19m16s
CI / go (Web-App) (push) Successful in 10m27s
CI / frontend (push) Failing after 7m25s
CI / docker (push) Successful in 46m48s
Add the block types Docmost has and Docwell lacked
The slash menu gains Video, Audio, Page break, Footnote, Tabs, Status,
Emoji, Subpages, Synced block and a fifth column size:
- Video and audio upload a file that plays on the page.
- Page breaks show as a labelled line and start a new page in print,
  Word and PDF.
- Footnotes are numbered in reading order and listed at the end of the
  page. Their notes hold bold, italic, code and links, with a plain copy
  kept for search.
- Tabs hold blocks on several named tabs; which one shows is each
  reader's own choice.
- Status badges are coloured words in the text.
- Typing ":" and a word picks an emoji, from the list the page icon
  picker uses, now kept in the editor package.
- Subpages lists the page's child pages, read afresh from the page tree.
- A synced block is a synced page that shows one block of another page,
  made from a copied block link.

Exports (HTML, Markdown, Word, PDF) and search handle every new block.
Page and space exports list a page's subpages. Labels are translated in
all five languages.

The phone toolbar gets Tabs, Footnote, Status, Emoji, Subpages and Page
break, and the app answers the editor's request for the page's
subpages. The bridge protocol goes to version 5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 17:37:32 +01:00

184 lines
4.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"fmt"
"regexp"
"strconv"
"strings"
"golang.org/x/net/html"
)
// Side-by-side columns (Packages/editor/src/columns.ts): a "columns" row of
// two to four "column" nodes, each with a width in percent or none for an
// equal share. Exports to Markdown, Word and PDF put the columns one after
// another; HTML keeps them side by side.
const maxColumns = 5
// newColumns is a row of columns around these bodies. Empty bodies are left
// out; fewer than two left are returned as plain blocks, and anything past
// the fourth joins the fourth.
func newColumns(bodies [][]Node, widths []float64) []Node {
type col struct {
body []Node
width float64
}
var cols []col
for i, b := range bodies {
if len(b) == 0 {
continue
}
w := 0.0
if i < len(widths) {
w = widths[i]
}
cols = append(cols, col{b, w})
}
if len(cols) < 2 {
var out []Node
for _, c := range cols {
out = append(out, c.body...)
}
return out
}
if len(cols) > maxColumns {
last := &cols[maxColumns-1]
for _, c := range cols[maxColumns:] {
last.body = append(last.body, c.body...)
last.width += c.width
}
cols = cols[:maxColumns]
}
// Widths count only when every column has one; they are scaled to 100.
total := 0.0
for _, c := range cols {
if c.width <= 0 {
total = 0
break
}
total += c.width
}
row := Node{Type: "columns"}
for _, c := range cols {
n := Node{Type: "column", Attrs: map[string]any{"width": nil}, Content: c.body}
if total > 0 {
n.Attrs["width"] = float64(int(c.width*1000/total+0.5)) / 10
}
row.Content = append(row.Content, n)
}
return []Node{row}
}
// columnWidth is a column's width in percent, 0 when it has none.
func columnWidth(n Node) float64 {
switch v := n.Attrs["width"].(type) {
case float64:
if v > 0 && v <= 100 {
return v
}
case string:
if f, err := strconv.ParseFloat(v, 64); err == nil && f > 0 && f <= 100 {
return f
}
}
return 0
}
// writeHTMLColumns writes a row as a flex box that docwell's own import
// and editor read back.
func writeHTMLColumns(b *strings.Builder, n Node, o Options) {
b.WriteString("<div data-type=\"columns\" style=\"display:flex;gap:24px\">\n")
for _, c := range n.Content {
grow := 1.0
attr := ""
if w := columnWidth(c); w > 0 {
grow = w
attr = fmt.Sprintf(" data-width=\"%s\"", formatNumber(w))
}
fmt.Fprintf(b, "<div data-type=\"column\"%s style=\"flex:%s 1 0;min-width:0\">\n", attr, formatNumber(grow))
for _, child := range c.Content {
writeHTML(b, child, o)
}
b.WriteString("</div>\n")
}
b.WriteString("</div>\n")
}
var cssWidth = regexp.MustCompile(`(?i)(?:^|;)\s*(?:width|flex-basis)\s*:\s*([0-9.]+)%`)
// styleWidth is a percentage width from an element's style, 0 without one.
func styleWidth(n *html.Node) float64 {
m := cssWidth.FindStringSubmatch(attrOf(n, "style"))
if m == nil {
return 0
}
f, _ := strconv.ParseFloat(m[1], 64)
return f
}
// confluenceCellWidth is the share Confluence's page layouts give a cell:
// sidebars are narrow, the rest share what is left.
func confluenceCellWidth(cell *html.Node) float64 {
if hasClass(cell, "aside") || hasClass(cell, "sidebar") {
return 30
}
return 0
}
// readColumns reads a row of columns from HTML: Docwell's own
// (data-type="columns"), Notion's (div.column-list > div.column) and
// Confluence's page layouts (div.columnLayout > div.cell). ok is false for
// any other element.
func (r *reader) readColumns(n *html.Node) (out []Node, ok bool) {
var isCol func(*html.Node) bool
width := styleWidth
switch {
case attrOf(n, "data-type") == "columns":
isCol = func(c *html.Node) bool { return attrOf(c, "data-type") == "column" }
width = func(c *html.Node) float64 {
if f, err := strconv.ParseFloat(attrOf(c, "data-width"), 64); err == nil {
return f
}
return 0
}
case hasClass(n, "column-list"):
isCol = func(c *html.Node) bool { return hasClass(c, "column") }
case hasClass(n, "columnLayout"):
isCol = func(c *html.Node) bool { return hasClass(c, "cell") }
width = confluenceCellWidth
default:
return nil, false
}
var bodies [][]Node
var widths []float64
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode || !isCol(c) {
continue
}
bodies = append(bodies, r.blocks(c))
widths = append(widths, width(c))
}
// Sidebars have a width and the other cells none: they share the rest.
if hasClass(n, "columnLayout") {
fixed, open := 0.0, 0
for _, w := range widths {
if w > 0 {
fixed += w
} else {
open++
}
}
if fixed > 0 && open > 0 {
for i, w := range widths {
if w == 0 {
widths[i] = max(100-fixed, 10*float64(open)) / float64(open)
}
}
}
}
return newColumns(bodies, widths), true
}