| 1 | import Foundation |
| 2 | |
| 3 | /// The web's steady/open-link rules. Unmatched delimiters are removed only |
| 4 | /// from text leaves in the final open paragraph, after CommonMark parsing. |
| 5 | enum MarkdownDraft { |
| 6 | static func matches(_ pattern: String, _ text: String) -> Bool { |
| 7 | text.range(of: pattern, options: .regularExpression) != nil |
| 8 | } |
| 9 | static func entities(_ value: String) -> String { MarkdownEntities.link(value) } |
| 10 | static func decodeText(_ value: String) -> String { |
| 11 | let bytes = Array(value.utf8) |
| 12 | var out: [UInt8] = [], at = 0 |
| 13 | while at < bytes.count { |
| 14 | if bytes[at] == 92, at + 1 < bytes.count, (33...47).contains(bytes[at+1]) || (58...64).contains(bytes[at+1]) || (91...96).contains(bytes[at+1]) || (123...126).contains(bytes[at+1]) { at += 1 } |
| 15 | out.append(bytes[at]); at += 1 |
| 16 | } |
| 17 | return MarkdownEntities.text(String(decoding: out, as: UTF8.self)) |
| 18 | } |
| 19 | static func steady(_ source: String) -> String { |
| 20 | // CRLF is one Swift Character, but the web splits on the LF byte. |
| 21 | let bytes = Array(source.utf8) |
| 22 | let start = bytes.lastIndex(of: 10).map { $0 + 1 } ?? 0 |
| 23 | let tail = Array(bytes[start...]) |
| 24 | return markerOnly(tail) || fenceLine(tail) ? String(decoding: bytes[..<start], as: UTF8.self) : source |
| 25 | } |
| 26 | |
| 27 | private static func space(_ c: UInt8) -> Bool { c == 32 || c == 9 } |
| 28 | private static func suffix(_ b: [UInt8], _ start: Int) -> Bool { |
| 29 | var i = start |
| 30 | while i < b.count, space(b[i]) { i += 1 } |
| 31 | while i < b.count, [42, 95, 96].contains(b[i]) { i += 1 } |
| 32 | return i == b.count |
| 33 | } |
| 34 | // Each alternative is scanned once. No backtracking through whitespace. |
| 35 | static func markerOnly(_ b: [UInt8]) -> Bool { |
| 36 | var i = 0 |
| 37 | while i < b.count, space(b[i]) { i += 1 } |
| 38 | if suffix(b, i) { return true } |
| 39 | guard i < b.count else { return true } |
| 40 | if [45, 43, 42, 62].contains(b[i]), suffix(b, i + 1) { return true } |
| 41 | var j = i |
| 42 | while j < b.count, (48...57).contains(b[j]), j - i < 9 { j += 1 } |
| 43 | if j > i { |
| 44 | if j < b.count, [46, 41].contains(b[j]) { j += 1 } |
| 45 | if suffix(b, j) { return true } |
| 46 | } |
| 47 | j = i |
| 48 | while j < b.count, b[j] == 35, j - i < 6 { j += 1 } |
| 49 | if j > i, suffix(b, j) { return true } |
| 50 | j = i |
| 51 | while j < b.count, [96, 126].contains(b[j]) { j += 1 } |
| 52 | if j > i, suffix(b, j) { return true } |
| 53 | j = i |
| 54 | while j < b.count, [45, 61, 95, 42, 32, 9].contains(b[j]) { j += 1 } |
| 55 | return j > i && suffix(b, j) |
| 56 | } |
| 57 | static func fenceLine(_ b: [UInt8]) -> Bool { |
| 58 | var i = 0 |
| 59 | while i < b.count, space(b[i]) || b[i] == 62 { i += 1 } |
| 60 | func fence(_ j: Int) -> Bool { |
| 61 | j + 2 < b.count && (b[j] == 96 || b[j] == 126) && b[j+1] == b[j] && b[j+2] == b[j] |
| 62 | } |
| 63 | if fence(i) { return true } |
| 64 | guard i < b.count else { return false } |
| 65 | if [45, 43, 42].contains(b[i]) { i += 1 } |
| 66 | else { |
| 67 | let start = i |
| 68 | while i < b.count, (48...57).contains(b[i]), i - start < 9 { i += 1 } |
| 69 | guard i > start, i < b.count, [46, 41].contains(b[i]) else { return false } |
| 70 | i += 1 |
| 71 | } |
| 72 | guard i < b.count, space(b[i]) else { return false } |
| 73 | while i < b.count, space(b[i]) { i += 1 } |
| 74 | return fence(i) |
| 75 | } |
| 76 | } |
| 77 | |