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>
231 lines
6.1 KiB
Go
231 lines
6.1 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package convert
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// The palette colours of text, highlights and callouts (Packages/editor
|
|
// colors.ts). Documents store the name; files that need a value get the
|
|
// light theme's.
|
|
var paletteText = map[string]string{
|
|
"gray": "#6b6f6d", "red": "#c0392b", "orange": "#c2571a", "yellow": "#9a7400", "green": "#3d7a1f",
|
|
"teal": "#1f7f79", "blue": "#2f6fbf", "purple": "#7c5cc4", "pink": "#b8327a",
|
|
}
|
|
|
|
var paletteBackground = map[string]string{
|
|
"gray": "#ecece9", "red": "#fbe3df", "orange": "#fbe7d7", "yellow": "#fbf1c7", "green": "#e4f1d8",
|
|
"teal": "#d9efec", "blue": "#dfe9f7", "purple": "#ebe4f7", "pink": "#f8e0ec",
|
|
}
|
|
|
|
func isPaletteColor(c string) bool {
|
|
_, ok := paletteText[c]
|
|
return ok
|
|
}
|
|
|
|
// rgb reads a "#rrggbb" colour.
|
|
func rgb(hex string) (r, g, b int) {
|
|
v, err := strconv.ParseUint(strings.TrimPrefix(hex, "#"), 16, 32)
|
|
if err != nil || len(hex) != 7 {
|
|
return 0, 0, 0
|
|
}
|
|
return int(v >> 16 & 0xff), int(v >> 8 & 0xff), int(v & 0xff)
|
|
}
|
|
|
|
// nearestPalette maps a "rrggbb" colour from another program to the
|
|
// palette colour of its hue, or "" for black, white and near-greys, which
|
|
// stay plain text (dark greys are usually just text).
|
|
func nearestPalette(hex string) string {
|
|
if len(strings.TrimPrefix(hex, "#")) != 6 {
|
|
return ""
|
|
}
|
|
r, g, b := rgb("#" + strings.TrimPrefix(strings.ToLower(hex), "#"))
|
|
hi, lo := max(r, g, b), min(r, g, b)
|
|
switch {
|
|
case hi < 60 || lo > 235:
|
|
return ""
|
|
case hi-lo < 40:
|
|
if hi > 90 && hi < 200 {
|
|
return "gray"
|
|
}
|
|
return ""
|
|
}
|
|
var hue float64
|
|
d := float64(hi - lo)
|
|
switch hi {
|
|
case r:
|
|
hue = 60 * float64(g-b) / d
|
|
case g:
|
|
hue = 60*float64(b-r)/d + 120
|
|
default:
|
|
hue = 60*float64(r-g)/d + 240
|
|
}
|
|
if hue < 0 {
|
|
hue += 360
|
|
}
|
|
for _, band := range []struct {
|
|
upTo float64
|
|
name string
|
|
}{{15, "red"}, {40, "orange"}, {70, "yellow"}, {160, "green"}, {200, "teal"}, {250, "blue"}, {290, "purple"}, {345, "pink"}} {
|
|
if hue < band.upTo {
|
|
return band.name
|
|
}
|
|
}
|
|
return "red"
|
|
}
|
|
|
|
// ---- Callouts --------------------------------------------------------------
|
|
|
|
// alert is a GitHub Markdown alert ("> [!NOTE]"), which callouts become in
|
|
// Markdown, with the colour and emoji a callout read from one gets.
|
|
type alert struct{ name, color, emoji string }
|
|
|
|
var alerts = []alert{
|
|
{"NOTE", "blue", "💡"}, {"TIP", "green", "✅"}, {"IMPORTANT", "purple", "❗"},
|
|
{"WARNING", "yellow", "⚠️"}, {"CAUTION", "red", "🚫"},
|
|
}
|
|
|
|
// alertFor is the alert closest to a callout colour.
|
|
func alertFor(color string) alert {
|
|
switch color {
|
|
case "green":
|
|
return alerts[1]
|
|
case "purple", "pink":
|
|
return alerts[2]
|
|
case "yellow", "orange":
|
|
return alerts[3]
|
|
case "red":
|
|
return alerts[4]
|
|
}
|
|
return alerts[0]
|
|
}
|
|
|
|
var alertMarker = regexp.MustCompile(`^\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*`)
|
|
|
|
func alertNamed(name string) alert {
|
|
for _, a := range alerts {
|
|
if a.name == strings.ToUpper(name) {
|
|
return a
|
|
}
|
|
}
|
|
return alerts[0]
|
|
}
|
|
|
|
// leadingEmoji splits an emoji, and the space after it, off the start of s.
|
|
// emoji is "" when s does not start with one followed by a space.
|
|
func leadingEmoji(s string) (emoji, rest string) {
|
|
i, joined := 0, true // joined: the next rune may start the emoji, or continue it after a ZWJ
|
|
for i < len(s) {
|
|
r, size := utf8.DecodeRuneInString(s[i:])
|
|
switch {
|
|
case joined && isEmojiBase(r):
|
|
joined = false
|
|
case i > 0 && (r == 0xFE0F || (r >= 0x1F3FB && r <= 0x1F3FF) || r == 0x20E3 || (r >= 0x1F1E6 && r <= 0x1F1FF)):
|
|
// Variation selectors, skin tones, keycaps and the second letter of a flag.
|
|
case i > 0 && r == 0x200D:
|
|
joined = true
|
|
case i > 0 && !joined && (r == 0x20 || r == 0xA0):
|
|
return s[:i], s[i+size:]
|
|
default:
|
|
return "", s
|
|
}
|
|
i += size
|
|
}
|
|
if i == 0 || joined {
|
|
return "", s
|
|
}
|
|
return s, ""
|
|
}
|
|
|
|
func isEmojiBase(r rune) bool {
|
|
return r >= 0x1F000 || (r >= 0x2190 && r <= 0x2BFF) || r == 0x2705 || unicode.Is(unicode.So, r)
|
|
}
|
|
|
|
// calloutParts is a callout's emoji and colour, with the defaults filled in.
|
|
func calloutParts(n Node) (emoji, color string) {
|
|
emoji, color = n.attr("emoji"), n.attr("color")
|
|
if !isPaletteColor(color) {
|
|
color = "blue"
|
|
}
|
|
if emoji == "" {
|
|
emoji = "💡"
|
|
}
|
|
return emoji, color
|
|
}
|
|
|
|
// newCallout is a callout node around body, which gets an empty paragraph
|
|
// when it has nothing.
|
|
func newCallout(emoji, color string, body []Node) Node {
|
|
if emoji == "" {
|
|
emoji = "💡"
|
|
}
|
|
if !isPaletteColor(color) {
|
|
color = "blue"
|
|
}
|
|
if len(body) == 0 {
|
|
body = []Node{{Type: "paragraph"}}
|
|
}
|
|
return Node{Type: "callout", Attrs: map[string]any{"emoji": emoji, "color": color}, Content: body}
|
|
}
|
|
|
|
// liftEmoji takes an emoji off the start of the first paragraph of body, as
|
|
// callouts imported from Markdown and Notion carry their icon there.
|
|
func liftEmoji(body []Node) (string, []Node) {
|
|
if len(body) == 0 || body[0].Type != "paragraph" || len(body[0].Content) == 0 || body[0].Content[0].Type != "text" {
|
|
return "", body
|
|
}
|
|
emoji, rest := leadingEmoji(body[0].Content[0].Text)
|
|
if emoji == "" {
|
|
return "", body
|
|
}
|
|
p := body[0]
|
|
p.Content = append([]Node{}, p.Content...)
|
|
p.Content[0].Text = rest
|
|
if rest == "" {
|
|
p.Content = p.Content[1:]
|
|
}
|
|
out := append([]Node{}, body...)
|
|
if len(p.Content) == 0 && len(out) > 1 {
|
|
return emoji, out[1:]
|
|
}
|
|
out[0] = p
|
|
return emoji, out
|
|
}
|
|
|
|
// ---- Tables ----------------------------------------------------------------
|
|
|
|
// cellText is a table cell's content on one line, for Markdown.
|
|
func (w *mdWriter) cellText(cell Node) string {
|
|
var parts []string
|
|
for _, b := range cell.Content {
|
|
var s string
|
|
switch b.Type {
|
|
case "paragraph", "heading":
|
|
s = w.inline(b.Content)
|
|
default:
|
|
s = escapeMD(PlainText(b))
|
|
}
|
|
s = strings.TrimSpace(strings.NewReplacer("\\\n", " ", "\n", " ").Replace(s))
|
|
if s != "" {
|
|
parts = append(parts, s)
|
|
}
|
|
}
|
|
return strings.Join(parts, " ")
|
|
}
|
|
|
|
// alignOf is a text block's alignment when it is not the default.
|
|
func alignOf(n Node) string {
|
|
switch a := n.attr("textAlign"); a {
|
|
case "center", "right":
|
|
return a
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var cssAlign = regexp.MustCompile(`(?i)text-align\s*:\s*(center|right)`)
|