Skip to content

JSON-LD 0.1 : JSON-LD Caching #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
> Like It? [Star It](https://github.com/PowerShellWeb/JSON-LD)
> Love It? [Support It](https://github.com/sponsors/StartAutomating)

## JSON-LD 0.1

Caching JSON-LD requests

## JSON-LD 0.0.1

Get Linked Data from any page

* Initial Release of JSON-LD Module (#1)
* `Get-JsonLD` gets linked data (#2)
* `Get-JsonLD` is aliased to `jsonLD` and `json-ld`
20 changes: 18 additions & 2 deletions Commands/Get-JsonLD.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ function Get-JsonLD {
# The URL that may contain JSON-LD data
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Uri]
$Url
$Url,

# If set, will force the request to be made even if the URL has already been cached.
[switch]
$Force
)

begin {
# Create a pattern to match the JSON-LD script tag
$linkedDataRegex = [Regex]::new(@'
(?<HTML_LinkedData>
<script # Match <script tag
Expand All @@ -39,10 +44,21 @@ application/ld\+json # The type that indicates linked d
(?<JsonContent>(?:.|\s){0,}?(?=\z|</script>)) # Anything until the end tag is JSONContent
)
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')

# Initialize the cache for JSON-LD requests
if (-not $script:JsonLDRequestCache) {
$script:JsonLDRequestCache = [Ordered]@{}
}
}

process {
$restResponse = Invoke-RestMethod -Uri $Url
$restResponse =
if ($Force -or -not $script:JsonLDRequestCache[$url]) {
$script:JsonLDRequestCache[$url] = Invoke-RestMethod -Uri $Url
$script:JsonLDRequestCache[$url]
} else {
$script:JsonLDRequestCache[$url]
}
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
foreach ($jsonObject in
$match.Groups['JsonContent'].Value |
Expand Down
10 changes: 7 additions & 3 deletions JSON-LD.psd1
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@{
RootModule = 'JSON-LD.psm1'
ModuleVersion = '0.0.1'
ModuleVersion = '0.1'
GUID = '4e65477c-012c-4077-87c7-3e07964636ce'
Author = 'JamesBrundage'
Author = 'James Brundage'
CompanyName = 'Start-Automating'
Copyright = '(c) 2025 Start-Automating.'
Description = 'Get JSON Linked Data with PowerShell'
FunctionsToExport = 'Get-JsonLD'
AliasesToExport = 'jsonLD', 'json-ld'
AliasesToExport = 'jsonLD', 'json-ld'
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Expand All @@ -20,6 +20,10 @@
> Like It? [Star It](https://github.com/PowerShellWeb/JSON-LD)
> Love It? [Support It](https://github.com/sponsors/StartAutomating)

## JSON-LD 0.1

Caching JSON-LD requests

Get Linked Data from any page

## JSON-LD 0.0.1
Expand Down
Loading