Files
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

127 lines
3.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Package convert turns page documents (Tiptap/ProseMirror JSON in the Docwell
// schema, see Packages/editor) into Markdown and HTML, and Markdown and HTML
// back into documents. Imports keep only what the schema knows, which also
// drops scripts, styles and event handlers from pasted or uploaded HTML.
package convert
import "encoding/json"
// Node is one ProseMirror node or text run.
type Node struct {
Type string `json:"type"`
Attrs map[string]any `json:"attrs,omitempty"`
Content []Node `json:"content,omitempty"`
Text string `json:"text,omitempty"`
Marks []Mark `json:"marks,omitempty"`
}
type Mark struct {
Type string `json:"type"`
Attrs map[string]any `json:"attrs,omitempty"`
}
// Parse reads a document; null or empty input is an empty document.
func Parse(raw json.RawMessage) (Node, error) {
doc := Node{Type: "doc"}
if len(raw) == 0 || string(raw) == "null" {
return doc, nil
}
err := json.Unmarshal(raw, &doc)
return doc, err
}
func (n Node) attr(key string) string {
switch v := n.Attrs[key].(type) {
case string:
return v
case float64:
return json.Number(formatNumber(v)).String()
}
return ""
}
// card is the address and title of a block that links somewhere: an embed,
// a link card, or a synced page (shown as a link to the page it shows).
func (n Node) card() (href, title string) {
if n.Type == "syncedPage" {
href = "/p/" + n.attr("page")
title = n.attr("title")
if icon := n.attr("icon"); icon != "" && title != "" {
title = icon + " " + title
}
// A synced block shows one block of the page and links to it.
block := n.attr("block")
if block != "" {
href += "#b-" + block
}
if title == "" {
title = "Synced page"
if block != "" {
title = "Synced block"
}
}
return href, title
}
href, title = n.attr("url"), n.attr("title")
if title == "" {
title = href
}
return href, title
}
func (n Node) intAttr(key string, def int) int {
if v, ok := n.Attrs[key].(float64); ok {
return int(v)
}
return def
}
func (m Mark) attr(key string) string {
if v, ok := m.Attrs[key].(string); ok {
return v
}
return ""
}
func formatNumber(f float64) string {
b, _ := json.Marshal(f)
return string(b)
}
// Title takes a leading level-1 heading as the page title and returns the
// document without it; title is "" when the document does not start with one.
func Title(doc Node) (string, Node) {
if len(doc.Content) == 0 {
return "", doc
}
first := doc.Content[0]
if first.Type != "heading" || first.intAttr("level", 1) != 1 {
return "", doc
}
doc.Content = doc.Content[1:]
return PlainText(first), doc
}
// PlainText is the node's text with no formatting.
func PlainText(n Node) string {
switch n.Type {
case "text":
return n.Text
case "status":
return n.attr("text")
case "footnote":
// Search finds a page by its footnotes too.
if t := n.attr("text"); t != "" {
return " (" + t + ")"
}
return ""
}
var s string
for _, c := range n.Content {
s += PlainText(c)
}
return s
}