Repository Public
1// SP Notes Core2// File: Sources/SPNotesDocument/MarkdownExport.swift3// Description: ProseMirror JSON to Markdown, a line-by-line port of the server's toMarkdown (text.ts) so that both4// sides produce the same bytes.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910enum MarkdownExport {11 static func inline(_ n: PMNode) -> String {12 if n.type == "text" {13 var t = n.text ?? ""14 for m in n.allMarks {15 switch m.type {16 case "bold": t = "**\(t)**"17 case "italic": t = "*\(t)*"18 case "code": t = "`\(t)`"19 case "strike": t = "~~\(t)~~"20 case "link": t = "[\(t)](\(JS.string(m.attrs?["href"])))"21 case "highlight": t = "==\(t)=="22 // server arbitration (3.0): the three marks Markdown has no syntax for are written as inline HTML23 case "underline": t = "<u>\(t)</u>"24 case "subscript": t = "<sub>\(t)</sub>"25 case "superscript": t = "<sup>\(t)</sup>"26 default: break27 }28 }29 return t30 }31 switch n.type {32 case "hardBreak": return " \n"33 case "mention": return "[[\(JS.string(JS.nullish(n.attrs?["label"]) ?? n.attrs?["id"]))]]"34 case "inlineMath", "math": return "$\(JS.nullish(n.attrs?["latex"]).map(JS.string) ?? "")$"35 case "footnoteRef": return "[^\(JS.string(n.attrs?["id"]))]"36 case "citation": return NextNodes.markdown(n) ?? ""37 default: return (n.content ?? []).map(inline).joined()38 }39 }4041 /// `children.map(block).join("").split("\n").filter(Boolean).map(l => "> " + l).join("\n") + "\n"`.42 private static func quoted(_ children: [PMNode]?) -> String {43 JS.splitLines((children ?? []).map { block($0) }.joined()).filter { !$0.isEmpty }.map { "> \($0)" }.joined(separator: "\n") + "\n"44 }4546 private static func item(_ li: PMNode, depth: Int) -> String {47 (li.content ?? []).enumerated().map { i, c in i == 0 ? JS.trimStart(block(c, depth + 1)) : block(c, depth + 1) }.joined()48 }4950 static func block(_ n: PMNode, _ depth: Int = 0) -> String {51 let pad = String(repeating: " ", count: depth)52 let a = n.attrs53 switch n.type {54 case "paragraph": return inline(n) + "\n"55 case "heading":56 let level = JS.nullish(a?["level"]).map(JS.number) ?? 157 let count = level.isFinite && level > 0 ? Int(min(level, 1000)) : 058 return "\(String(repeating: "#", count: count)) \(inline(n))\n"59 case "blockquote": return quoted(n.content)60 case "codeBlock": return "```\(JS.nullish(a?["language"]).map(JS.string) ?? "")\n\(TextTools.nodeText(n))\n```\n"61 case "bulletList": return (n.content ?? []).map { "\(pad)- \(item($0, depth: depth))" }.joined()62 case "orderedList": return (n.content ?? []).enumerated().map { i, li in "\(pad)\(i + 1). \(item(li, depth: depth))" }.joined()63 case "taskList":64 return (n.content ?? []).map { "\(pad)- [\(JS.truthy($0.attrs?["checked"]) ? "x" : " ")] \(item($0, depth: depth))" }.joined()65 case "table":66 let rows = (n.content ?? []).map { r in67 (r.content ?? []).map { c in JS.replacing(inline(c).replacingOccurrences(of: "|", with: "\\|"), "\n", with: " ") }68 }69 guard let head = rows.first else { return "" }70 let body = rows.dropFirst().map { "| \($0.joined(separator: " | ")) |" }.joined(separator: "\n")71 return "| \(head.joined(separator: " | ")) |\n| \(head.map { _ in "---" }.joined(separator: " | ")) |\n\(body)\n"72 case "horizontalRule": return "---\n"73 case "image": return "![\(JS.nullish(a?["alt"]).map(JS.string) ?? "")](\(JS.string(a?["src"])))\n"74 case "fileBlock": return "[📎 \(JS.string(a?["name"]))](\(JS.string(a?["src"])))\n"75 case "youtube": return "\(JS.string(a?["src"]))\n"76 case "blockMath": return "$$\n\(JS.nullish(a?["latex"]).map(JS.string) ?? "")\n$$\n"77 case "callout":78 let kind = (JS.nullish(a?["kind"]).map(JS.string) ?? "note").uppercased()79 return "> [!\(kind)] \(JS.nullish(a?["icon"]).map(JS.string) ?? "")\n" + quoted(n.content)80 case "details":81 let s = n.content?.first { $0.type == "detailsSummary" }82 let b = n.content?.first { $0.type == "detailsContent" }83 let body = b.map { ($0.content ?? []).map { block($0) }.joined(separator: "\n") } ?? ""84 return "<details><summary>\(s.map(inline) ?? "")</summary>\n\n\(body)\n</details>\n"85 case "aiBlock": return "> [!AI] \(JS.nullish(a?["model"]).map(JS.string) ?? "")\n" + quoted(n.content)86 case "footnote": return "[^\(JS.string(a?["id"]))]: \((n.content ?? []).map(inline).joined(separator: " "))\n"87 default:88 if let next = NextNodes.markdown(n) { return next }89 return (n.content ?? []).map { block($0, depth) }.joined()90 }91 }9293 static func markdown(_ doc: PMNode) -> String {94 JS.trim(JS.collapseNewlines((doc.content ?? []).map { block($0) }.joined(separator: "\n"))) + "\n"95 }96}97