-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathsync-docs-index.ps1
More file actions
103 lines (83 loc) · 2.37 KB
/
Copy pathsync-docs-index.ps1
File metadata and controls
103 lines (83 loc) · 2.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -----------------------------
# CONFIG
# -----------------------------
$docsPath = Join-Path $PSScriptRoot "docs"
$outputFile = Join-Path $docsPath "index-contents.md"
# -----------------------------
# HELPERS
# -----------------------------
function Get-Heading {
param ([string]$filePath)
foreach ($line in Get-Content $filePath) {
if ($line -match '^\s*#\s+(.+)') {
return $matches[1].Trim()
}
}
return [System.IO.Path]::GetFileNameWithoutExtension($filePath)
}
function Should-Ignore {
param ([string]$path)
return $path -match "[\\/](release-notes)[\\/]"
}
function Normalize-RelativePath {
param ([string]$fullPath)
$relative = $fullPath.Substring($docsPath.Length)
return ($relative -replace '\\','/').TrimStart('/')
}
# -----------------------------
# COLLECT FILES
# -----------------------------
$files = Get-ChildItem $docsPath -Recurse -File -Filter "*.md" |
Where-Object {
$_.Name -ne "index-contents.md" -and
-not (Should-Ignore $_.FullName)
}
# -----------------------------
# BUILD TOPICS + VALIDATION
# -----------------------------
$topics = @()
$brokenLinks = @()
foreach ($file in $files) {
$title = Get-Heading $file.FullName
$relative = Normalize-RelativePath $file.FullName
$link = "docs/$relative"
# VALIDATION: ensure file still exists
if (-not (Test-Path $file.FullName)) {
$brokenLinks += $link
continue
}
$topics += [PSCustomObject]@{
Title = $title
Path = "./$relative"
}
}
# -----------------------------
# SORT (GLOBAL ALPHABETICAL)
# -----------------------------
$topics = $topics | Sort-Object Title
# -----------------------------
# RENDER INDEX
# -----------------------------
$output = @()
$output += "# Documentation Index"
$output += ""
$output += "## Topics"
$output += ""
foreach ($t in $topics) {
$output += "- [$($t.Title)]($($t.Path))"
}
# -----------------------------
# WRITE INDEX
# -----------------------------
$output | Set-Content -Path $outputFile -Encoding UTF8
# -----------------------------
# REPORT BROKEN LINKS
# -----------------------------
if ($brokenLinks.Count -gt 0) {
Write-Warning "Broken links detected:"
$brokenLinks | ForEach-Object { Write-Warning " - $_" }
}
else {
Write-Host "All links validated successfully."
}
Write-Host "Index generated at: $outputFile"