Commit 435b00c7af
Unsigned ci/gitbay-security: success
Layout: unified · split
Package.swift +4 −1
| @@ -16,10 +16,13 @@ let package = Package( | ||
| 16 | 16 | .product(name: "ElementaryUI", package: "elementary-ui"), |
| 17 | 17 | .product(name: "JavaScriptKit", package: "JavaScriptKit"), |
| 18 | 18 | .product(name: "OrgSwift", package: "org-swift"), |
| 19 | "OrgHighlight", | |
| 19 | 20 | ], |
| 20 | 21 | swiftSettings: [ |
| 21 | 22 | .swiftLanguageMode(.v5) |
| 22 | 23 | ], |
| 23 | ) | |
| 24 | ), | |
| 25 | .target(name: "OrgHighlight"), | |
| 26 | .testTarget(name: "OrgHighlightTests", dependencies: ["OrgHighlight"]), | |
| 24 | 27 | ] |
| 25 | 28 | ) |
README.org +7 −2
| @@ -6,13 +6,18 @@ https://org.krz.sh | ||
| 6 | 6 | |
| 7 | 7 | * does |
| 8 | 8 | - plain text editor with capitalization, autocorrect, spellcheck |
| 9 | - org syntax highlighting and line numbers in the editor | |
| 10 | - light and dark themes, following the system setting | |
| 9 | 11 | - html output of the written org-mode |
| 10 | 12 | - download the org-mode to a file |
| 11 | 13 | - download the parsed html to a file |
| 12 | 14 | |
| 13 | 15 | * screenshots |
| 14 | - screenshots/light_preview.png | |
| 15 | - screenshots/dark_preview.png | |
| 16 | #+CAPTION: org-live in light mode | |
| 17 | [[file:screenshots/light_preview.png]] | |
| 18 | ||
| 19 | #+CAPTION: org-live in dark mode | |
| 20 | [[file:screenshots/dark_preview.png]] | |
| 16 | 21 | |
| 17 | 22 | * build |
| 18 | 23 | Swift compiled to WebAssembly with [[https://elementary.codes][ElementaryUI]], |
Sources/OrgHighlight/OrgHighlighter.swift added +330
| @@ -0,0 +1,330 @@ | ||
| 1 | /// Highlights org source for the editor: one `<div class="ln">` per source line, with | |
| 2 | /// `<span>` classes for markup. Line-oriented with block state; it does not parse the | |
| 3 | /// document, so it stays cheap enough to run on every keystroke. | |
| 4 | public enum OrgHighlighter { | |
| 5 | ||
| 6 | /// The HTML of each source line, in order. | |
| 7 | public static func lines(_ source: String) -> [String] { | |
| 8 | var block: [UInt8]? = nil | |
| 9 | var out: [UInt8] = [] | |
| 10 | return source.utf8.split(separator: UInt8(ascii: "\n"), omittingEmptySubsequences: false).map { line in | |
| 11 | out.removeAll(keepingCapacity: true) | |
| 12 | out.append(contentsOf: lineOpen) | |
| 13 | highlightLine(Array(line), block: &block, into: &out) | |
| 14 | out.append(contentsOf: lineClose) | |
| 15 | return String(decoding: out, as: UTF8.self) | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | private static let lineOpen = Array("<div class=\"ln\">".utf8) | |
| 20 | private static let lineClose = Array("\n</div>".utf8) | |
| 21 | ||
| 22 | /// Blocks whose bodies are shown verbatim, without inline markup. | |
| 23 | private static let verbatimBlocks: [[UInt8]] = ["src", "example", "export", "comment"].map { Array($0.utf8) } | |
| 24 | ||
| 25 | private static func highlightLine(_ b: [UInt8], block: inout [UInt8]?, into out: inout [UInt8]) { | |
| 26 | var w = Writer(b: b, out: out) | |
| 27 | defer { out = w.out } | |
| 28 | let start = indentEnd(b) | |
| 29 | ||
| 30 | if let name = block { | |
| 31 | if lowerHasPrefix(b, at: start, "#+end_") { | |
| 32 | block = nil | |
| 33 | w.span("meta", 0, b.count) | |
| 34 | } else if verbatimBlocks.contains(name) { | |
| 35 | w.span("blk", 0, b.count) | |
| 36 | } else { | |
| 37 | inline(&w, 0, b.count) | |
| 38 | } | |
| 39 | return | |
| 40 | } | |
| 41 | ||
| 42 | // Headings: stars at column 0 followed by a space. | |
| 43 | var stars = 0 | |
| 44 | while stars < b.count && b[stars] == UInt8(ascii: "*") { stars += 1 } | |
| 45 | if stars > 0 && stars < b.count && b[stars] == UInt8(ascii: " ") { | |
| 46 | heading(&w, stars) | |
| 47 | return | |
| 48 | } | |
| 49 | ||
| 50 | if lowerHasPrefix(b, at: start, "#+begin_") { | |
| 51 | var e = start + 8 | |
| 52 | while e < b.count && !isSpace(b[e]) { e += 1 } | |
| 53 | block = b[(start + 8)..<e].map(lower) | |
| 54 | w.span("meta", 0, b.count) | |
| 55 | return | |
| 56 | } | |
| 57 | if hasPrefix(b, at: start, "#+") { | |
| 58 | var e = start + 2 | |
| 59 | while e < b.count && isKeyChar(b[e]) { e += 1 } | |
| 60 | if e > start + 2 && e < b.count && b[e] == UInt8(ascii: ":") { | |
| 61 | w.span("kw", 0, e + 1) | |
| 62 | w.text(e + 1, b.count) | |
| 63 | } else { | |
| 64 | w.span("meta", 0, b.count) | |
| 65 | } | |
| 66 | return | |
| 67 | } | |
| 68 | if start < b.count && b[start] == UInt8(ascii: "#") && (start + 1 == b.count || isSpace(b[start + 1])) { | |
| 69 | w.span("cmt", 0, b.count) | |
| 70 | return | |
| 71 | } | |
| 72 | if start < b.count && b[start] == UInt8(ascii: "|") { | |
| 73 | w.span("tbl", 0, b.count) | |
| 74 | return | |
| 75 | } | |
| 76 | if start < b.count && b[start] == UInt8(ascii: ":") { | |
| 77 | if start + 1 == b.count || b[start + 1] == UInt8(ascii: " ") { | |
| 78 | w.span("fix", 0, b.count) | |
| 79 | return | |
| 80 | } | |
| 81 | var e = start + 1 | |
| 82 | while e < b.count && isKeyChar(b[e]) { e += 1 } | |
| 83 | if e > start + 1 && e < b.count && b[e] == UInt8(ascii: ":") && (e + 1 == b.count || isSpace(b[e + 1])) { | |
| 84 | w.span("drw", 0, e + 1) | |
| 85 | w.text(e + 1, b.count) | |
| 86 | return | |
| 87 | } | |
| 88 | } | |
| 89 | if b.count - start >= 5 && b[start...].allSatisfy({ $0 == UInt8(ascii: "-") || isSpace($0) }) | |
| 90 | && b[start..<(start + 5)].allSatisfy({ $0 == UInt8(ascii: "-") }) { | |
| 91 | w.span("hr", 0, b.count) | |
| 92 | return | |
| 93 | } | |
| 94 | if let bulletEnd = bullet(b, start) { | |
| 95 | w.span("bul", 0, bulletEnd) | |
| 96 | var i = bulletEnd | |
| 97 | if i + 3 <= b.count && b[i] == UInt8(ascii: "[") && b[i + 2] == UInt8(ascii: "]") | |
| 98 | && (b[i + 1] == UInt8(ascii: " ") || b[i + 1] == UInt8(ascii: "X") || b[i + 1] == UInt8(ascii: "x") || b[i + 1] == UInt8(ascii: "-")) { | |
| 99 | w.span("cb", i, i + 3) | |
| 100 | i += 3 | |
| 101 | } | |
| 102 | inline(&w, i, b.count) | |
| 103 | return | |
| 104 | } | |
| 105 | inline(&w, 0, b.count) | |
| 106 | } | |
| 107 | ||
| 108 | private static func heading(_ w: inout Writer, _ stars: Int) { | |
| 109 | let b = w.b | |
| 110 | let level = (stars - 1) % 3 + 1 | |
| 111 | w.open("h\(level)") | |
| 112 | var i = stars + 1 | |
| 113 | w.text(0, i) | |
| 114 | if let (word, cls) = todoKeyword(b, i) { | |
| 115 | w.span(cls, i, word) | |
| 116 | i = word | |
| 117 | } | |
| 118 | if i + 4 <= b.count && b[i] == UInt8(ascii: " ") && b[i + 1] == UInt8(ascii: "[") && b[i + 2] == UInt8(ascii: "#") && b[i + 4] == UInt8(ascii: "]") { | |
| 119 | w.text(i, i + 1) | |
| 120 | w.span("pri", i + 1, i + 5) | |
| 121 | i += 5 | |
| 122 | } else if i + 3 <= b.count && b[i] == UInt8(ascii: "[") && b[i + 1] == UInt8(ascii: "#") && b[i + 3] == UInt8(ascii: "]") { | |
| 123 | w.span("pri", i, i + 4) | |
| 124 | i += 4 | |
| 125 | } | |
| 126 | if let tags = tagsStart(b, from: i) { | |
| 127 | inline(&w, i, tags) | |
| 128 | w.span("tag", tags, b.count) | |
| 129 | } else { | |
| 130 | inline(&w, i, b.count) | |
| 131 | } | |
| 132 | w.close() | |
| 133 | } | |
| 134 | ||
| 135 | private static func todoKeyword(_ b: [UInt8], _ i: Int) -> (Int, String)? { | |
| 136 | for (word, cls) in [("TODO", "todo"), ("DONE", "done")] { | |
| 137 | let u = Array(word.utf8) | |
| 138 | if hasPrefix(b, at: i, word) && (i + u.count == b.count || b[i + u.count] == UInt8(ascii: " ")) { | |
| 139 | return (i + u.count, cls) | |
| 140 | } | |
| 141 | } | |
| 142 | return nil | |
| 143 | } | |
| 144 | ||
| 145 | /// Start of a trailing `:tag1:tag2:` group preceded by whitespace, if any. | |
| 146 | private static func tagsStart(_ b: [UInt8], from: Int) -> Int? { | |
| 147 | var end = b.count | |
| 148 | while end > from && isSpace(b[end - 1]) { end -= 1 } | |
| 149 | guard end - from >= 3, b[end - 1] == UInt8(ascii: ":") else { return nil } | |
| 150 | var s = end - 1 | |
| 151 | while s > from && (isTagChar(b[s - 1]) || b[s - 1] == UInt8(ascii: ":")) { s -= 1 } | |
| 152 | while s < end && b[s] != UInt8(ascii: ":") { s += 1 } | |
| 153 | guard s < end - 1, s > from, isSpace(b[s - 1]) else { return nil } | |
| 154 | return s | |
| 155 | } | |
| 156 | ||
| 157 | /// End of a list bullet and its following space, if the line has one. | |
| 158 | private static func bullet(_ b: [UInt8], _ start: Int) -> Int? { | |
| 159 | guard start < b.count else { return nil } | |
| 160 | var e = start | |
| 161 | let c = b[start] | |
| 162 | if c == UInt8(ascii: "-") || c == UInt8(ascii: "+") || (c == UInt8(ascii: "*") && start > 0) { | |
| 163 | e += 1 | |
| 164 | } else { | |
| 165 | while e < b.count && isDigit(b[e]) { e += 1 } | |
| 166 | guard e > start, e < b.count, b[e] == UInt8(ascii: ".") || b[e] == UInt8(ascii: ")") else { return nil } | |
| 167 | e += 1 | |
| 168 | } | |
| 169 | if e == b.count { return e } | |
| 170 | guard b[e] == UInt8(ascii: " ") else { return nil } | |
| 171 | return e + 1 | |
| 172 | } | |
| 173 | ||
| 174 | // MARK: - Inline | |
| 175 | ||
| 176 | private static func inline(_ w: inout Writer, _ from: Int, _ to: Int) { | |
| 177 | let b = w.b | |
| 178 | var i = from | |
| 179 | var plain = from | |
| 180 | while i < to { | |
| 181 | var match: (end: Int, cls: String)? = nil | |
| 182 | let c = b[i] | |
| 183 | if c == UInt8(ascii: "[") { | |
| 184 | if i + 1 < to && b[i + 1] == UInt8(ascii: "["), let e = find(b, "]]", i + 2, to) { | |
| 185 | match = (e + 2, "lnk") | |
| 186 | } else if hasPrefix(b, at: i, "[fn:"), let e = find(b, "]", i + 4, to) { | |
| 187 | match = (e + 1, "fn") | |
| 188 | } else if isDate(b, i + 1, to), let e = find(b, "]", i + 1, to) { | |
| 189 | match = (e + 1, "ts") | |
| 190 | } | |
| 191 | } else if c == UInt8(ascii: "<") && isDate(b, i + 1, to), let e = find(b, ">", i + 1, to) { | |
| 192 | match = (e + 1, "ts") | |
| 193 | } else if c == UInt8(ascii: "h") && (i == from || !isWordChar(b[i - 1])) | |
| 194 | && (hasPrefix(b, at: i, "https://") || hasPrefix(b, at: i, "http://")) { | |
| 195 | var e = i | |
| 196 | while e < to && !isSpace(b[e]) { e += 1 } | |
| 197 | match = (e, "lnk") | |
| 198 | } else if let cls = emphasisClass(c), let e = emphasisEnd(b, i, from, to) { | |
| 199 | match = (e, cls) | |
| 200 | } | |
| 201 | if let m = match { | |
| 202 | w.text(plain, i) | |
| 203 | w.span(m.cls, i, m.end) | |
| 204 | i = m.end | |
| 205 | plain = i | |
| 206 | } else { | |
| 207 | i += 1 | |
| 208 | } | |
| 209 | } | |
| 210 | w.text(plain, to) | |
| 211 | } | |
| 212 | ||
| 213 | private static func emphasisClass(_ c: UInt8) -> String? { | |
| 214 | switch c { | |
| 215 | case UInt8(ascii: "*"): "b" | |
| 216 | case UInt8(ascii: "/"): "i" | |
| 217 | case UInt8(ascii: "_"): "u" | |
| 218 | case UInt8(ascii: "+"): "s" | |
| 219 | case UInt8(ascii: "="), UInt8(ascii: "~"): "v" | |
| 220 | default: nil | |
| 221 | } | |
| 222 | } | |
| 223 | ||
| 224 | /// End (past the closing marker) of an emphasis span opening at `i`, following org's | |
| 225 | /// rules for the characters allowed around markers. | |
| 226 | private static func emphasisEnd(_ b: [UInt8], _ i: Int, _ from: Int, _ to: Int) -> Int? { | |
| 227 | let m = b[i] | |
| 228 | if i > from && !isSpace(b[i - 1]) && !contains("-('\"{", b[i - 1]) { return nil } | |
| 229 | guard i + 2 < to, !isSpace(b[i + 1]), b[i + 1] != m else { return nil } | |
| 230 | var k = i + 2 | |
| 231 | while k < to { | |
| 232 | if b[k] == m && !isSpace(b[k - 1]) && (k + 1 == to || isSpace(b[k + 1]) || contains("-.,;:!?')}[\"\\", b[k + 1])) { | |
| 233 | return k + 1 | |
| 234 | } | |
| 235 | k += 1 | |
| 236 | } | |
| 237 | return nil | |
| 238 | } | |
| 239 | ||
| 240 | // MARK: - Output | |
| 241 | ||
| 242 | private struct Writer { | |
| 243 | let b: [UInt8] | |
| 244 | var out: [UInt8] | |
| 245 | ||
| 246 | mutating func text(_ from: Int, _ to: Int) { | |
| 247 | var i = from | |
| 248 | while i < to { | |
| 249 | switch b[i] { | |
| 250 | case UInt8(ascii: "&"): out.append(contentsOf: "&".utf8) | |
| 251 | case UInt8(ascii: "<"): out.append(contentsOf: "<".utf8) | |
| 252 | case UInt8(ascii: ">"): out.append(contentsOf: ">".utf8) | |
| 253 | default: out.append(b[i]) | |
| 254 | } | |
| 255 | i += 1 | |
| 256 | } | |
| 257 | } | |
| 258 | ||
| 259 | mutating func open(_ cls: String) { | |
| 260 | out.append(contentsOf: "<span class=\"".utf8) | |
| 261 | out.append(contentsOf: cls.utf8) | |
| 262 | out.append(contentsOf: "\">".utf8) | |
| 263 | } | |
| 264 | ||
| 265 | mutating func close() { | |
| 266 | out.append(contentsOf: "</span>".utf8) | |
| 267 | } | |
| 268 | ||
| 269 | mutating func span(_ cls: String, _ from: Int, _ to: Int) { | |
| 270 | open(cls) | |
| 271 | text(from, to) | |
| 272 | close() | |
| 273 | } | |
| 274 | } | |
| 275 | ||
| 276 | // MARK: - Bytes | |
| 277 | ||
| 278 | private static func indentEnd(_ b: [UInt8]) -> Int { | |
| 279 | var i = 0 | |
| 280 | while i < b.count && isSpace(b[i]) { i += 1 } | |
| 281 | return i | |
| 282 | } | |
| 283 | ||
| 284 | private static func hasPrefix(_ b: [UInt8], at i: Int, _ p: String) -> Bool { | |
| 285 | var j = i | |
| 286 | for c in p.utf8 { | |
| 287 | guard j < b.count, b[j] == c else { return false } | |
| 288 | j += 1 | |
| 289 | } | |
| 290 | return true | |
| 291 | } | |
| 292 | ||
| 293 | private static func lowerHasPrefix(_ b: [UInt8], at i: Int, _ p: String) -> Bool { | |
| 294 | var j = i | |
| 295 | for c in p.utf8 { | |
| 296 | guard j < b.count, lower(b[j]) == c else { return false } | |
| 297 | j += 1 | |
| 298 | } | |
| 299 | return true | |
| 300 | } | |
| 301 | ||
| 302 | private static func find(_ b: [UInt8], _ s: String, _ from: Int, _ to: Int) -> Int? { | |
| 303 | let n = Array(s.utf8) | |
| 304 | var i = from | |
| 305 | while i + n.count <= to { | |
| 306 | if b[i] == n[0] && Array(b[i..<(i + n.count)]) == n { return i } | |
| 307 | i += 1 | |
| 308 | } | |
| 309 | return nil | |
| 310 | } | |
| 311 | ||
| 312 | /// `YYYY-MM-DD` at `i`. | |
| 313 | private static func isDate(_ b: [UInt8], _ i: Int, _ to: Int) -> Bool { | |
| 314 | guard i + 10 <= to else { return false } | |
| 315 | for k in 0..<10 { | |
| 316 | let ok = (k == 4 || k == 7) ? b[i + k] == UInt8(ascii: "-") : isDigit(b[i + k]) | |
| 317 | if !ok { return false } | |
| 318 | } | |
| 319 | return true | |
| 320 | } | |
| 321 | ||
| 322 | private static func contains(_ set: String, _ c: UInt8) -> Bool { set.utf8.contains(c) } | |
| 323 | private static func lower(_ c: UInt8) -> UInt8 { c >= 65 && c <= 90 ? c + 32 : c } | |
| 324 | private static func isSpace(_ c: UInt8) -> Bool { c == 32 || c == 9 } | |
| 325 | private static func isDigit(_ c: UInt8) -> Bool { c >= 48 && c <= 57 } | |
| 326 | private static func isAlpha(_ c: UInt8) -> Bool { (c >= 65 && c <= 90) || (c >= 97 && c <= 122) } | |
| 327 | private static func isWordChar(_ c: UInt8) -> Bool { isAlpha(c) || isDigit(c) || c >= 0x80 } | |
| 328 | private static func isKeyChar(_ c: UInt8) -> Bool { isAlpha(c) || isDigit(c) || c == 95 || c == 45 } | |
| 329 | private static func isTagChar(_ c: UInt8) -> Bool { isWordChar(c) || c == 95 || c == 64 || c == 35 || c == 37 } | |
| 330 | } | |
Sources/OrgLive/App.swift +63 −15
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import ElementaryUI |
| 2 | 2 | import JavaScriptKit |
| 3 | import OrgHighlight | |
| 3 | 4 | import OrgSwift |
| 4 | 5 | |
| 5 | 6 | @main |
| @@ -14,7 +15,7 @@ struct App { | ||
| 14 | 15 | struct Editor { |
| 15 | 16 | var body: some View { |
| 16 | 17 | form(.autocomplete(.on)) { |
| 17 | div { | |
| 18 | div(.class("toolbar")) { | |
| 18 | 19 | label(.custom(name: "for", value: "editor")) { "org-live" } |
| 19 | 20 | span(.class("spacer")) {} |
| 20 | 21 | button(.type(.button)) { "Save HTML" } |
| @@ -22,20 +23,31 @@ struct Editor { | ||
| 22 | 23 | button(.type(.button)) { "Save Org" } |
| 23 | 24 | .onClick { save(editorValue(), extension: "org") } |
| 24 | 25 | } |
| 25 | textarea( | |
| 26 | .id("editor"), | |
| 27 | .name("editor"), | |
| 28 | .custom(name: "autocapitalize", value: "on"), | |
| 29 | .custom(name: "autocorrect", value: "on"), | |
| 30 | .custom(name: "spellcheck", value: "true"), | |
| 31 | .placeholder("* Lorem Ipsum"), | |
| 32 | .cols(80), | |
| 33 | .wrap(.hard), | |
| 34 | .autofocus, | |
| 35 | .required | |
| 36 | ) {} | |
| 37 | .onInput { event in | |
| 38 | preview.innerHTML = .string(renderHTML(event.targetValue ?? "")) | |
| 26 | div(.id("source")) { | |
| 27 | div(.class("layers")) { | |
| 28 | div(.id("highlight"), .custom(name: "aria-hidden", value: "true")) {} | |
| 29 | textarea( | |
| 30 | .id("editor"), | |
| 31 | .name("editor"), | |
| 32 | .custom(name: "autocapitalize", value: "on"), | |
| 33 | .custom(name: "autocorrect", value: "on"), | |
| 34 | .custom(name: "spellcheck", value: "true"), | |
| 35 | .placeholder("* Lorem Ipsum"), | |
| 36 | .cols(80), | |
| 37 | .wrap(.hard), | |
| 38 | .autofocus, | |
| 39 | .required | |
| 40 | ) {} | |
| 41 | .onInput { event in update(event.targetValue ?? "") } | |
| 42 | .onAppear { | |
| 43 | let editor = document.getElementById!("editor").object! | |
| 44 | editor.value = .string(sample) | |
| 45 | _ = editor.focus!() | |
| 46 | _ = editor.setSelectionRange!(0, 0) | |
| 47 | update(sample) | |
| 48 | document.getElementById!("source").object!.scrollTop = 0 | |
| 49 | } | |
| 50 | } | |
| 39 | 51 | } |
| 40 | 52 | } |
| 41 | 53 | div(.id("preview")) {} |
| @@ -44,11 +56,47 @@ struct Editor { | ||
| 44 | 56 | |
| 45 | 57 | private let document = JSObject.global.document.object! |
| 46 | 58 | private var preview: JSObject { document.getElementById!("preview").object! } |
| 59 | private var highlight: JSObject { document.getElementById!("highlight").object! } | |
| 47 | 60 | |
| 48 | 61 | private func editorValue() -> String { |
| 49 | 62 | document.getElementById!("editor").value.string ?? "" |
| 50 | 63 | } |
| 51 | 64 | |
| 65 | /// Lines currently in #highlight, so an edit only replaces the lines that changed. | |
| 66 | private var shownLines: [String] = [] | |
| 67 | ||
| 68 | private func update(_ org: String) { | |
| 69 | updateHighlight(OrgHighlighter.lines(org)) | |
| 70 | preview.innerHTML = .string(renderHTML(org)) | |
| 71 | } | |
| 72 | ||
| 73 | private func updateHighlight(_ lines: [String]) { | |
| 74 | var start = 0 | |
| 75 | while start < lines.count && start < shownLines.count && lines[start] == shownLines[start] { start += 1 } | |
| 76 | var newEnd = lines.count | |
| 77 | var oldEnd = shownLines.count | |
| 78 | while newEnd > start && oldEnd > start && lines[newEnd - 1] == shownLines[oldEnd - 1] { | |
| 79 | newEnd -= 1 | |
| 80 | oldEnd -= 1 | |
| 81 | } | |
| 82 | shownLines = lines | |
| 83 | let element = highlight | |
| 84 | let children = element.children.object! | |
| 85 | if oldEnd > start { | |
| 86 | let range = document.createRange!().object! | |
| 87 | _ = range.setStartBefore!(children[start]) | |
| 88 | _ = range.setEndAfter!(children[oldEnd - 1]) | |
| 89 | _ = range.deleteContents!() | |
| 90 | } | |
| 91 | guard newEnd > start else { return } | |
| 92 | let html = JSValue.string(lines[start..<newEnd].joined()) | |
| 93 | if start < Int(children.length.number!) { | |
| 94 | _ = children[start].object!.insertAdjacentHTML!("beforebegin", html) | |
| 95 | } else { | |
| 96 | _ = element.insertAdjacentHTML!("beforeend", html) | |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 52 | 100 | private func renderHTML(_ org: String) -> String { |
| 53 | 101 | var options = OrgRenderOptions() |
| 54 | 102 | options.headingLevelOffset = 1 |
Sources/OrgLive/Sample.swift added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | /// Initial editor content: a tour of the org syntax the editor highlights and renders. | |
| 2 | let sample = """ | |
| 3 | #+TITLE: org-live | |
| 4 | #+AUTHOR: Your Name | |
| 5 | #+DATE: <2026-11-01 Sun> | |
| 6 | ||
| 7 | Type org-mode on this side; the HTML appears below. Replace this text with | |
| 8 | your own, then save it as .org or .html. | |
| 9 | ||
| 10 | * Text | |
| 11 | Inline markup: *bold*, /italic/, _underline_, +strike-through+, =verbatim= | |
| 12 | and ~code~. Superscripts like E = mc^2 work too. | |
| 13 | ||
| 14 | Links: [[https://orgmode.org][the org-mode site]], a bare URL such as | |
| 15 | https://orgmode.org/manual/, and a footnote reference.[fn:1] | |
| 16 | ||
| 17 | Timestamps: <2026-11-02 Mon 10:00>, inactive [2026-10-31 Sat], and a range | |
| 18 | <2026-11-03 Tue>--<2026-11-05 Thu>. | |
| 19 | ||
| 20 | * TODO [#A] Lists :lists: | |
| 21 | - Unordered items | |
| 22 | - nest by indenting | |
| 23 | - [X] checkbox, done | |
| 24 | - [ ] checkbox, open | |
| 25 | ||
| 26 | 1. Ordered items | |
| 27 | 2. count themselves | |
| 28 | ||
| 29 | - Term :: a description list pairs a term with its definition | |
| 30 | ||
| 31 | ** DONE Sub-heading with a tag :done: | |
| 32 | Headings go three levels deep here, and further in org. | |
| 33 | ||
| 34 | *** Third level | |
| 35 | ||
| 36 | * Tables | |
| 37 | | Language | Year | Typing | | |
| 38 | |----------+------+---------| | |
| 39 | | Swift | 2014 | static | | |
| 40 | | Lisp | 1958 | dynamic | | |
| 41 | ||
| 42 | * Blocks | |
| 43 | #+begin_src swift | |
| 44 | let greeting = "Hello, *org*" | |
| 45 | print(greeting) | |
| 46 | #+end_src | |
| 47 | ||
| 48 | #+begin_quote | |
| 49 | Org mode is for keeping notes, maintaining TODO lists, planning projects, | |
| 50 | and authoring documents with a fast and effective plain-text system. | |
| 51 | #+end_quote | |
| 52 | ||
| 53 | #+begin_example | |
| 54 | Example blocks keep their text exactly as written. | |
| 55 | #+end_example | |
| 56 | ||
| 57 | #+begin_center | |
| 58 | Centered text | |
| 59 | #+end_center | |
| 60 | ||
| 61 | ----- | |
| 62 | ||
| 63 | # A comment line: in the source, not in the output. | |
| 64 | ||
| 65 | [fn:1] Footnotes are collected at the end of the document. | |
| 66 | """ | |
Tests/OrgHighlightTests/OrgHighlighterTests.swift added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | import Testing | |
| 2 | @testable import OrgHighlight | |
| 3 | ||
| 4 | /// The inner HTML of each line, without the `<div class="ln">` wrapper. | |
| 5 | private func lines(_ source: String) -> [String] { | |
| 6 | OrgHighlighter.lines(source).map { line in | |
| 7 | #expect(line.hasPrefix("<div class=\"ln\">") && line.hasSuffix("\n</div>")) | |
| 8 | return String(line.dropFirst("<div class=\"ln\">".count).dropLast("\n</div>".count)) | |
| 9 | } | |
| 10 | } | |
| 11 | ||
| 12 | @Test func oneDivPerSourceLine() { | |
| 13 | #expect(lines("").count == 1) | |
| 14 | #expect(lines("a\n").count == 2) | |
| 15 | #expect(lines("a\n\nb").count == 3) | |
| 16 | } | |
| 17 | ||
| 18 | @Test func escapesHTML() { | |
| 19 | #expect(lines("<script>&") == ["<script>&"]) | |
| 20 | } | |
| 21 | ||
| 22 | @Test func headings() { | |
| 23 | #expect(lines("* Title") == [#"<span class="h1">* Title</span>"#]) | |
| 24 | #expect(lines("** TODO [#A] Fix it :work:urgent:") == [ | |
| 25 | #"<span class="h2">** <span class="todo">TODO</span> <span class="pri">[#A]</span> Fix it <span class="tag">:work:urgent:</span></span>"# | |
| 26 | ]) | |
| 27 | #expect(lines("**** DONE x") == [#"<span class="h1">**** <span class="done">DONE</span> x</span>"#]) | |
| 28 | #expect(lines("*bold* text") == [#"<span class="b">*bold*</span> text"#]) | |
| 29 | #expect(lines("* TODOs") == [#"<span class="h1">* TODOs</span>"#]) | |
| 30 | #expect(lines("* a:b:") == [#"<span class="h1">* a:b:</span>"#]) | |
| 31 | } | |
| 32 | ||
| 33 | @Test func blocks() { | |
| 34 | let src = "#+BEGIN_SRC swift\nlet x = *y*\n#+end_src\n*b*" | |
| 35 | #expect(lines(src) == [ | |
| 36 | #"<span class="meta">#+BEGIN_SRC swift</span>"#, | |
| 37 | #"<span class="blk">let x = *y*</span>"#, | |
| 38 | #"<span class="meta">#+end_src</span>"#, | |
| 39 | #"<span class="b">*b*</span>"#, | |
| 40 | ]) | |
| 41 | #expect(lines("#+begin_quote\n* not a heading /i/\n#+end_quote") == [ | |
| 42 | #"<span class="meta">#+begin_quote</span>"#, | |
| 43 | #"* not a heading <span class="i">/i/</span>"#, | |
| 44 | #"<span class="meta">#+end_quote</span>"#, | |
| 45 | ]) | |
| 46 | } | |
| 47 | ||
| 48 | @Test func lineKinds() { | |
| 49 | #expect(lines("#+TITLE: Notes") == [#"<span class="kw">#+TITLE:</span> Notes"#]) | |
| 50 | #expect(lines("# a comment") == [#"<span class="cmt"># a comment</span>"#]) | |
| 51 | #expect(lines("#hashtag") == ["#hashtag"]) | |
| 52 | #expect(lines("| a | b |") == [#"<span class="tbl">| a | b |</span>"#]) | |
| 53 | #expect(lines(" :PROPERTIES:") == [#"<span class="drw"> :PROPERTIES:</span>"#]) | |
| 54 | #expect(lines(":ID: 42") == [#"<span class="drw">:ID:</span> 42"#]) | |
| 55 | #expect(lines(": fixed") == [#"<span class="fix">: fixed</span>"#]) | |
| 56 | #expect(lines("-----") == [#"<span class="hr">-----</span>"#]) | |
| 57 | } | |
| 58 | ||
| 59 | @Test func lists() { | |
| 60 | #expect(lines("- [X] done") == [#"<span class="bul">- </span><span class="cb">[X]</span> done"#]) | |
| 61 | #expect(lines(" 1. one") == [#"<span class="bul"> 1. </span>one"#]) | |
| 62 | #expect(lines(" * star item") == [#"<span class="bul"> * </span>star item"#]) | |
| 63 | #expect(lines("-5 degrees") == ["-5 degrees"]) | |
| 64 | } | |
| 65 | ||
| 66 | @Test func inlineMarkup() { | |
| 67 | #expect(lines("a /it/ _u_ +s+ =v= ~c~.") == [ | |
| 68 | #"a <span class="i">/it/</span> <span class="u">_u_</span> <span class="s">+s+</span> <span class="v">=v=</span> <span class="v">~c~</span>."# | |
| 69 | ]) | |
| 70 | #expect(lines("a/b/c and 2*3*4") == ["a/b/c and 2*3*4"]) | |
| 71 | #expect(lines("* not closed") == [#"<span class="h1">* not closed</span>"#]) | |
| 72 | #expect(lines("x * y * z") == ["x * y * z"]) | |
| 73 | } | |
| 74 | ||
| 75 | @Test func linksAndTimestamps() { | |
| 76 | #expect(lines("see [[https://a.b][A <b>]] now") == [ | |
| 77 | #"see <span class="lnk">[[https://a.b][A <b>]]</span> now"# | |
| 78 | ]) | |
| 79 | #expect(lines("at https://a.b/c/d/ ok") == [#"at <span class="lnk">https://a.b/c/d/</span> ok"#]) | |
| 80 | #expect(lines("<2026-11-01 Sun> [2026-11-01]") == [ | |
| 81 | #"<span class="ts"><2026-11-01 Sun></span> <span class="ts">[2026-11-01]</span>"# | |
| 82 | ]) | |
| 83 | #expect(lines("note[fn:1]") == [#"note<span class="fn">[fn:1]</span>"#]) | |
| 84 | } | |
| 85 | ||
| 86 | @Test func nonASCII() { | |
| 87 | #expect(lines("* Café /naïve/ 日本") == [#"<span class="h1">* Café <span class="i">/naïve/</span> 日本</span>"#]) | |
| 88 | } | |
screenshots/dark_preview.png
Binary file not shown.
screenshots/light_preview.png
Binary file not shown.
static/styles.css +136 −14
| @@ -352,7 +352,52 @@ template { | ||
| 352 | 352 | |
| 353 | 353 | /* CUSTOM SITE CSS |
| 354 | 354 | ========================================================================== */ |
| 355 | :root { | |
| 356 | color-scheme: light dark; | |
| 357 | --bg: #fff; | |
| 358 | --fg: #111; | |
| 359 | --rule: #000; | |
| 360 | --gutter: #aaa; | |
| 361 | --placeholder: #999; | |
| 362 | --selection: rgb(0 100 255 / 0.25); | |
| 363 | --h1: #1f4e9e; | |
| 364 | --h2: #8a4b00; | |
| 365 | --h3: #2e6b30; | |
| 366 | --todo: #c62828; | |
| 367 | --done: #2e7d32; | |
| 368 | --accent: #b26a00; | |
| 369 | --muted: #777; | |
| 370 | --meta: #6a1b9a; | |
| 371 | --verbatim: #00695c; | |
| 372 | --table: #37474f; | |
| 373 | --link: #1565c0; | |
| 374 | } | |
| 375 | ||
| 376 | @media (prefers-color-scheme: dark) { | |
| 377 | :root { | |
| 378 | --bg: #1c1b22; | |
| 379 | --fg: #e6e6e6; | |
| 380 | --rule: #888; | |
| 381 | --gutter: #5c5c66; | |
| 382 | --placeholder: #77777f; | |
| 383 | --selection: rgb(80 150 255 / 0.35); | |
| 384 | --h1: #79b8ff; | |
| 385 | --h2: #ffab70; | |
| 386 | --h3: #85e89d; | |
| 387 | --todo: #f97583; | |
| 388 | --done: #85e89d; | |
| 389 | --accent: #e3b341; | |
| 390 | --muted: #8b949e; | |
| 391 | --meta: #c4a7f5; | |
| 392 | --verbatim: #6fd8c2; | |
| 393 | --table: #c9d1d9; | |
| 394 | --link: #58a6ff; | |
| 395 | } | |
| 396 | } | |
| 397 | ||
| 355 | 398 | body { |
| 399 | background-color: var(--bg); | |
| 400 | color: var(--fg); | |
| 356 | 401 | font-family: |
| 357 | 402 | Menlo, |
| 358 | 403 | Consolas, |
| @@ -372,7 +417,7 @@ form { | ||
| 372 | 417 | justify-content: center; |
| 373 | 418 | } |
| 374 | 419 | |
| 375 | div:not(#preview) { | |
| 420 | .toolbar { | |
| 376 | 421 | display: flex; |
| 377 | 422 | flex-direction: row; |
| 378 | 423 | align-items: center; |
| @@ -396,19 +441,7 @@ button { | ||
| 396 | 441 | width: 20%; |
| 397 | 442 | } |
| 398 | 443 | |
| 399 | textarea { | |
| 400 | background-color: inherit; | |
| 401 | border: none; | |
| 402 | border-bottom: 1px solid black; | |
| 403 | border-radius: 0; | |
| 404 | -webkit-box-shadow: none; | |
| 405 | -moz-box-shadow: none; | |
| 406 | box-shadow: none; | |
| 407 | resize: none; | |
| 408 | outline: none; | |
| 409 | } | |
| 410 | ||
| 411 | textarea, | |
| 444 | #source, | |
| 412 | 445 | #preview { |
| 413 | 446 | height: 40vh; |
| 414 | 447 | width: 100vw; |
| @@ -420,6 +453,95 @@ textarea, | ||
| 420 | 453 | overflow: scroll; |
| 421 | 454 | } |
| 422 | 455 | |
| 456 | #source { | |
| 457 | border-bottom: 1px solid var(--rule); | |
| 458 | } | |
| 459 | ||
| 460 | /* The textarea sits over #highlight with transparent text, so the two must | |
| 461 | lay out identically. #highlight sets the height; the textarea fills it. */ | |
| 462 | .layers { | |
| 463 | position: relative; | |
| 464 | min-height: 100%; | |
| 465 | } | |
| 466 | ||
| 467 | #highlight, | |
| 468 | #editor { | |
| 469 | box-sizing: border-box; | |
| 470 | margin: 0; | |
| 471 | border: none; | |
| 472 | border-radius: 0; | |
| 473 | padding: 0 0 0 5ch; | |
| 474 | font: inherit; | |
| 475 | line-height: 1.4; | |
| 476 | letter-spacing: normal; | |
| 477 | tab-size: 8; | |
| 478 | white-space: pre-wrap; | |
| 479 | overflow-wrap: break-word; | |
| 480 | word-break: normal; | |
| 481 | } | |
| 482 | ||
| 483 | #highlight { | |
| 484 | counter-reset: ln; | |
| 485 | } | |
| 486 | ||
| 487 | #editor { | |
| 488 | position: absolute; | |
| 489 | inset: 0; | |
| 490 | width: 100%; | |
| 491 | height: 100%; | |
| 492 | overflow: hidden; | |
| 493 | resize: none; | |
| 494 | outline: none; | |
| 495 | -webkit-box-shadow: none; | |
| 496 | -moz-box-shadow: none; | |
| 497 | box-shadow: none; | |
| 498 | background: transparent; | |
| 499 | color: transparent; | |
| 500 | -webkit-text-fill-color: transparent; | |
| 501 | caret-color: var(--fg); | |
| 502 | } | |
| 503 | ||
| 504 | #editor::placeholder { | |
| 505 | color: var(--placeholder); | |
| 506 | -webkit-text-fill-color: var(--placeholder); | |
| 507 | } | |
| 508 | ||
| 509 | #editor::selection { | |
| 510 | background: var(--selection); | |
| 511 | } | |
| 512 | ||
| 513 | .ln { | |
| 514 | position: relative; | |
| 515 | counter-increment: ln; | |
| 516 | } | |
| 517 | ||
| 518 | .ln::before { | |
| 519 | content: counter(ln); | |
| 520 | position: absolute; | |
| 521 | left: -5ch; | |
| 522 | width: 4ch; | |
| 523 | text-align: right; | |
| 524 | color: var(--gutter); | |
| 525 | } | |
| 526 | ||
| 527 | .h1 { color: var(--h1); font-weight: bold; } | |
| 528 | .h2 { color: var(--h2); font-weight: bold; } | |
| 529 | .h3 { color: var(--h3); font-weight: bold; } | |
| 530 | .todo { color: var(--todo); } | |
| 531 | .done { color: var(--done); } | |
| 532 | .pri, .bul, .cb { color: var(--accent); } | |
| 533 | .tag, .cmt, .drw, .fix, .hr { color: var(--muted); } | |
| 534 | .kw, .meta, .ts { color: var(--meta); } | |
| 535 | .blk, .v { color: var(--verbatim); } | |
| 536 | .tbl { color: var(--table); } | |
| 537 | .lnk, .fn { color: var(--link); text-decoration: underline; } | |
| 538 | ||
| 539 | #preview a { color: var(--link); } | |
| 540 | .b { font-weight: bold; } | |
| 541 | .i { font-style: italic; } | |
| 542 | .u { text-decoration: underline; } | |
| 543 | .s { text-decoration: line-through; } | |
| 544 | ||
| 423 | 545 | :modal { |
| 424 | 546 | height: 90vh; |
| 425 | 547 | width: 90vw; |