Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Markdown/MarkdownEditing.swift3// Description: Pure text edits of the note editor's formatting commands (bold, italic, code, line prefixes), on UTF-16 ranges as AppKit text views use them.4import Foundation56public enum MarkdownEditing {7 public struct Edit: Sendable, Equatable {8 /// The range of the original text to replace, its replacement, and the selection afterwards.9 public var range: NSRange10 public var replacement: String11 public var selection: NSRange12 }1314 /// Wraps the selection in `marker` (`**`, `*`, `` ` ``), or unwraps it when it already is — whether the markers15 /// are inside the selection or just around it. An empty selection gets the two markers with the caret between.16 public static func toggle(_ marker: String, in text: String, selection: NSRange) -> Edit {17 let ns = text as NSString18 let m = (marker as NSString).length19 let sel = NSIntersectionRange(selection, NSRange(location: 0, length: ns.length)).length == selection.length && selection.location <= ns.length ? selection : NSRange(location: ns.length, length: 0)20 let inner = ns.substring(with: sel)21 // markers just outside the selection22 if sel.location >= m, sel.location + sel.length + m <= ns.length,23 ns.substring(with: NSRange(location: sel.location - m, length: m)) == marker, ns.substring(with: NSRange(location: sel.location + sel.length, length: m)) == marker,24 !(marker == "*" && isDoubled(ns, at: sel.location - m, before: true)) {25 return Edit(range: NSRange(location: sel.location - m, length: sel.length + 2 * m), replacement: inner, selection: NSRange(location: sel.location - m, length: sel.length))26 }27 // markers inside the selection28 if sel.length >= 2 * m, inner.hasPrefix(marker), inner.hasSuffix(marker), !(marker == "*" && inner.hasPrefix("**")) {29 let core = (inner as NSString).substring(with: NSRange(location: m, length: sel.length - 2 * m))30 return Edit(range: sel, replacement: core, selection: NSRange(location: sel.location, length: sel.length - 2 * m))31 }32 return Edit(range: sel, replacement: marker + inner + marker, selection: NSRange(location: sel.location + m, length: sel.length))33 }3435 private static func isDoubled(_ ns: NSString, at index: Int, before: Bool) -> Bool {36 index >= 1 && ns.substring(with: NSRange(location: index - 1, length: 1)) == "*"37 }3839 /// Adds `prefix` ("# ", "- ", "> ") to every line the selection touches, or removes it when all of them have it.40 public static func togglePrefix(_ prefix: String, in text: String, selection: NSRange) -> Edit {41 let ns = text as NSString42 let sel = selection.location + selection.length <= ns.length ? selection : NSRange(location: ns.length, length: 0)43 let lines = ns.lineRange(for: sel)44 let block = ns.substring(with: lines)45 let trailingNewline = block.hasSuffix("\n")46 var parts = block.components(separatedBy: "\n")47 if trailingNewline { parts.removeLast() }48 let all = parts.allSatisfy { $0.hasPrefix(prefix) }49 let out = parts.map { all ? String($0.dropFirst(prefix.count)) : prefix + $0 }.joined(separator: "\n") + (trailingNewline ? "\n" : "")50 let delta = (out as NSString).length - lines.length51 let p = (prefix as NSString).length52 let selection = parts.count == 153 ? NSRange(location: max(lines.location, sel.location + (all ? -p : p)), length: sel.length)54 : NSRange(location: lines.location, length: lines.length + delta - (trailingNewline ? 1 : 0))55 return Edit(range: lines, replacement: out, selection: selection)56 }57}58