Files
Docwell/Server/internal/convert/suggest.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

100 lines
2.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"html"
"strconv"
"strings"
xhtml "golang.org/x/net/html"
)
// Suggestions (Packages/editor/src/suggest.ts) are two marks on text:
// suggestInsert on text someone proposes to add and suggestDelete on text
// they propose to remove. HTML keeps them as <ins>/<del> with their
// details, so a page exported and imported again keeps its open
// suggestions. Markdown, Word and PDF show the page as it stands until
// someone accepts them: proposed text left out, proposed deletions kept.
const (
markSuggestInsert = "suggestInsert"
markSuggestDelete = "suggestDelete"
)
// withoutSuggestions is the document with its open suggestions not taken:
// inserted text dropped and deletion marks taken off.
func withoutSuggestions(n Node) Node {
if len(n.Content) == 0 {
return n
}
out := make([]Node, 0, len(n.Content))
for _, c := range n.Content {
if hasMark(c, markSuggestInsert) {
continue
}
if hasMark(c, markSuggestDelete) {
c.Marks = withoutMark(c.Marks, markSuggestDelete)
}
out = append(out, withoutSuggestions(c))
}
n.Content = out
return n
}
func hasMark(n Node, typ string) bool {
for _, m := range n.Marks {
if m.Type == typ {
return true
}
}
return false
}
func withoutMark(marks []Mark, typ string) []Mark {
var out []Mark
for _, m := range marks {
if m.Type != typ {
out = append(out, m)
}
}
return out
}
// suggestionTags are the opening and closing tags of a suggestion mark.
func suggestionTags(m Mark) (string, string) {
tag, class := "ins", "dw-suggest-ins"
if m.Type == markSuggestDelete {
tag, class = "del", "dw-suggest-del"
}
open := "<" + tag + " class=\"" + class + "\" data-suggestion=\"" + html.EscapeString(m.attr("id")) +
"\" data-author=\"" + html.EscapeString(m.attr("author")) + "\" data-name=\"" + html.EscapeString(m.attr("name")) +
"\" data-at=\"" + suggestionTime(m) + "\">"
return open, "</" + tag + ">"
}
// suggestionMark reads a suggestion from an <ins> or <del> with data-suggestion.
func suggestionMark(n *xhtml.Node, typ string) (Mark, bool) {
id := attrOf(n, "data-suggestion")
if strings.TrimSpace(id) == "" {
return Mark{}, false
}
at, _ := strconv.ParseFloat(attrOf(n, "data-at"), 64)
return Mark{Type: typ, Attrs: map[string]any{
"id": id, "author": attrOf(n, "data-author"), "name": attrOf(n, "data-name"), "at": at,
}}, true
}
// suggestionTime is when a suggestion was made, in milliseconds, as text.
func suggestionTime(m Mark) string {
switch v := m.Attrs["at"].(type) {
case float64:
return strconv.FormatFloat(v, 'f', 0, 64)
case string:
if f, err := strconv.ParseFloat(v, 64); err == nil {
return strconv.FormatFloat(f, 'f', 0, 64)
}
}
return "0"
}