-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdoc-urls-list.ps1
More file actions
63 lines (50 loc) · 1.97 KB
/
doc-urls-list.ps1
File metadata and controls
63 lines (50 loc) · 1.97 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
param(
[Parameter(Mandatory=$true)]
[string]$RootPath, # e.g. E:\Intent.Docs\articles
[Parameter(Mandatory=$true)]
[string]$BaseUrl, # e.g. https://docs.intentarchitect.com/articles
[string]$OutputFile
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
if (!(Test-Path -LiteralPath $RootPath -PathType Container)) {
throw "RootPath not found or not a folder: $RootPath"
}
$rootFull = (Resolve-Path -LiteralPath $RootPath).Path.TrimEnd('\','/')
if ($BaseUrl -notmatch '/$') { $BaseUrl += '/' }
function Get-RelativeToRoot {
param([string]$Root, [string]$Full)
if (-not $Full.StartsWith($Root, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "File is not under root. Root: '$Root' File: '$Full'"
}
$Full.Substring($Root.Length).TrimStart('\','/')
}
function To-UrlPath {
param([string]$RelFsPath)
if ($RelFsPath -notlike '*.md') { return $null }
# Normalize separators to URL style and change extension to .html
$p = $RelFsPath -replace '\\','/'
$p = [Regex]::Replace($p, '\.md$', '.html', 'IgnoreCase')
# encode each segment
$segments = $p -split '/'
$encoded = foreach ($s in $segments) {
if ([string]::IsNullOrWhiteSpace($s)) { continue }
[Uri]::EscapeDataString($s)
}
($encoded -join '/')
}
$files = Get-ChildItem -LiteralPath $rootFull -Recurse -File -Filter *.md
$urls = foreach ($f in $files) {
$relFs = Get-RelativeToRoot -Root $rootFull -Full $f.FullName
$relUrl = To-UrlPath -RelFsPath $relFs
if ($relUrl) { $BaseUrl.TrimEnd('/') + '/' + $relUrl.TrimStart('/') }
}
$urls = $urls | Sort-Object -Unique
if ($OutputFile) {
$dir = Split-Path -Path $OutputFile -Parent
if ($dir -and !(Test-Path $dir)) { New-Item -ItemType Directory -Path $dir | Out-Null }
Set-Content -LiteralPath $OutputFile -Value ($urls -join [Environment]::NewLine) -Encoding UTF8
Write-Host "Wrote $($urls.Count) URLs to $OutputFile"
} else {
$urls
}