johnandersen777.bsky.social/knotless
git that lives in your Atmosphere account
Forked from chadtmiller.com/knotless
0
0
import Foundation
/// The colours the server gave a Markdown text's blocks of code, read from the
/// HTML it made of them, by each block's text: a block this app's Markdown reads
/// differently from the server's just goes without.
enum MarkdownFences {
static func ranges(_ html: String) -> [String: String] {
var out: [String: String] = [:]
for found in html.matches(of: #/<pre><code data-ranges="([^"]*)">(.*?)</code></pre>/#.dotMatchesNewlines()) {
var text = String(found.output.2)
if text.hasSuffix("\n") { text.removeLast() }
out[unescaped(text)] = String(found.output.1)
}
return out
}
static func unescaped(_ text: String) -> String {
// `&` last, so that an escaped `<` stays one.
text.replacing("<", with: "<").replacing(">", with: ">")
.replacing(""", with: "\"").replacing("'", with: "'").replacing("'", with: "'")
.replacing("&", with: "&")
}
}