From 4701a0ebb9915349c7d57edd582093fb2e5df9d0 Mon Sep 17 00:00:00 2001 From: MuhtasimTanmoy Date: Wed, 23 Jun 2021 23:29:23 +0600 Subject: [PATCH 1/3] Fix parse the content for openGraph image for github --- CHANGELOG.md | 2 ++ Sources/Regex.swift | 1 + Sources/StringExtension.swift | 6 +++++- Sources/SwiftLinkPreview.swift | 24 +++++++++++++----------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9f8e7..9111a04 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ - Changed by [lhunath](https://github.com/lhunath) - fixed youtube and open graph tags image metadata issues - Changed by [nafis042](https://github.com/nafis042) +- Fixed github link image for `og:image` property. + - Changed by [MuhtasimTanmoy](https://github.com/MuhtasimTanmoy/) ## [3.2.0](https://github.com/LeonardoCardoso/Swift-Link-Preview/releases/tag/3.2.0) diff --git a/Sources/Regex.swift b/Sources/Regex.swift index 3675e38..c0c2725 100644 --- a/Sources/Regex.swift +++ b/Sources/Regex.swift @@ -11,6 +11,7 @@ import Foundation class Regex { static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)$" + static let openGraphImagePattern = "(.+?)\\.(gif||jpg|jpeg|png|bmp)$" static let videoTagPattern = "]+src=\"([^\"]+)" static let secondaryVideoTagPattern = "og:video\" content=\"([^\"](.+?))\"(.+?)[/]?>" static let imageTagPattern = "" diff --git a/Sources/StringExtension.swift b/Sources/StringExtension.swift index b4ffe90..8f71c79 100644 --- a/Sources/StringExtension.swift +++ b/Sources/StringExtension.swift @@ -103,7 +103,11 @@ extension String { return Regex.test(self, regex: Regex.imagePattern) } - + + func isOpenGraphImage() -> Bool { + return Regex.test(self, regex: Regex.openGraphImagePattern) + } + func isVideo() -> Bool { return Regex.test(self, regex: Regex.videoTagPattern) } diff --git a/Sources/SwiftLinkPreview.swift b/Sources/SwiftLinkPreview.swift index cb0839c..a4626a5 100644 --- a/Sources/SwiftLinkPreview.swift +++ b/Sources/SwiftLinkPreview.swift @@ -556,7 +556,7 @@ extension SwiftLinkPreview { let value = value.decoded.extendedTrim if tag == "image" { let value = addImagePrefixIfNeeded(value, result: result) - if value.isImage() { result.set(value, for: key) } + if value.isOpenGraphImage(){ result.set(value, for: key) } } else if tag == "video" { let value = addImagePrefixIfNeeded(value, result: result) if value.isVideo() { result.set(value, for: key) } @@ -621,20 +621,22 @@ extension SwiftLinkPreview { let images = result.images if images == nil || images?.isEmpty ?? true { - let values = Regex.pregMatchAll(htmlCode, regex: Regex.imageTagPattern, index: 2) - if !values.isEmpty { - let imgs = values.map { self.addImagePrefixIfNeeded($0, result: result) } - result.images = imgs - result.image = imgs.first - } - else{ - let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1) + // Should look for first instead of tag. + let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1) + if !values.isEmpty { + result.images = values + result.image = values.first + } else { + // If no OpenGraph image found pick any from tag to show. + let values = Regex.pregMatchAll(htmlCode, regex: Regex.imageTagPattern, index: 2) if !values.isEmpty { - result.images = values - result.image = values.first + let imgs = values.map { self.addImagePrefixIfNeeded($0, result: result) } + result.images = imgs + result.image = imgs.first } } + } } else { let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1) From 96024c114b58cbc9c702709c8bfa1e4f002cefa4 Mon Sep 17 00:00:00 2001 From: Leonardo Cardoso Date: Tue, 27 Jul 2021 16:50:20 +0200 Subject: [PATCH 2/3] Fix updated regexes --- Sources/Regex.swift | 4 ++-- Sources/SwiftLinkPreview.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Regex.swift b/Sources/Regex.swift index 0ffa811..c4e08dc 100644 --- a/Sources/Regex.swift +++ b/Sources/Regex.swift @@ -13,9 +13,9 @@ class Regex { static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)$" static let openGraphImagePattern = "(.+?)\\.(gif||jpg|jpeg|png|bmp)$" static let videoTagPattern = "]+src=\"([^\"]+)" - static let secondaryVideoTagPattern = "og:video\".*content=\"(.+?)\".*>" + static let secondaryVideoTagPattern = "og:video\"(.+?)content=\"([^\"](.+?))\"(.+?)[/]?>" static let imageTagPattern = "" - static let secondaryImageTagPattern = "og:image\".*content=\"(.+?)\".*>" + static let secondaryImageTagPattern = "og:image\"(.+?)content=\"([^\"](.+?))\"(.+?)[/]?>" static let titlePattern = "(.*?)" static let metatagPattern = "" static let metatagContentPattern = "content=(\"(.*?)\")|('(.*?)')" diff --git a/Sources/SwiftLinkPreview.swift b/Sources/SwiftLinkPreview.swift index 10b1deb..58b4d86 100644 --- a/Sources/SwiftLinkPreview.swift +++ b/Sources/SwiftLinkPreview.swift @@ -623,7 +623,7 @@ extension SwiftLinkPreview { if images == nil || images?.isEmpty ?? true { // Should look for first instead of tag. - let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1) + let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 2) if !values.isEmpty { result.images = values result.image = values.first @@ -639,7 +639,7 @@ extension SwiftLinkPreview { } } else { - let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1) + let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 2) if !values.isEmpty { result.images = values result.image = values.first From 780191437238abd402c694e39d639a9194aec5b6 Mon Sep 17 00:00:00 2001 From: Leonardo Cardoso Date: Tue, 27 Jul 2021 17:14:48 +0200 Subject: [PATCH 3/3] Update examples --- .../SwiftLinkPreviewExample/Controllers/ViewController.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Example/SwiftLinkPreviewExample/Controllers/ViewController.swift b/Example/SwiftLinkPreviewExample/Controllers/ViewController.swift index 04408ee..3bb2d28 100644 --- a/Example/SwiftLinkPreviewExample/Controllers/ViewController.swift +++ b/Example/SwiftLinkPreviewExample/Controllers/ViewController.swift @@ -38,6 +38,8 @@ class ViewController: UIViewController { "www.youtube.com", "www.google.com", "facebook.com", + + "https://github.com/LeonardoCardoso/SwiftLinkPreview", "https://leocardz.com/swift-link-preview-5a9860c7756f", "NASA! 🖖🏽 https://www.nasa.gov/", @@ -57,7 +59,6 @@ class ViewController: UIViewController { "Youtube?! It does! https://www.youtube.com/watch?v=cv2mjAgFTaI", "Also Vimeo https://vimeo.com/67992157", - "Even with image itself https://lh6.googleusercontent.com/-aDALitrkRFw/UfQEmWPMQnI/AAAAAAAFOlQ/mDh1l4ej15k/w337-h697-no/db1969caa4ecb88ef727dbad05d5b5b3.jpg", "Well, it's a gif! https://goo.gl/jKCPgp" ]