Cotton code
35 lines · 1.5 KB · SwiftRaw
// Preview words and borrowed titles use the faces' one Markdown setter.// A preview may collapse whitespace; it never interprets marks itself.import CottonFacesimport CottonKitimport Foundationpublic enum MarkdownText {    public static func stripInlineMarkdown(_ line: String) -> String {        FaceMarkdown.plain(line, blocks: false)    }    public static func stripMarkdown(_ body: String) -> String {        FaceMarkdown.plain(body)            .split(separator: "\n", omittingEmptySubsequences: false)            .map { $0.split(whereSeparator: { $0 == " " || $0 == "\t" || $0 == "\r" }).joined(separator: " ") }            .filter { !$0.isEmpty }            .joined(separator: "\n")    }    /// What a note is called on any surface that lists it — its own title, else    /// the first line it borrows as one, whole: the column holds the whole    /// line, and a head with too little room cuts it at a word when it must    /// (`PaperTitleLines`). `nil` ONLY when there is genuinely nothing to call    /// it, and the caller owns the "Untitled" word so this stays copy-free.    public static func displayTitle(title: String?, body: String) -> String? {        let named = title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""        if !named.isEmpty { return named }        let firstLine =            stripMarkdown(body).split(separator: "\n", omittingEmptySubsequences: false)            .first            .map { $0.trimmingCharacters(in: .whitespaces) } ?? ""        return firstLine.isEmpty ? nil : firstLine    }}