~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
23 lines · 1017 B Raw History
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 {
        // `&amp;` last, so that an escaped `&lt;` stays one.
        text.replacing("&lt;", with: "<").replacing("&gt;", with: ">")
            .replacing("&quot;", with: "\"").replacing("&#39;", with: "'").replacing("&#x27;", with: "'")
            .replacing("&amp;", with: "&")
    }
}