@@ -22,23 +22,36 @@ extension InlineNode {
22
22
}
23
23
24
24
extension InlineNode {
25
- @available ( iOS 16 . 0 , macOS 13 . 0 , tvOS 13 . 0 , watchOS 6 . 0 , * )
26
25
var size : MarkdownImageSize ? {
27
26
switch self {
28
27
case . text( let input) :
29
- 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*)?\}/
28
+ 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*)? \\ } "
30
29
31
- if let match = input. wholeMatch ( of: pattern) {
32
- let widthParts = [ match. output. 1 , match. output. 3 ] . compactMap { $0 }
33
- let heightParts = [ match. output. 2 , match. output. 4 ] . compactMap { $0 }
30
+ guard let regex = try ? NSRegularExpression ( pattern: pattern, options: [ ] ) else {
31
+ return nil
32
+ }
33
+
34
+ let range = NSRange ( input. startIndex..< input. endIndex, in: input)
35
+ guard let match = regex. firstMatch ( in: input, options: [ ] , range: range) else {
36
+ return nil
37
+ }
34
38
35
- let width = widthParts . compactMap { Float ( String ( $0 ) ) } . last
36
- let height = heightParts . compactMap { Float ( String ( $0 ) ) } . last
39
+ var width : CGFloat ?
40
+ var height : CGFloat ?
37
41
38
- return MarkdownImageSize ( width: width. map ( CGFloat . init) , height: height. map ( CGFloat . init) )
42
+ if let widthRange = Range ( match. range ( at: 1 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
43
+ width = CGFloat ( widthValue)
44
+ } else if let widthRange = Range ( match. range ( at: 3 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
45
+ width = CGFloat ( widthValue)
39
46
}
40
47
41
- return nil
48
+ if let heightRange = Range ( match. range ( at: 2 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
49
+ height = CGFloat ( heightValue)
50
+ } else if let heightRange = Range ( match. range ( at: 4 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
51
+ height = CGFloat ( heightValue)
52
+ }
53
+
54
+ return MarkdownImageSize ( width: width, height: height)
42
55
default :
43
56
return nil
44
57
}
0 commit comments