aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgUI/Math/MathBlockView.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <jj@r.bdr.sh>2026-06-18 23:53:25 +0200
committerRuben Beltran del Rio <jj@r.bdr.sh>2026-06-19 17:56:23 +0200
commit49b0f4a3fb0064313d16782839865b763ef760ce (patch)
treea58439f1433de9ff66dc0be28f025afe56e79cfe /Sources/NorgUI/Math/MathBlockView.swift
parentcb91a73bfc845d74c3e4d1115bce5dfed10d456d (diff)
Support Math rendering1.2.0
Diffstat (limited to 'Sources/NorgUI/Math/MathBlockView.swift')
-rw-r--r--Sources/NorgUI/Math/MathBlockView.swift34
1 files changed, 34 insertions, 0 deletions
diff --git a/Sources/NorgUI/Math/MathBlockView.swift b/Sources/NorgUI/Math/MathBlockView.swift
new file mode 100644
index 0000000..c29fdb6
--- /dev/null
+++ b/Sources/NorgUI/Math/MathBlockView.swift
@@ -0,0 +1,34 @@
+import SwiftUI
+
+/// Renders a `@math` block asynchronously with a fallback to `VerbatimBlock`.
+struct MathBlockView: View {
+ @Environment(\.norgTheme) private var theme
+
+ let latex: String
+
+ @State private var rendered: NorgMathImage?
+
+ var body: some View {
+ content
+ .task(id: renderKey) {
+ guard let renderer = theme.mathRenderer else { return }
+ rendered = await renderer.render(
+ latex: latex, mode: .block, fontSize: theme.mathFontSize, color: nil)
+ }
+ }
+
+ @ViewBuilder
+ private var content: some View {
+ if let rendered {
+ rendered.swiftUIImage
+ .frame(width: rendered.size.width, height: rendered.size.height, alignment: .leading)
+ .padding(theme.verbatimPadding)
+ .frame(maxWidth: .infinity, alignment: .leading)
+ .background(theme.codeBackground, in: RoundedRectangle(cornerRadius: 8))
+ } else {
+ VerbatimBlock(label: nil, content: latex)
+ }
+ }
+
+ private var renderKey: String { "\(latex)@\(theme.mathFontSize)" }
+}