Skip to content

Commit

Permalink
Use NSRegularExpression to support below iOS 16
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1893 committed Dec 20, 2024
1 parent d8e0552 commit d99a1fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.8
// swift-tools-version:5.6

import PackageDescription

Expand Down Expand Up @@ -31,7 +31,9 @@ let package = Package(
.product(name: "NetworkImage", package: "NetworkImage"),
],
swiftSettings: [
.enableUpcomingFeature("BareSlashRegexLiterals"),
.unsafeFlags([
"-enable-bare-slash-regex",
]),
]
),
.testTarget(
Expand Down
31 changes: 22 additions & 9 deletions Sources/MarkdownUI/Utility/InlineNode+RawImageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,36 @@ extension InlineNode {
}

extension InlineNode {
@available(iOS 16.0, macOS 13.0, tvOS 13.0, watchOS 6.0, *)
var size: MarkdownImageSize? {
switch self {
case .text(let input):
let pattern = /{(?:width\s*=\s*(\d+)px\s*)?(?:height\s*=\s*(\d+)px\s*)?(?:width\s*=\s*(\d+)px\s*)?(?:height\s*=\s*(\d+)px\s*)?\}/
let pattern = "\\{(?:width\\s*=\\s*(\\d+)px\\s*)?(?:height\\s*=\\s*(\\d+)px\\s*)?(?:width\\s*=\\s*(\\d+)px\\s*)?(?:height\\s*=\\s*(\\d+)px\\s*)?\\}"

if let match = input.wholeMatch(of: pattern) {
let widthParts = [match.output.1, match.output.3].compactMap { $0 }
let heightParts = [match.output.2, match.output.4].compactMap { $0 }
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return nil
}

let range = NSRange(input.startIndex..<input.endIndex, in: input)
guard let match = regex.firstMatch(in: input, options: [], range: range) else {
return nil
}

let width = widthParts.compactMap { Float(String($0)) }.last
let height = heightParts.compactMap { Float(String($0)) }.last
var width: CGFloat?
var height: CGFloat?

return MarkdownImageSize(width: width.map(CGFloat.init), height: height.map(CGFloat.init))
if let widthRange = Range(match.range(at: 1), in: input), let widthValue = Int(input[widthRange]) {
width = CGFloat(widthValue)
} else if let widthRange = Range(match.range(at: 3), in: input), let widthValue = Int(input[widthRange]) {
width = CGFloat(widthValue)
}

return nil
if let heightRange = Range(match.range(at: 2), in: input), let heightValue = Int(input[heightRange]) {
height = CGFloat(heightValue)
} else if let heightRange = Range(match.range(at: 4), in: input), let heightValue = Int(input[heightRange]) {
height = CGFloat(heightValue)
}

return MarkdownImageSize(width: width, height: height)
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MarkdownUI/Views/Inlines/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct ImageView: View {

extension ImageView {
init?(_ inlines: [InlineNode]) {
if inlines.count == 2, #available(iOS 16.0, macOS 13.0, tvOS 16.0, *), let data = inlines.first?.imageData, let size = inlines.last?.size {
if inlines.count == 2, let data = inlines.first?.imageData, let size = inlines.last?.size {
self.init(data: data, size: size)
}
else if inlines.count == 1, let data = inlines.first?.imageData {
Expand Down

0 comments on commit d99a1fa

Please sign in to comment.