Files
Docwell/Server/internal/convert/columns.go
T
tajniak81andClaude Opus 5.5 c98b127f1e
CI / go (Media) (push) Successful in 13m40s
CI / go (Server) (push) Failing after 23m20s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 10m35s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Failing after 3h4m42s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / mobile (push) Failing after 9m19s
CI / docker (push) Successful in 2h2m57s
Add columns, suggesting mode, verified pages, scanned PDF search and server translations
- Columns: /columns puts 2 to 4 columns side by side, with widths dragged
  between them; they stack on phones. HTML export and import keep them
  (also Notion column lists and Confluence page layouts).
- Suggesting mode: edits become suggested insertions and deletions that
  editors accept or reject one by one or all at once. Text only.
- Verified pages: editors verify a page for a while or without end; the
  verifier is notified when it runs out (migration 1764700000).
- Scanned PDFs attached to pages are read by AI for search where a
  workspace turns it on.
- The server writes error messages, emails, digests and phone
  notifications in English, Polish, German, French and Spanish; the apps
  send their language with each request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 08:38:08 +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 = 4
// 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
}