Skip to content

Commit 0f55992

Browse files
Merge pull request #19 from PoshWeb/opengraph-improvements
OpenGraph 0.1.1
2 parents 957fcc7 + 04ff585 commit 0f55992

9 files changed

Lines changed: 477 additions & 114 deletions

File tree

.github/workflows/OpenGraph-Build.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
Install-Module -Name Pester -Repository PSGallery -Force -Scope CurrentUser -MaximumVersion $PesterMaxVersion -SkipPublisherCheck -AllowClobber
3636
Import-Module Pester -Force -PassThru -MaximumVersion $PesterMaxVersion} @Parameters
3737
- name: Check out repository
38-
uses: actions/checkout@v4
38+
uses: actions/checkout@v2
3939
- name: RunPester
4040
id: RunPester
4141
shell: pwsh
@@ -92,6 +92,9 @@ jobs:
9292
$result =
9393
Invoke-Pester -PassThru -Verbose -OutputFile ".\$moduleName.TestResults.xml" -OutputFormat NUnitXml @codeCoverageParameters
9494
95+
"::set-output name=TotalCount::$($result.TotalCount)",
96+
"::set-output name=PassedCount::$($result.PassedCount)",
97+
"::set-output name=FailedCount::$($result.FailedCount)" | Out-Host
9598
if ($result.FailedCount -gt 0) {
9699
"::debug:: $($result.FailedCount) tests failed"
97100
foreach ($r in $result.TestResult) {
@@ -490,21 +493,7 @@ jobs:
490493
if: ${{ success() }}
491494
steps:
492495
- name: Check out repository
493-
uses: actions/checkout@v2
494-
- name: GitLogger
495-
uses: GitLogging/GitLoggerAction@main
496-
id: GitLogger
497-
- name: Use PSSVG Action
498-
uses: StartAutomating/PSSVG@main
499-
id: PSSVG
500-
- name: Use PipeScript Action
501-
uses: StartAutomating/PipeScript@main
502-
id: PipeScript
496+
uses: actions/checkout@main
503497
- name: UseEZOut
504498
uses: StartAutomating/EZOut@master
505-
- name: UseHelpOut
506-
uses: StartAutomating/HelpOut@master
507-
- name: Use PSJekyll Action
508-
uses: PowerShellWeb/PSJekyll@main
509-
id: PSJekyll
510499

Build/GitHub/Jobs/OpenGraph.psd1

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,8 @@
44
steps = @(
55
@{
66
name = 'Check out repository'
7-
uses = 'actions/checkout@v2'
8-
},
9-
@{
10-
name = 'GitLogger'
11-
uses = 'GitLogging/GitLoggerAction@main'
12-
id = 'GitLogger'
13-
},
14-
@{
15-
name = 'Use PSSVG Action'
16-
uses = 'StartAutomating/PSSVG@main'
17-
id = 'PSSVG'
18-
},
19-
@{
20-
name = 'Use PipeScript Action'
21-
uses = 'StartAutomating/PipeScript@main'
22-
id = 'PipeScript'
23-
},
24-
'RunEZOut',
25-
'RunHelpOut',
26-
@{
27-
name = 'Use PSJekyll Action'
28-
uses = 'PowerShellWeb/PSJekyll@main'
29-
id = 'PSJekyll'
30-
}
7+
uses = 'actions/checkout@main'
8+
}
9+
'RunEZOut'
3110
)
3211
}

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
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+
10+
## OpenGraph 0.1.1
11+
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)
17+
18+
---
19+
20+
## OpenGraph 0.1
21+
22+
* `OpenGraph.ToString()` now returns HTML (#10)
23+
* `Get-OpenGraph` now caches results (#11)
24+
25+
---
26+
127
## OpenGraph 0.0.1
228

329
* Initial Release of OpenGraph Module (#1)

Commands/Get-OpenGraph.ps1

Lines changed: 53 additions & 12 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/
2123
#>
2224
[Alias('openGraph','ogp')]
2325
[CmdletBinding(PositionalBinding=$false)]
2426
param(
2527
# The URL that may contain Open Graph metadata
26-
[Parameter(ValueFromPipelineByPropertyName)]
28+
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
2729
[Uri]
2830
$Url,
2931

32+
# Any HTML that may contain open graph metadata.
33+
[Parameter(ValueFromPipelineByPropertyName)]
34+
[string]
35+
$Html,
36+
3037
# A dictionary of additional Open Graph metadata to include in the result
3138
[Parameter(ValueFromPipelineByPropertyName)]
3239
[Collections.IDictionary]
@@ -40,36 +47,70 @@ function Get-OpenGraph
4047

4148
begin {
4249
# Make a regex to match meta tags
43-
$metaRegex = [Regex]::new('<meta.+?/>','IgnoreCase','00:00:00.1')
44-
if (-not $script:OpenGraphCache) {
45-
$script:OpenGraphCache = [Ordered]@{}
46-
}
50+
# We will match both open and closed tags.
51+
$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+
}
4757
}
4858

4959
process {
5060
# Declare an empty object to hold the Open Graph metadata
5161
$openGraphMetadata = [Ordered]@{PSTypeName='OpenGraph'}
52-
if ($Url) {
53-
if ($script:OpenGraphCache[$url] -and -not $Force) {
54-
return $script:OpenGraphCache[$url]
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
5578
}
56-
$restResponse = Invoke-RestMethod -Uri $Url
57-
foreach ($match in $metaRegex.Matches("$restResponse")) {
79+
}
80+
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
5886
$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]
91+
}
92+
# If the meta tag contained a property and content,
5993
if ($matchXml.meta.property -and $matchXml.meta.content) {
60-
$openGraphMetadata[$matchXml.meta.property] = $matchXml.meta.content
94+
# we will add it to our openGraph metadata.
95+
$openGraphMetadata[
96+
$matchXml.meta.property
97+
] = $matchXml.meta.content
6198
}
6299
}
63-
$script:OpenGraphCache[$url] = $openGraphMetadata
64100
}
101+
102+
# If any data was provided
65103
if ($Data) {
104+
# copy it into open graph metadata
66105
foreach ($key in $Data.Keys) {
67106
$openGraphMetadata[$key] = $Data[$key]
68107
}
69108
}
70109

110+
# If there was no open graph metadata, return nothing.
71111
if (-not $openGraphMetadata.Count) { return }
72112

113+
# Otherwise, return our OpenGraph metadata
73114
[PSCustomObject]$openGraphMetadata
74115
}
75116
}

