-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreate-Toc-Improved.ps1
More file actions
134 lines (115 loc) · 3.93 KB
/
Copy pathCreate-Toc-Improved.ps1
File metadata and controls
134 lines (115 loc) · 3.93 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<#
.SYNOPSIS
Creates table of contents (toc.yml) files for DocFX documentation.
.DESCRIPTION
This script recursively searches the docs directory for folders without toc.yml files
and generates appropriate toc.yml files based on the contents of each folder.
.PARAMETER DocsRootPath
The root path of the documentation files. Defaults to './docs'.
.EXAMPLE
.\Create-Toc-Improved.ps1
Creates toc.yml files in all subdirectories of the ./docs folder.
.EXAMPLE
.\Create-Toc-Improved.ps1 -DocsRootPath './documentation'
Creates toc.yml files in all subdirectories of the ./documentation folder.
.NOTES
Author: Joseph Streeter
Version: 1.0
#>
function New-Toc
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$FolderPath
)
begin
{
Write-Verbose -Message "Starting to create TOC for folder: $FolderPath"
}
process
{
try
{
$Contents = Get-ChildItem -Path $FolderPath -ErrorAction Stop |
Where-Object { $_.Name -ne "toc.yml" -and $_.Name -ne "media" }
$TocEntries = [System.Collections.Generic.List[string]]@()
foreach ($Content in $Contents)
{
$FormattedName = $Content.Name.Replace("-", " ").Replace(".md", "")
if ($Content.UnixMode -like "d*")
{
$EntryText = "- name: $FormattedName`n href: $($Content.Name)/toc.yml"
$TocEntries.Add($EntryText)
}
else
{
$EntryText = "- name: $FormattedName`n href: $($Content.Name)"
$TocEntries.Add($EntryText)
}
}
return $TocEntries
}
catch
{
Write-Error -Message "Error processing folder $FolderPath : $($_.Exception.Message)"
}
}
end
{
Write-Verbose -Message "Completed TOC creation for folder: $FolderPath"
}
}
function Update-DocFxTableOfContents
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$DocsRootPath = './docs'
)
begin
{
Write-Verbose -Message "Starting TOC generation for all directories in $DocsRootPath"
if (-not (Test-Path -Path $DocsRootPath))
{
Write-Error -Message "The specified docs path does not exist: $DocsRootPath"
return
}
}
process
{
try
{
$Directories = Get-ChildItem -Path $DocsRootPath -Recurse -Directory -ErrorAction Stop |
Where-Object { $_.Name -ne "media" }
$TocCreationCount = 0
foreach ($Directory in $Directories)
{
if ((Get-ChildItem -Path $Directory).Name -notcontains "toc.yml")
{
Write-Verbose -Message "Creating toc.yml for $($Directory.FullName)"
$TocContent = New-Toc -FolderPath $Directory.FullName
$TocPath = Join-Path -Path $Directory.FullName -ChildPath "toc.yml"
$TocContent | Out-File -FilePath $TocPath -Encoding utf8
Write-Verbose -Message "Created toc.yml at $TocPath"
$TocCreationCount++
}
}
Write-Output "TOC generation complete. Created $TocCreationCount new toc.yml files."
}
catch
{
Write-Error -Message "Error during TOC generation: $($_.Exception.Message)"
}
}
end
{
Write-Verbose -Message "Completed TOC generation process"
}
}
# Execute the main function to update the table of contents
Update-DocFxTableOfContents -Verbose