import OrgSwift import SwiftUI /// Draws one TeX expression for ``OrgView``: a `Text` holding it, typically an image set on /// the baseline, or nil to show the source as written. public typealias OrgMathRenderer = @MainActor (OrgMath) -> Text? private struct OrgMathKey: EnvironmentKey { static let defaultValue: OrgMathRenderer? = nil } extension EnvironmentValues { var orgMath: OrgMathRenderer? { get { self[OrgMathKey.self] } set { self[OrgMathKey.self] = newValue } } } /// A styled run as `Text`, each expression drawn by `math` where it can be. Runs are taken /// by their math attribute alone, so an expression a `textTransform` split is still one; /// two identical expressions side by side share a range, so one holding its source k times /// draws k. @MainActor func orgText(_ string: AttributedString, math: OrgMathRenderer?) -> Text { guard let math, string.runs.contains(where: { $0[OrgMathAttribute.self] != nil }) else { return Text(string) } var result = Text(verbatim: "") for (expression, range) in string.runs[OrgMathAttribute.self] { let piece = AttributedString(string[range]) guard let expression, let drawn = math(expression) else { result = Text("\(result)\(Text(piece))") continue } let count = max(1, piece.characters.count / max(1, expression.source.count)) for _ in 0..