Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraUI/Sources/Markdown/MarkdownModel.swift10// Module : Markdown11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Markdown9091// MARK: - Render model9293public indirect enum MDInline: Sendable, Hashable {94 case text(String)95 case emphasis([MDInline])96 case strong([MDInline])97 case strikethrough([MDInline])98 case code(String)99 case link(destination: String, children: [MDInline])100 case image(source: String, alt: String)101 case math(String, display: Bool)102 case lineBreak103 case softBreak104105 /// Text as a reader would copy it (math keeps its TeX source).106 public var plainText: String {107 switch self {108 case .text(let s), .code(let s): s109 case .emphasis(let c), .strong(let c), .strikethrough(let c): c.map(\.plainText).joined()110 case .link(_, let c): c.map(\.plainText).joined()111 case .image(_, let alt): alt112 case .math(let tex, let display): display ? "$$\(tex)$$" : "$\(tex)$"113 case .lineBreak: "\n"114 case .softBreak: " "115 }116 }117}118119public struct MDListItem: Sendable, Hashable {120 public var checkbox: Bool?121 public var blocks: [MDBlock]122}123124public struct MDTable: Sendable, Hashable {125 public enum Alignment: Sendable, Hashable { case leading, center, trailing }126 public var alignments: [Alignment]127 public var header: [[MDInline]]128 public var rows: [[[MDInline]]]129 public var columnCount: Int { max(header.count, rows.map(\.count).max() ?? 0) }130}131132public indirect enum MDBlock: Sendable, Hashable {133 case paragraph([MDInline])134 case heading(level: Int, [MDInline])135 case code(language: String?, text: String)136 case quote([MDBlock])137 case list(ordered: Bool, start: Int, items: [MDListItem])138 case table(MDTable)139 case rule140 case math(String)141 /// Raw HTML from the model is never interpreted: it is displayed as literal text.142 case html(String)143144 public var plainText: String {145 switch self {146 case .paragraph(let i), .heading(_, let i): i.map(\.plainText).joined()147 case .code(_, let t): t148 case .quote(let b): b.map(\.plainText).joined(separator: "\n")149 case .list(let ordered, let start, let items):150 items.enumerated().map { n, it in (ordered ? "\(start + n). " : "• ") + it.blocks.map(\.plainText).joined(separator: "\n") }.joined(separator: "\n")151 case .table(let t): ([t.header] + t.rows).map { $0.map { $0.map(\.plainText).joined() }.joined(separator: "\t") }.joined(separator: "\n")152 case .rule: "———"153 case .math(let tex): "$$\(tex)$$"154 case .html(let s): s155 }156 }157}158159/// One independently parsed slice of a message. While a message streams only its last chunk changes, so the views of160/// every earlier chunk are left untouched.161public struct MDChunk: Sendable, Hashable, Identifiable {162 public let id: Int163 public let source: String164 public let blocks: [MDBlock]165}166167// MARK: - Parser168169public final class MarkdownParser: @unchecked Sendable {170 public static let shared = MarkdownParser()171 private let lock = NSLock()172 private var cache: [String: [MDBlock]] = [:]173 private var order: [String] = []174 private let capacity: Int175176 public init(capacity: Int = 6000) { self.capacity = capacity }177178 /// Splits `source` into top-level chunks and parses each (cached by content).179 public func chunks(_ source: String) -> [MDChunk] {180 Self.split(source).enumerated().map { i, s in MDChunk(id: i, source: s, blocks: blocks(forChunk: s)) }181 }182183 /// Parses a whole document in one piece (exports, tests).184 public func parse(_ source: String) -> [MDBlock] { Self.parseUncached(source) }185186 private func blocks(forChunk s: String) -> [MDBlock] {187 if let hit = lock.withLock({ cache[s] }) { return hit }188 let parsed = Self.parseUncached(s)189 lock.withLock {190 if cache[s] == nil { order.append(s) }191 cache[s] = parsed192 if order.count > capacity { for k in order.prefix(capacity / 4) { cache[k] = nil }; order.removeFirst(capacity / 4) }193 }194 return parsed195 }196197 /// Cuts at blank lines that are outside fenced code and display math, and never between the lines of a list or a198 /// table (those keep their own blank-line semantics inside one chunk when they are contiguous).199 static func split(_ source: String) -> [String] {200 var chunks: [String] = []; var current: [Substring] = []201 var fence: (char: Character, count: Int)?; var inDisplayMath = false202 func flush() { let s = current.joined(separator: "\n"); if !s.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { chunks.append(s) }; current.removeAll(keepingCapacity: true) }203 for line in source.split(separator: "\n", omittingEmptySubsequences: false) {204 let trimmed = line.drop(while: { $0 == " " })205 if let f = fence {206 current.append(line)207 if trimmed.hasPrefix(String(repeating: f.char, count: f.count)), trimmed.allSatisfy({ $0 == f.char || $0 == " " }) { fence = nil }208 continue209 }210 if let c = trimmed.first, c == "`" || c == "~" {211 let n = trimmed.prefix(while: { $0 == c }).count212 if n >= 3, line.count - trimmed.count < 4 {213 // a fence that follows a list item stays with it; otherwise it starts its own chunk214 if let prev = current.last, prev.trimmingCharacters(in: .whitespaces).isEmpty { flush() }215 fence = (c, n); current.append(line); continue216 }217 }218 let dollars = line.components(separatedBy: "$$").count - 1219 if dollars % 2 == 1 { inDisplayMath.toggle() }220 if trimmed.hasPrefix("\\[") && !line.contains("\\]") { inDisplayMath = true }221 if inDisplayMath && line.contains("\\]") && !trimmed.hasPrefix("\\[") { inDisplayMath = false }222 if line.allSatisfy({ $0 == " " || $0 == "\t" }) && !inDisplayMath { flush() } else { current.append(line) }223 }224 flush()225 return chunks226 }227228 // MARK: Math protection229230 private static let open: Character = "\u{E000}", close: Character = "\u{E001}"231232 struct Protected { var text: String; var math: [(tex: String, display: Bool, raw: String)] }233234 /// Replaces TeX spans by private-use placeholders so that the Markdown parser cannot mangle `_`, `*` or `\\` in them.235 /// Code spans and fenced blocks are skipped. `$…$` follows the usual rule: no space just inside the delimiters and236 /// no digit right after the closing one (so "$5 and $10" stays text).237 static func protectMath(_ source: String) -> Protected {238 var out = ""; out.reserveCapacity(source.count)239 var math: [(String, Bool, String)] = []240 let s = Array(source); var i = 0; let n = s.count241 var atLineStart = true242 func emit(_ tex: String, _ display: Bool, _ raw: String) { out.append(open); out.append(String(math.count)); out.append(close); math.append((tex.trimmingCharacters(in: .whitespacesAndNewlines), display, raw)) }243 func find(_ needle: [Character], from: Int, sameLine: Bool) -> Int? {244 var j = from245 while j + needle.count <= n {246 if sameLine && s[j] == "\n" { return nil }247 if s[j] == "\\" && needle != ["\\", ")"] && needle != ["\\", "]"] { j += 2; continue }248 if Array(s[j..<(j + needle.count)]) == needle { return j }249 j += 1250 }251 return nil252 }253 while i < n {254 let c = s[i]255 if atLineStart {256 // fenced code block: copy verbatim up to the closing fence257 var k = i; while k < n, s[k] == " " { k += 1 }258 if k < n, s[k] == "`" || s[k] == "~", k - i < 4 {259 let fc = s[k]; var run = 0; while k + run < n, s[k + run] == fc { run += 1 }260 if run >= 3 {261 var j = k + run; var closed = false262 while j < n {263 guard let nl = s[j...].firstIndex(of: "\n") else { j = n; break }264 var p = nl + 1; while p < n, s[p] == " " { p += 1 }265 var r = 0; while p + r < n, s[p + r] == fc { r += 1 }266 if r >= run { var e = p + r; while e < n, s[e] != "\n" { e += 1 }; j = e; closed = true; break }267 j = nl + 1268 }269 _ = closed270 out.append(contentsOf: s[i..<j]); i = j; atLineStart = false; continue271 }272 }273 }274 atLineStart = c == "\n"275 if c == "`" { // inline code span: same-length backtick run closes it276 var run = 0; while i + run < n, s[i + run] == "`" { run += 1 }277 var j = i + run; var end: Int?278 while j < n { if s[j] == "`" { var r = 0; while j + r < n, s[j + r] == "`" { r += 1 }; if r == run { end = j + r; break }; j += r } else { j += 1 } }279 let stop = end ?? (i + run)280 out.append(contentsOf: s[i..<stop]); i = stop; continue281 }282 if c == "\\", i + 1 < n {283 if s[i + 1] == "[", let e = find(["\\", "]"], from: i + 2, sameLine: false) { emit(String(s[(i + 2)..<e]), true, String(s[i..<(e + 2)])); i = e + 2; continue }284 if s[i + 1] == "(", let e = find(["\\", ")"], from: i + 2, sameLine: false) { emit(String(s[(i + 2)..<e]), false, String(s[i..<(e + 2)])); i = e + 2; continue }285 out.append(c); out.append(s[i + 1]); i += 2; continue286 }287 if c == "$" {288 if i + 1 < n, s[i + 1] == "$" {289 if let e = find(["$", "$"], from: i + 2, sameLine: false) { emit(String(s[(i + 2)..<e]), true, String(s[i..<(e + 2)])); i = e + 2; continue }290 } else if i + 1 < n, !s[i + 1].isWhitespace, let e = find(["$"], from: i + 1, sameLine: true), e > i + 1, !s[e - 1].isWhitespace, !(e + 1 < n && s[e + 1].isNumber), !(i > 0 && s[i - 1].isNumber),291 !s[i + 1].isNumber || s[(i + 1)..<e].contains(where: { "\\^_{}=+*/<>".contains($0) }) || !s[(i + 1)..<e].contains(" ") {292 emit(String(s[(i + 1)..<e]), false, String(s[i...e])); i = e + 1; continue293 }294 }295 out.append(c); i += 1296 }297 return Protected(text: out, math: math)298 }299300 // MARK: Conversion301302 static func parseUncached(_ source: String) -> [MDBlock] {303 let protected = protectMath(source)304 let doc = Document(parsing: protected.text, options: [.disableSmartOpts])305 var conv = Converter(math: protected.math)306 return doc.children.flatMap { conv.block($0) }307 }308309 private struct Converter {310 let math: [(tex: String, display: Bool, raw: String)]311312 /// Puts the original TeX source back (inside code, where placeholders must not survive).313 func restore(_ s: String) -> String {314 guard s.contains(MarkdownParser.open) else { return s }315 var out = ""; var rest = Substring(s)316 while let o = rest.firstIndex(of: MarkdownParser.open), let c = rest[o...].firstIndex(of: MarkdownParser.close) {317 out += rest[..<o]318 if let idx = Int(rest[rest.index(after: o)..<c]), math.indices.contains(idx) { out += math[idx].raw }319 rest = rest[rest.index(after: c)...]320 }321 return out + rest322 }323324 func splitText(_ s: String) -> [MDInline] {325 guard s.contains(MarkdownParser.open) else { return s.isEmpty ? [] : [.text(s)] }326 var out: [MDInline] = []; var rest = Substring(s)327 while let o = rest.firstIndex(of: MarkdownParser.open), let c = rest[o...].firstIndex(of: MarkdownParser.close) {328 if o > rest.startIndex { out.append(.text(String(rest[..<o]))) }329 if let idx = Int(rest[rest.index(after: o)..<c]), math.indices.contains(idx) { out.append(.math(math[idx].tex, display: math[idx].display)) }330 rest = rest[rest.index(after: c)...]331 }332 if !rest.isEmpty { out.append(.text(String(rest))) }333 return out334 }335336 func inlines(_ node: Markup) -> [MDInline] { node.children.flatMap { inline($0) } }337338 func inline(_ node: Markup) -> [MDInline] {339 switch node {340 case let t as Markdown.Text: return splitText(t.string)341 case let e as Emphasis: return [.emphasis(inlines(e))]342 case let s as Strong: return [.strong(inlines(s))]343 case let s as Strikethrough: return [.strikethrough(inlines(s))]344 case let c as InlineCode: return [.code(restore(c.code))]345 case let l as Markdown.Link: return [.link(destination: restore(l.destination ?? ""), children: inlines(l))]346 case let i as Markdown.Image: return [.image(source: restore(i.source ?? ""), alt: restore(i.plainText))]347 case is LineBreak: return [.lineBreak]348 case is SoftBreak: return [.softBreak]349 case let h as InlineHTML: return [.text(restore(h.rawHTML))]350 case let s as SymbolLink: return [.code(s.destination ?? "")]351 default: return splitText(restore(node.format()))352 }353 }354355 mutating func block(_ node: Markup) -> [MDBlock] {356 switch node {357 case let p as Paragraph:358 // display math inside a paragraph becomes its own block359 let all = inlines(p); var out: [MDBlock] = []; var run: [MDInline] = []360 func trimmed(_ r: [MDInline]) -> [MDInline] {361 var r = r362 while let f = r.first, f == .softBreak || f == .lineBreak { r.removeFirst() }363 while let l = r.last, l == .softBreak || l == .lineBreak { r.removeLast() }364 return r365 }366 for i in all {367 if case .math(let tex, true) = i { let t = trimmed(run); if !t.isEmpty { out.append(.paragraph(t)) }; run = []; out.append(.math(tex)) } else { run.append(i) }368 }369 let t = trimmed(run); if !t.isEmpty { out.append(.paragraph(t)) }370 return out371 case let h as Heading: return [.heading(level: h.level, inlines(h))]372 case let c as CodeBlock:373 var code = restore(c.code); if code.hasSuffix("\n") { code.removeLast() }374 let lang = c.language?.split(separator: " ").first.map(String.init)375 return [.code(language: lang?.isEmpty == false ? lang : nil, text: code)]376 case let q as BlockQuote: var out: [MDBlock] = []; for c in q.children { out += block(c) }; return [.quote(out)]377 case let l as UnorderedList: return [.list(ordered: false, start: 1, items: items(l))]378 case let l as OrderedList: return [.list(ordered: true, start: Int(l.startIndex), items: items(l))]379 case let t as Markdown.Table:380 let aligns: [MDTable.Alignment] = t.columnAlignments.map { switch $0 { case .center: .center; case .right: .trailing; default: .leading } }381 var header: [[MDInline]] = []; for cell in t.head.cells { header.append(inlines(cell)) }382 var rows: [[[MDInline]]] = []383 for row in t.body.rows { var r: [[MDInline]] = []; for cell in row.cells { r.append(inlines(cell)) }; rows.append(r) }384 return [.table(MDTable(alignments: aligns, header: header, rows: rows))]385 case is ThematicBreak: return [.rule]386 case let h as HTMLBlock: return [.html(restore(h.rawHTML).trimmingCharacters(in: .newlines))]387 default:388 let text = restore(node.format()).trimmingCharacters(in: .whitespacesAndNewlines)389 return text.isEmpty ? [] : [.paragraph([.text(text)])]390 }391 }392393 mutating func items(_ list: Markup) -> [MDListItem] {394 var out: [MDListItem] = []395 for child in list.children {396 guard let li = child as? ListItem else { continue }397 var blocks: [MDBlock] = []; for c in li.children { blocks += block(c) }398 out.append(MDListItem(checkbox: li.checkbox.map { $0 == .checked }, blocks: blocks))399 }400 return out401 }402 }403}404405/// Which links may be opened from model output. Anything else (javascript:, file:, custom schemes) is inert text.406public enum LinkPolicy {407 public static func safeURL(_ destination: String) -> URL? {408 guard let url = URL(string: destination.trimmingCharacters(in: .whitespaces)), let scheme = url.scheme?.lowercased() else { return nil }409 guard ["http", "https", "mailto"].contains(scheme) else { return nil }410 if scheme != "mailto", (url.host ?? "").isEmpty { return nil }411 return url412 }413}414