Skip to content

Commit c4ff83b

Browse files
committed
✨ (HugoHelpers.ps1, Utilities.ps1): add Update-HashtableList function and ConvertTo-CamelCase utility
✅ (Update-ReourcesFrontMatter.ps1): improve progress logging and refactor resource loading Introduce the `Update-HashtableList` function to handle ordered dictionaries, allowing for more complex data structures in front matter updates. Add `ConvertTo-CamelCase` utility to facilitate string manipulation. Enhance progress logging in `Update-ReourcesFrontMatter.ps1` by calculating and displaying progress in 10% increments, improving user feedback during long operations. These changes aim to improve functionality and user experience when managing markdown resources.
1 parent de612e2 commit c4ff83b

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

.powershell/_includes/HugoHelpers.ps1

+88
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,94 @@ function Update-Field {
115115
return
116116
}
117117

118+
function Update-HashtableList {
119+
param (
120+
[Parameter(Mandatory = $true)]
121+
[System.Collections.Specialized.OrderedDictionary]$frontMatter,
122+
[Parameter(Mandatory = $true)]
123+
[string]$fieldName,
124+
[Parameter(Mandatory = $true)]
125+
[AllowEmptyCollection()]
126+
[hashtable[]]$values, # Accepts only an array of hashtables
127+
[string]$addAfter = $null,
128+
[string]$addBefore = $null,
129+
[switch]$Overwrite
130+
)
131+
132+
# Ensure values are always an array and remove any null values
133+
$values = @($values | Where-Object { $_ -ne $null })
134+
135+
# Convert all input hashtables to ordered hashtables while keeping their order intact
136+
$values = $values | ForEach-Object {
137+
if ($_ -is [System.Collections.Specialized.OrderedDictionary]) { $_ }
138+
else {
139+
$orderedHash = [ordered]@{}
140+
$_.GetEnumerator() | ForEach-Object { $orderedHash[$_.Key] = $_.Value }
141+
$orderedHash
142+
}
143+
}
144+
145+
# If the field doesn't exist, create it with position handling
146+
if (-not $frontMatter.Contains($fieldName)) {
147+
$index = $null
148+
if ($addAfter -and $frontMatter.Contains($addAfter)) {
149+
$index = $frontMatter.Keys.IndexOf($addAfter) + 1
150+
}
151+
elseif ($addBefore -and $frontMatter.Contains($addBefore)) {
152+
$index = $frontMatter.Keys.IndexOf($addBefore)
153+
}
154+
155+
if ($index -ne $null) {
156+
$frontMatter.Insert($index, $fieldName, $values)
157+
}
158+
else {
159+
$frontMatter[$fieldName] = $values
160+
}
161+
162+
Write-Debug "$fieldName added"
163+
}
164+
else {
165+
# Ensure the field is always an array
166+
if (-not ($frontMatter[$fieldName] -is [System.Collections.IEnumerable])) {
167+
$frontMatter[$fieldName] = @($frontMatter[$fieldName])
168+
}
169+
170+
if ($Overwrite) {
171+
$frontMatter[$fieldName] = $values
172+
}
173+
else {
174+
# Preserve the existing values as an ordered array
175+
$existingValues = @($frontMatter[$fieldName])
176+
177+
# Create a lookup table of existing hashtables for deduplication
178+
$existingHashtablesJson = @{}
179+
foreach ($hash in $existingValues) {
180+
$existingHashtablesJson[(ConvertTo-Json $hash -Compress)] = $true
181+
}
182+
183+
# Append only unique hashtables in original order
184+
foreach ($hash in $values) {
185+
$hashJson = ConvertTo-Json $hash -Compress
186+
if (-not $existingHashtablesJson.ContainsKey($hashJson)) {
187+
$existingHashtablesJson[$hashJson] = $true
188+
$existingValues += $hash
189+
}
190+
}
191+
192+
# Maintain the original order exactly
193+
$frontMatter[$fieldName] = $existingValues
194+
Write-Debug "$fieldName updated with new unique values"
195+
}
196+
}
197+
198+
# Ensure the field remains an array even if it has only one value
199+
if ($frontMatter[$fieldName] -isnot [array]) {
200+
$frontMatter[$fieldName] = @($frontMatter[$fieldName])
201+
}
202+
}
203+
204+
205+
118206
# Update-List function to have the same signature as Update-Field
119207
# Update-List -frontMatter $frontMatter -fieldName 'tags' -values @('DevOps', 'Agile', 'Scrum')
120208
function Update-StringList {

.powershell/_includes/Utilities.ps1

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
function ConvertTo-CamelCase {
4+
param (
5+
[string]$Text
6+
)
7+
8+
# Split words by spaces or non-alphanumeric characters
9+
$words = $Text -split '\W+' | Where-Object { $_ -match '\w' } # Remove empty values
10+
11+
if ($words.Count -eq 0) { return '' } # Handle empty input
12+
13+
# Ensure first word is lowercase, and capitalize subsequent words properly
14+
$camelCase = $words[0].ToLower() + ($words[1..($words.Count - 1)] | ForEach-Object {
15+
if ($_.Length -gt 0) {
16+
$_.Substring(0, 1).ToUpper() + $_.Substring(1).ToLower()
17+
}
18+
}) -join ''
19+
20+
return $camelCase
21+
}
22+
23+
Write-Debug "Utilities.ps1 loaded"

.powershell/single-use/resources/Update-ReourcesFrontMatter.ps1

+16-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ $Counter = 1
2121
$categoriesCatalog = Get-CatalogHashtable -Classification "categories"
2222
$tagsCatalog = Get-CatalogHashtable -Classification "tags"
2323

24+
Write-InformationLog "Loading ({count}) markdown files...." -PropertyValues $resources.Count
25+
$resourceCount = $resources.Count
26+
$progressStep = [math]::Ceiling($resourceCount / 10) # Calculate step for 10% progress
2427
$hugoMarkdownObjects = @()
25-
$TotalFiles = $resources.Count
26-
Write-InformationLog "Loading ({count}) markdown files...." -PropertyValues $TotalFiles
27-
$resources | ForEach-Object {
28-
if ((Test-Path $_)) {
28+
29+
$resources | ForEach-Object -Begin { $index = 0 } -Process {
30+
if (Test-Path $_) {
2931
$hugoMarkdown = Get-HugoMarkdown -Path $_
3032
$hugoMarkdownObjects += $hugoMarkdown
3133
}
34+
35+
$index++
36+
if ($index % $progressStep -eq 0 -or $index -eq $resourceCount) {
37+
Write-InformationLog "Progress: $([math]::Round(($index / $resourceCount) * 100))% complete"
38+
}
3239
}
40+
3341
$TotalItems = $hugoMarkdownObjects.Count
3442
Write-InformationLog "Loaded ({count}) HugoMarkdown Objects." -PropertyValues $TotalItems
3543
### FILTER hugoMarkdownObjects
@@ -213,13 +221,16 @@ while ($hugoMarkdownQueue.Count -gt 0 -or $hugoMarkdownBatchQueue.Count -gt 0) {
213221
$categoryClassification = Get-ClassificationsForType -updateMissing -ClassificationType "categories" -hugoMarkdown $hugoMarkdown
214222
$categoryClassificationOrdered = Get-ClassificationOrderedList -minScore 50 -byLevel -classifications $categoryClassification | Select-Object -First 3
215223
$categories = $categoryClassificationOrdered | ForEach-Object { $_.category }
216-
217224
Update-StringList -frontMatter $hugoMarkdown.FrontMatter -fieldName 'categories' -values @($categories) -Overwrite
225+
#$categoriesMeta = $categoryClassificationOrdered | ForEach-Object { [ordered]@{ category = $_.category; final_score = $_.final_score } }
226+
#Update-HashtableList -frontMatter $hugoMarkdown.FrontMatter -fieldName 'categoriesMeta' -addAfter "categories" -values $categoriesMeta -Overwrite
218227
#-----------------Tags-------------------
219228
$tagClassification = Get-ClassificationsForType -updateMissing -ClassificationType "tags" -hugoMarkdown $hugoMarkdown
220229
$tagClassificationOrdered = Get-ClassificationOrderedList -minScore 70 -classifications $tagClassification | Select-Object -First 10
221230
$tags = $tagClassificationOrdered | ForEach-Object { $_.category }
222231
Update-StringList -frontMatter $hugoMarkdown.FrontMatter -fieldName 'tags' -values @($tags) -Overwrite
232+
#$tagsMeta = $tagClassificationOrdered | ForEach-Object { [ordered]@{ category = $_.category; final_score = $_.final_score } }
233+
#Update-HashtableList -frontMatter $hugoMarkdown.FrontMatter -fieldName 'tagsMeta' -addAfter "tags" -values $tagsMeta -Overwrite
223234
# =================COMPLETE===================
224235
$eeResult = Get-Classification -CacheFolder $hugoMarkdown.FolderPath -ClassificationName "Engineering Excellence"
225236
$tlResult = Get-Classification -CacheFolder $hugoMarkdown.FolderPath -ClassificationName "Technical Leadership"

0 commit comments

Comments
 (0)