// Copied from Equipo's EquipoKit (oficina/equipo 00d09aa7, // native/apple/Sources/EquipoKit/Markdown/MarkdownInput.swift). Base's changes are // marked "Base:"; keep the rest in step with Equipo's. import Foundation import cmark_gfm /// Goldmark's delimiter classification includes Unicode symbols and NEL. /// Adapt only the inline parser's private block buffer, preserving byte widths. /// All projected text and targets come from the retained original block buffer. enum MarkdownInput { struct Prepared { let source: String let original: String let preparedLines: [String] let originalLines: [String] init(source: String, original: String) { self.source = source; self.original = original preparedLines = source.components(separatedBy: "\n") originalLines = original.components(separatedBy: "\n") } func block(_ bytes: [UInt8], startLine: Int) -> [UInt8] { var result: [UInt8] = [] var line = max(0, startLine - 1) for part in bytes.split(separator: 10, omittingEmptySubsequences: false).dropLast(bytes.last == 10 ? 1 : 0) { // Block content has one entry for each physical LF line, // including empty lines. Never search into a later block. if line < preparedLines.count { let candidate = preparedLines[line] var suffix = part[...] var restored: [UInt8]? while !suffix.isEmpty { let wanted = String(decoding: suffix, as: UTF8.self) if let range = candidate.range(of: wanted, options: .backwards) { let offset = candidate[.. Prepared { var bytes = Array(source.utf8) // Goldmark splits lines on LF and treats preceding CRs as whitespace. // cmark also splits lone CRs; preserve widths while preventing an extra // CR before CRLF from inventing a blank line or a loose list. for i in bytes.indices where bytes[i] == 13 { if i + 1 < bytes.count, bytes[i + 1] != 10 { bytes[i] = 32 } } // The reference-definition block parser runs before the inline phase. // Adapt angle destinations here too; the retained block restores bytes. if let placeholder = (Array(UInt8(1)...UInt8(8)) + [127]).first(where: { !bytes.contains($0) }) { var i = 0 while i + 2 < bytes.count { if bytes[i] == 93, bytes[i+1] == 40 || bytes[i+1] == 58 { var j = i + 2 while j < bytes.count, [32,9,10,13].contains(bytes[j]) { j += 1 } if j < bytes.count, bytes[j] == 60 { j += 1 while j < bytes.count, bytes[j] != 62, bytes[j] != 10 { if bytes[j] == 60 { bytes[j] = placeholder } j += 1 } } } i += 1 } } return Prepared(source: String(decoding: bytes, as: UTF8.self), original: source) } static func destinationForParser(_ value: String, angled: Bool) -> String { var bytes = forCMark(Array(value.utf8)) if angled { bytes = bytes.map { $0 == 60 ? 1 : $0 } } var buffer = cmark_strbuf() cmark_strbuf_init(cmark_get_default_mem_allocator(), &buffer, 0) defer { cmark_strbuf_free(&buffer) } bytes.withUnsafeBufferPointer { houdini_unescape_html_f(&buffer, $0.baseAddress, Int32($0.count)) } cmark_strbuf_unescape(&buffer) return String(decoding: UnsafeBufferPointer(start: buffer.ptr, count: Int(buffer.size)), as: UTF8.self) } static func forCMark(_ bytes: [UInt8]) -> [UInt8] { var out: [UInt8] = [] for scalar in String(decoding: bytes, as: UTF8.self).unicodeScalars { let replacement: String if scalar.value == 13 { replacement = " " } else if scalar.value == 0x85 { replacement = "\u{a0}" } else if scalar.value == 0x2028 || scalar.value == 0x2029 { replacement = "\u{2000}" } else if scalar.value == 11 { replacement = "\u{000C}" } else if scalar.value > 127, CharacterSet.punctuationCharacters.union(.symbols).contains(scalar), cmark_utf8proc_is_punctuation(Int32(scalar.value)) == 0 { switch scalar.utf8.count { case 2: replacement = "¡" case 3: replacement = "‼" default: replacement = "\u{10100}" } } else { replacement = String(scalar) } out.append(contentsOf: replacement.utf8) } // Goldmark allows a '<' inside an angle destination. cmark rejects it. // A same-width placeholder lets cmark recognize the link structure; // the original destination is retained and validated before linking. var at = 0 while at + 2 < out.count { if out[at] == 93, out[at + 1] == 40 || out[at + 1] == 58 { var i = at + 2 while i < out.count, [32, 9, 10, 13].contains(out[i]) { i += 1 } if i < out.count, out[i] == 60 { i += 1 while i < out.count, out[i] != 62, out[i] != 10 { if out[i] == 60 { out[i] = 1 } i += 1 } } } at += 1 } // cmark's inline backtick cache has 80 slots. Preserve source widths // while presenting longer matching runs as an 80-tick delimiter. var runs: [(start: Int, end: Int)] = [] var cursor = 0 while cursor < out.count { guard out[cursor] == 96 else { cursor += 1; continue } let start = cursor while cursor < out.count, out[cursor] == 96 { cursor += 1 } runs.append((start, cursor)) } var next: [Int: Int] = [:], matching: [Int: Int] = [:] for index in runs.indices.reversed() { let length = runs[index].end - runs[index].start matching[index] = next[length] next[length] = index } var index = 0 while index < runs.count { let run = runs[index] var before = run.start while before > 0, out[before - 1] == 92 { before -= 1 } guard (run.start - before) % 2 == 0, let close = matching[index] else { index += 1; continue } if run.end - run.start > 80 { for at in (run.start + 80)..