Created
February 27, 2025 20:26
-
-
Save anotherlab/9d93a2b3d74848b0801f9fa57377510a to your computer and use it in GitHub Desktop.
Powershell script to strip out invalid Front Matter tags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory = $true)][string]$localPath | |
) | |
# Imported tags that we need to filter out | |
$bad = @("id", "guid", "permalink", "layout", "collect_box_size") | |
# Get all of the matching file names | |
$MatchingFileNames = (Get-ChildItem $localPath -File) | sort-Object Name | |
$UpdatedFileCount = 0 | |
$fileCounter = 0 | |
$totalFiles = $MatchingFileNames.Count | |
foreach($MatchingFileName in $MatchingFileNames) | |
{ | |
$fileCounter++ | |
$percentComplete = [math]::Floor(($fileCounter / $totalFiles) * 100) | |
Write-Progress -Activity "Processing files" -PercentComplete $percentComplete | |
# read the data in | |
$file_data = Get-Content $MatchingFileName | |
# Simple check to make sure we start with a front matter file | |
$IsFrontMatter = $file_data[0] -like "---" | |
if ($IsFrontMatter -eq $True) | |
{ | |
# read the file | |
$new_data = New-Object System.Collections.Generic.List[System.Object] | |
# get the line count | |
$lineCount = $file_data.Length | |
# Write-Host ('Reading ' + $MatchingFileName.FullName ) | |
$CurentLine = 0 | |
$FMCount = 0 | |
$writefile = $False | |
# while not at the end of the file | |
while ($CurentLine -lt $lineCount) | |
{ | |
# curent line | |
$ThisLine = $file_data[$CurentLine] | |
# while still in the Front Matter blokc | |
if ($CurentLine -gt 0 -and $FMCount -eq 0) | |
{ | |
# Are we still in Front Matter? | |
if ($file_data[$CurentLine] -like "---") | |
{ | |
$FMCount++ | |
} | |
# Still in Front Matter? | |
if ($FMCount -eq 0) | |
{ | |
# Read each line and look for invalid tokens | |
$allow = $true | |
foreach($token in $bad) | |
{ | |
if ($ThisLine -match ("^" + $token)) | |
{ | |
$allow = $False | |
$writefile = $True | |
# then skip over any settings after each token that are for that token | |
while ($file_data[$CurentLine+1] -match ("^ - ")) | |
{ | |
$CurentLine++ | |
} | |
} | |
} | |
# Only include the allowed Front Matter tokens | |
if ($allow -eq $True) | |
{ | |
$new_data.Add($ThisLine) | |
} | |
} | |
# otherwise just include the line | |
else { | |
$new_data.Add($ThisLine) | |
} | |
} | |
# No longer on front matter, add the line | |
else | |
{ | |
$new_data.Add($ThisLine) | |
} | |
$CurentLine++ | |
} | |
if ($writefile -eq $true) | |
{ | |
$UpdatedFileCount++ | |
Out-File -FilePath $MatchingFileName -InputObject $new_data | |
} | |
} | |
} | |
write-host $UpdatedFileCount " files updated out of " $MatchingFileNames.Count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment