-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_paths.ps1
More file actions
51 lines (44 loc) · 2.02 KB
/
Copy pathfix_paths.ps1
File metadata and controls
51 lines (44 loc) · 2.02 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
param([string]$targetDir)
if (-not (Test-Path $targetDir)) {
Write-Error "Target directory does not exist: $targetDir"
exit 1
}
Write-Host "Scanning for Markdown files in $targetDir..."
Get-ChildItem -Path $targetDir -Recurse -Filter *.md | ForEach-Object {
$path = $_.FullName
$content = Get-Content $path -Raw -Encoding UTF8
$original = $content
# Fix Markdown links: ](folder.assets/...)
$content = [Regex]::Replace($content, '\]\(([^)]+)\)', { param($m)
$url = $m.Groups[1].Value
# Only process if it looks like an assets path (contains .assets/)
if ($url -notmatch '\.assets/') { return $m.Value }
# If starts with http or /, ignore
if ($url -match '^(http|/)') { return $m.Value }
# If starts with ./, return as is (already correct)
if ($url -match '^\./') { return $m.Value }
# Otherwise, prepend ./
return '](./' + $url + ')'
})
# Fix HTML src: src="folder.assets/..."
$content = [Regex]::Replace($content, 'src=([''"])([^''"]+)([''"])', { param($m)
$quote = $m.Groups[1].Value
$url = $m.Groups[2].Value
# Only process if it looks like an assets path (contains .assets/)
if ($url -notmatch '\.assets/') { return $m.Value }
# If starts with http or /, ignore
if ($url -match '^(http|/)') { return $m.Value }
# If starts with ./, return as is (already correct)
if ($url -match '^\./') { return $m.Value }
# Otherwise, prepend ./
return 'src=' + $quote + './' + $url + $quote
})
# Clean up any accidental double prefixes like ././ or ./././
# We loop until no more double dots are found to be safe
while ($content -match '\]\(\./\./') { $content = $content -replace '\]\(\./\./', '](./' }
while ($content -match 'src=["'']\./\./') { $content = $content -replace 'src=([''"])\./\./', 'src=$1./' }
if ($content -ne $original) {
Write-Host "Fixed paths in: $path"
Set-Content -Path $path -Value $content -Encoding UTF8
}
}