krz/org-swift

A dependency-free Swift library that renders org-mode to sanitized HTML. html library org-mode swift

Commit ca2a0de207

ca2a0de2077b96dc2405d59def877f53e1933063

parent: d1d98c0638

Unsigned

cmc <hello@cleberg.net> · 2026-08-31 18:13 UTC

Rebuild the layout when the color scheme changes

A code styler bakes its theme's colors into the AttributedString it
returns, so a layout built in light mode kept light-theme code after a
switch to dark — dark grey on a dark background. The task now keys on the
color scheme as well as the source.

Found switching the app to dark mode on krz/gitbay's README.

Layout: unified · split

Sources/OrgSwiftUI/OrgView.swift +13 −4
@@ -9,14 +9,16 @@ import SwiftUI
99/// ```
1010///
1111/// The parse, footnote numbering, URL resolution and syntax highlighting run once off
12/// `body`, in a task keyed to the source, so scrolling and Dynamic Type changes do not
13/// re-derive them. Text is selectable, links open through the environment's `openURL`, and
14/// everything scales with Dynamic Type.
12/// `body`, in a task keyed to the source *and the color scheme*, so scrolling and Dynamic
13/// Type changes do not re-derive them — but switching to dark mode does, because a
14/// ``OrgCodeStyler`` bakes its theme's colors into the text it returns. Text is selectable,
15/// links open through the environment's `openURL`, and everything scales with Dynamic Type.
1516public struct OrgView: View {
1617 private let source: String
1718 private let options: OrgRenderOptions
1819 private let styler: OrgCodeStyler
1920
21 @Environment(\.colorScheme) private var colorScheme
2022 @State private var layout = OrgLayout()
2123
2224 public init(
@@ -38,13 +40,20 @@ public struct OrgView: View {
3840 }
3941 .frame(maxWidth: .infinity, alignment: .leading)
4042 .textSelection(.enabled)
41 .task(id: source) {
43 .task(id: LayoutKey(source: source, colorScheme: colorScheme)) {
4244 layout = OrgLayoutBuilder(options: options, styler: styler)
4345 .layout(OrgParser.parse(source))
4446 }
4547 }
4648}
4749
50/// What the prepared layout depends on. Highlighted code carries baked-in colors, so the
51/// scheme belongs in the key alongside the source.
52private struct LayoutKey: Equatable {
53 var source: String
54 var colorScheme: ColorScheme
55}
56
4857/// The notes section org's exporter puts at the end of a document.
4958struct OrgFootnotesView: View {
5059 let footnotes: [OrgFootnote]