Skip to content

Commit 11760b0

Browse files
author
James Brundage
committed
feat: Get-OpenGraph unclosed meta tags support ( Fixes #23 )
1 parent c1d5ca9 commit 11760b0

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

Commands/Get-OpenGraph.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ function Get-OpenGraph
1717
'https://cnn.com/',
1818
'https://msnbc.com/',
1919
'https://fox.com/' |
20-
Get-OpenGraph
20+
Get-OpenGraph
21+
.EXAMPLE
22+
OpenGraph https://posh.pckt.blog/static-sites-are-simple-6u51kgj
2123
#>
2224
[Alias('openGraph','ogp','Test-OpenGraph', 'Test-OGP')]
2325
[CmdletBinding(PositionalBinding=$false)]
@@ -53,7 +55,7 @@ function Get-OpenGraph
5355

5456
begin {
5557
# Make a regex to match meta tags
56-
$metaRegex = [Regex]::new('<meta.+?/>','IgnoreCase','00:00:00.1')
58+
$metaRegex = [Regex]::new('<meta.+?/?>','IgnoreCase','00:00:00.1')
5759
if (-not $script:OpenGraphCache) {
5860
$script:OpenGraphCache = [Ordered]@{}
5961
}
@@ -99,10 +101,14 @@ function Get-OpenGraph
99101
$restResponse = Invoke-RestMethod -Uri $Url
100102

101103
foreach ($match in $metaRegex.Matches("$restResponse")) {
102-
$matchXml = "$match" -as [xml]
104+
$matchXml = (
105+
# close unclosed `<meta>` tags.
106+
"$match" -replace '/?>$','/>'
107+
) -as [xml]
108+
103109
if ($matchXml.meta.property -and $matchXml.meta.content) {
104110
$openGraphMetadata[$matchXml.meta.property] = $matchXml.meta.content
105-
}
111+
}
106112
}
107113

108114
$script:OpenGraphCache[$url] = $openGraphMetadata

0 commit comments

Comments
 (0)