forked from itenium-be/blog-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepen-headers.ps1
More file actions
71 lines (58 loc) · 1.75 KB
/
deepen-headers.ps1
File metadata and controls
71 lines (58 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Script to deepen all markdown headers by one level
# Transforms # -> ##, ## -> ###, etc.
# Preserves code blocks (content between ``` delimiters)
param(
[Parameter(Mandatory=$false)]
[string]$Path = ".",
[Parameter(Mandatory=$false)]
[switch]$WhatIf
)
function Update-MarkdownHeaders {
param(
[string]$FilePath,
[switch]$WhatIf
)
$content = Get-Content $FilePath -Raw
$lines = Get-Content $FilePath
$newLines = @()
$inCodeBlock = $false
foreach ($line in $lines) {
# Check if we're entering/exiting a code block
if ($line -match '^```') {
$inCodeBlock = -not $inCodeBlock
$newLines += $line
continue
}
# If we're in a code block, don't modify the line
if ($inCodeBlock) {
$newLines += $line
continue
}
# If it's a header line (starts with #), add one more #
if ($line -match '^(#+)\s') {
$newLine = $line -replace '^(#+)\s', '$1# '
$newLines += $newLine
} else {
$newLines += $line
}
}
if ($WhatIf) {
Write-Host "Would update: $FilePath" -ForegroundColor Yellow
} else {
$newLines | Set-Content $FilePath -Encoding UTF8
Write-Host "Updated: $FilePath" -ForegroundColor Green
}
}
# Find all markdown files
$mdFiles = Get-ChildItem -Path $Path -Filter "*.md" -Recurse
Write-Host "Found $($mdFiles.Count) markdown files" -ForegroundColor Cyan
Write-Host ""
if ($WhatIf) {
Write-Host "=== DRY RUN MODE ===" -ForegroundColor Yellow
Write-Host ""
}
foreach ($file in $mdFiles) {
Update-MarkdownHeaders -FilePath $file.FullName -WhatIf:$WhatIf
}
Write-Host ""
Write-Host "Done!" -ForegroundColor Cyan