Skip to content

Commit 3492b10

Browse files
Merge pull request #24 from PoshWeb/OpenGraph-Updates
OpenGraph 0.1.1
2 parents 0f55992 + 4017155 commit 3492b10

4 files changed

Lines changed: 108 additions & 89 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
Please:
2-
3-
* [Like](https://github.com/PoshWeb/OpenGraph)
4-
* [Share](https://github.com/PoshWeb/OpenGraph)
5-
* [Subscribe](https://github.com/PoshWeb/)
6-
* [Support](https://github.com/sponsors/StartAutomating)
7-
8-
---
9-
101
## OpenGraph 0.1.1
112

12-
* Simplified Module Scaffolding (#14)
13-
* Supporting open or closed tags (#15)
14-
* `Get-OpenGraph -Html` supports direct content (#16)
15-
* `Get-OpenGraph -Url` is now positional and pipeable (#17)
16-
* Improving README (#18)
3+
* `Get-OpenGraph` now supports unclosed `<meta>` tags (#23)
4+
* `Test-OpenGraph` and `Test-OGP` are aliases of `Get-OpenGraph` (#22).
5+
* Additionally, `Get-OpenGraph` now outputs `$false` when no OpenGraph tags are found.
6+
* `Get-OpenGraph` now accepts any pipeline input and number of arguments (#21)
7+
* `Get-OpenGraph` correctly outputs cached results (#20)
178

189
---
1910

Commands/Get-OpenGraph.ps1

Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@ function Get-OpenGraph
1818
'https://msnbc.com/',
1919
'https://fox.com/' |
2020
Get-OpenGraph
21-
.LINK
22-
https://ogp.me/
21+
.EXAMPLE
22+
OpenGraph https://posh.pckt.blog/static-sites-are-simple-6u51kgj
2323
#>
24-
[Alias('openGraph','ogp')]
24+
[Alias('openGraph','ogp','Test-OpenGraph', 'Test-OGP')]
2525
[CmdletBinding(PositionalBinding=$false)]
26-
param(
26+
param(
27+
# A list of any arguments.
28+
# This allows the command to take natural input.
29+
[Parameter(ValueFromRemainingArguments)]
30+
[Alias('Argument','Arguments','Args')]
31+
[PSObject[]]
32+
$ArgumentList,
33+
2734
# The URL that may contain Open Graph metadata
28-
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
29-
[Uri]
35+
[Parameter(ValueFromPipelineByPropertyName)]
36+
[string]
3037
$Url,
3138

3239
# Any HTML that may contain open graph metadata.
@@ -42,75 +49,93 @@ function Get-OpenGraph
4249
# If set, forces the function to retrieve the Open Graph metadata even if it is already cached.
4350
[Parameter(ValueFromPipelineByPropertyName)]
4451
[switch]
45-
$Force
52+
$Force,
53+
54+
# Any number of input objects
55+
[Parameter(ValueFromPipeline)]
56+
[Alias('Input')]
57+
[PSObject[]]
58+
$InputObject
4659
)
4760

4861
begin {
4962
# Make a regex to match meta tags
50-
# We will match both open and closed tags.
5163
$metaRegex = [Regex]::new('<meta.+?/?>','IgnoreCase','00:00:00.1')
52-
# If we do not have a cache
53-
if (-not $script:Cache) {
54-
# create one.
55-
$script:Cache = [Ordered]@{}
56-
}
64+
if (-not $script:OpenGraphCache) {
65+
$script:OpenGraphCache = [Ordered]@{}
66+
}
5767
}
5868

5969
process {
60-
# Declare an empty object to hold the Open Graph metadata
61-
$openGraphMetadata = [Ordered]@{PSTypeName='OpenGraph'}
62-
if ($Url -and -not $PSBoundParameters['html']) {
63-
# If the url is an absolute url with a scheme or http or https.
64-
if ($url.Scheme -in 'http', 'https') {
65-
# Get the url (if it is not cached).
66-
if (-not $script:Cache[$url] -or $Force) {
67-
$script:Cache[$url] =try {
68-
Invoke-RestMethod -Uri $Url
69-
} catch { $_ }
70-
}
71-
$html = $script:Cache[$url]
72-
}
73-
# Otherwise, see if the path exists
74-
elseif (Test-Path $url)
75-
{
76-
# and get content from that path.
77-
$html = Get-Content "$url" -Raw
78-
}
70+
# Turn any of our strongly bound parameters into arguments
71+
if ($Url) {
72+
$argumentList = @($argumentList) + $url
7973
}
8074

81-
# If we had any html,
82-
if ($html) {
83-
# find all of the `<meta>` tags.
84-
foreach ($match in $metaRegex.Matches($html)) {
85-
# Try to make them XML
86-
$matchXml = "$match" -as [xml]
87-
# If that fails,
88-
if (-not $matchXml) {
89-
# try once more after explicitly closing the end tag.
90-
$matchXml = $match -replace '>$', '/>' -as [xml]
75+
# If any data was provided
76+
if ($Data) {
77+
$argumentList = @($argumentList) + $Data
78+
}
79+
if ($InputObject) {
80+
$ArgumentList = @($ArgumentList) + $InputObject
81+
}
82+
83+
:nextArgument foreach ($argument in $argumentList) {
84+
# Declare an empty object to hold the Open Graph metadata
85+
if (-not $argument) { continue }
86+
$openGraphMetadata = [Ordered]@{PSTypeName='OpenGraph'}
87+
$url, $data = $null, $null
88+
89+
if ($argument -as [uri]) {
90+
$url = $argument -as [uri]
91+
} elseif ($argument -is [Collections.IDictionary]) {
92+
$data = $argument
93+
} else {
94+
Write-Warning "Only [uri] and [Collections.IDictionary] supported: $argument"
95+
continue nextArgument
96+
}
97+
98+
if ($Url) {
99+
if ($script:OpenGraphCache[$url] -and -not $Force) {
100+
foreach ($key in $script:OpenGraphCache[$url].Keys) {
101+
$openGraphMetadata[$key] =
102+
$script:OpenGraphCache[$url][$key]
103+
}
104+
([PSCustomObject]$script:OpenGraphCache[$url])
105+
continue nextArgument
91106
}
92-
# If the meta tag contained a property and content,
93-
if ($matchXml.meta.property -and $matchXml.meta.content) {
94-
# we will add it to our openGraph metadata.
95-
$openGraphMetadata[
96-
$matchXml.meta.property
97-
] = $matchXml.meta.content
107+
108+
$restResponse = Invoke-RestMethod -Uri $Url
109+
110+
foreach ($match in $metaRegex.Matches("$restResponse")) {
111+
$matchXml = (
112+
# close unclosed `<meta>` tags.
113+
"$match" -replace '/?>$','/>'
114+
) -as [xml]
115+
116+
if ($matchXml.meta.property -and $matchXml.meta.content) {
117+
$openGraphMetadata[$matchXml.meta.property] = $matchXml.meta.content
118+
}
98119
}
120+
121+
$script:OpenGraphCache[$url] = $openGraphMetadata
99122
}
100-
}
101123

102-
# If any data was provided
103-
if ($Data) {
104-
# copy it into open graph metadata
105-
foreach ($key in $Data.Keys) {
106-
$openGraphMetadata[$key] = $Data[$key]
124+
if ($Data) {
125+
foreach ($key in $Data.Keys) {
126+
$openGraphMetadata[$key] = $Data[$key]
127+
}
128+
}
129+
130+
# If there was no metadata
131+
if (-not $openGraphMetadata.Count) {
132+
# output false (so `Test-` verb scenarios are met)
133+
$false
134+
# and continue to the next argument.
135+
continue nextArgument
107136
}
108-
}
109-
110-
# If there was no open graph metadata, return nothing.
111-
if (-not $openGraphMetadata.Count) { return }
112137

113-
# Otherwise, return our OpenGraph metadata
114-
[PSCustomObject]$openGraphMetadata
138+
[PSCustomObject]$openGraphMetadata
139+
}
115140
}
116141
}

OpenGraph.psd1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
RootModule = 'OpenGraph.psm1'
33
ModuleVersion = '0.1.1'
44
GUID = 'be4e4070-1ea6-4a2e-8b6a-c6b7755e5ace'
5-
Author = 'JamesBrundage'
5+
Author = 'James Brundage'
66
CompanyName = 'Start-Automating'
7-
Copyright = '(c) 2025 Start-Automating'
7+
Copyright = '(c) 2025-2026 Start-Automating'
88
Description = 'Get OpenGraph with PowerShell'
99
FunctionsToExport = 'Get-OpenGraph'
10-
AliasesToExport = 'OpenGraph', 'ogp'
10+
AliasesToExport = 'OpenGraph', 'ogp', 'Test-OpenGraph', 'Test-OGP'
1111
TypesToProcess = 'OpenGraph.types.ps1xml'
1212
PrivateData = @{
1313
PSData = @{
@@ -17,11 +17,11 @@
1717
ReleaseNotes = @'
1818
## OpenGraph 0.1.1
1919
20-
* Simplified Module Scaffolding (#14)
21-
* Supporting open or closed tags (#15)
22-
* `Get-OpenGraph -Html` supports direct content (#16)
23-
* `Get-OpenGraph -Url` is now positional and pipeable (#17)
24-
* Improving README (#18)
20+
* `Get-OpenGraph` now supports unclosed `<meta>` tags (#23)
21+
* `Test-OpenGraph` and `Test-OGP` are aliases of `Get-OpenGraph` (#22).
22+
* Additionally, `Get-OpenGraph` now outputs `$false` when no OpenGraph tags are found.
23+
* `Get-OpenGraph` now accepts any pipeline input and number of arguments (#21)
24+
* `Get-OpenGraph` correctly outputs cached results (#20)
2525
2626
---
2727

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Import-Module OpenGraph -PassThru
2020
You can also clone the repo and import the module locally:
2121

2222
~~~PowerShell
23-
git clone https://github.com/PoshWeb/OpenGraph
23+
git clone https://github.com/PowerShellWeb/OpenGraph
2424
cd ./OpenGraph
2525
Import-Module ./ -PassThru
2626
~~~
@@ -40,10 +40,11 @@ This function retrieves the Open Graph metadata from a given URL and returns it
4040

4141
|Name|Type|Description|
4242
|-|-|-|
43-
|Url|Uri|The URL that may contain Open Graph metadata|
44-
|Html|String|Any HTML that may contain open graph metadata.|
43+
|ArgumentList|PSObject[]|A list of any arguments. <br/>This allows the command to take natural input.|
44+
|Url|String|The URL that may contain Open Graph metadata|
4545
|Data|IDictionary|A dictionary of additional Open Graph metadata to include in the result|
4646
|Force|SwitchParameter|If set, forces the function to retrieve the Open Graph metadata even if it is already cached.|
47+
|InputObject|PSObject[]|Any number of input objects|
4748

4849
##### Examples
4950
###### Example 1
@@ -57,15 +58,17 @@ Get-OpenGraph -Url https://abc.com/
5758
'https://fox.com/' |
5859
Get-OpenGraph
5960
~~~
60-
#### Links
61-
* [https://ogp.me/](https://ogp.me/)
61+
###### Example 3
62+
~~~PowerShell
63+
64+
~~~
6265
## Types
6366
### OpenGraph
6467
#### Members
6568
|Name|MemberType|
6669
|-|-|
6770
|[ToString](Types/OpenGraph/ToString.ps1)|ScriptMethod|
6871
|[get_HTML](Types/OpenGraph/get_HTML.ps1)|ScriptProperty|
69-
> (c) 2025 Start-Automating
72+
> (c) 2025-2026 Start-Automating
7073
71-
> [LICENSE](https://github.com/PoshWeb/OpenGraph/blob/main/LICENSE)
74+
> [LICENSE](https://github.com/PowerShellWeb/OpenGraph/blob/main/LICENSE)

0 commit comments

Comments
 (0)