OpenGraph.ps.psm1

Lines changed: 0 additions & 26 deletions
This file was deleted.

OpenGraph.psd1

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'OpenGraph.psm1'
3-
ModuleVersion = '0.1'
3+
ModuleVersion = '0.1.1'
44
GUID = 'be4e4070-1ea6-4a2e-8b6a-c6b7755e5ace'
55
Author = 'JamesBrundage'
66
CompanyName = 'Start-Automating'
@@ -11,21 +11,28 @@
1111
TypesToProcess = 'OpenGraph.types.ps1xml'
1212
PrivateData = @{
1313
PSData = @{
14-
Tags = @('OpenGraph','SEO','Web','PowerShellWeb' )
15-
ProjectURI = 'https://github.com/PowerShellWeb/OpenGraph'
16-
LicenseURI = 'https://github.com/PowerShellWeb/OpenGraph/blob/main/LICENSE'
14+
Tags = @('OpenGraph','SEO','Web','PoshWeb','OpenGraphProtocol')
15+
ProjectURI = 'https://github.com/PoshWeb/OpenGraph'
16+
LicenseURI = 'https://github.com/PoshWeb/OpenGraph/blob/main/LICENSE'
1717
ReleaseNotes = @'
18-
> Like It? [Star It](https://github.com/PowerShellWeb/OpenGraph)
19-
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
18+
## OpenGraph 0.1.1
2019
21-
## OpenGraph 0.1
22-
23-
* `OpenGraph.ToString()` now returns HTML (#10)
24-
* `Get-OpenGraph` now caches results (#11)
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)
2525
2626
---
2727
28-
Additional release notes can be found at [CHANGELOG.md](https://github.com/PowerShellWeb/OpenGraph/blob/main/CHANGELOG.md)
28+
Please:
29+
30+
* [Like](https://github.com/PoshWeb/OpenGraph)
31+
* [Share](https://github.com/PoshWeb/OpenGraph)
32+
* [Subscribe](https://github.com/PoshWeb/)
33+
* [Support](https://github.com/sponsors/StartAutomating)
34+
35+
Additional history found available in the [CHANGELOG.md](https://github.com/PoshWeb/OpenGraph/blob/main/CHANGELOG.md)
2936
'@
3037
}
3138
}

OpenGraph.psm1

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@ $commandsPath = Join-Path $PSScriptRoot Commands
1010
. $file.FullName
1111
}
1212

13-
$myModule = $MyInvocation.MyCommand.ScriptBlock.Module
14-
$ExecutionContext.SessionState.PSVariable.Set($myModule.Name, $myModule)
15-
$myModule.pstypenames.insert(0, $myModule.Name)
16-
17-
New-PSDrive -Name $MyModule.Name -PSProvider FileSystem -Scope Global -Root $PSScriptRoot -ErrorAction Ignore
18-
19-
if ($home) {
20-
$MyModuleProfileDirectory = Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) $MyModule.Name
21-
if (-not (Test-Path $MyModuleProfileDirectory)) {
22-
$null = New-Item -ItemType Directory -Path $MyModuleProfileDirectory -Force
23-
}
24-
New-PSDrive -Name "My$($MyModule.Name)" -PSProvider FileSystem -Scope Global -Root $MyModuleProfileDirectory -ErrorAction Ignore
25-
}
26-
27-
# Set a script variable of this, set to the module
28-
# (so all scripts in this scope default to the correct `$this`)
29-
$script:this = $myModule
30-
31-
#region Custom
32-
#endregion Custom
33-
34-
Export-ModuleMember -Alias * -Function * -Variable $myModule.Name
13+
Export-ModuleMember -Alias * -Function *
3514

3615

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
11
# OpenGraph
2+
[![OpenGraph](https://img.shields.io/powershellgallery/dt/OpenGraph)](https://www.powershellgallery.com/packages/OpenGraph/)
3+
## Get OpenGraph with PowerShell
24

3-
[OpenGraph](https://ogp.me) is a protocol for embedding information in a page search optimization and social media.
5+
## Installing and Importing
46

5-
The OpenGraph module lets you see this information for any page that has it.
7+
You can install OpenGraph from the [PowerShell gallery](https://powershellgallery.com/)
68

79
~~~PowerShell
8-
Get-OpenGraph https://abc.com/
10+
Install-Module OpenGraph -Scope CurrentUser -Force
911
~~~
1012

13+
Once installed, you can import the module with:
14+
15+
~~~PowerShell
16+
Import-Module OpenGraph -PassThru
17+
~~~
18+
19+
20+
You can also clone the repo and import the module locally:
21+
22+
~~~PowerShell
23+
git clone https://github.com/PoshWeb/OpenGraph
24+
cd ./OpenGraph
25+
Import-Module ./ -PassThru
26+
~~~
27+
28+
## Functions
29+
OpenGraph has 1 function
30+
### Get-OpenGraph
31+
#### Gets Open Graph metadata for a given URL.
32+
Gets Open Graph metadata for a given URL.
33+
34+
[Open Graph](https://ogp.me/) is a protocol that enables any web page to become a rich object in a social graph.
35+
36+
It is used many social networks to display rich content when links are shared.
37+
38+
This function retrieves the Open Graph metadata from a given URL and returns it as a custom object.
39+
##### Parameters
40+
41+
|Name|Type|Description|
42+
|-|-|-|
43+
|Url|Uri|The URL that may contain Open Graph metadata|
44+
|Html|String|Any HTML that may contain open graph metadata.|
45+
|Data|IDictionary|A dictionary of additional Open Graph metadata to include in the result|
46+
|Force|SwitchParameter|If set, forces the function to retrieve the Open Graph metadata even if it is already cached.|
47+
48+
##### Examples
49+
###### Example 1
50+
~~~PowerShell
51+
Get-OpenGraph -Url https://abc.com/
52+
~~~
53+
###### Example 2
54+
~~~PowerShell
55+
'https://cnn.com/',
56+
'https://msnbc.com/',
57+
'https://fox.com/' |
58+
Get-OpenGraph
59+
~~~
60+
#### Links
61+
* [https://ogp.me/](https://ogp.me/)
62+
## Types
63+
### OpenGraph
64+
#### Members
65+
|Name|MemberType|
66+
|-|-|
67+
|[ToString](Types/OpenGraph/ToString.ps1)|ScriptMethod|
68+
|[get_HTML](Types/OpenGraph/get_HTML.ps1)|ScriptProperty|
69+
> (c) 2025 Start-Automating
70+
71+
> [LICENSE](https://github.com/PoshWeb/OpenGraph/blob/main/LICENSE)

0 commit comments

Comments
 (0)