-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocfx-toc-generator.psm1
115 lines (90 loc) · 4.1 KB
/
docfx-toc-generator.psm1
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
function Get-FolderGrabCondition($folderName) {
$ignoreItemName = ".nodoc"
$IgnoreFolderDeafultList = ".\.git", ".\_site", ".\obj", ".\src"
$notInListCondition = (-not $IgnoreFolderDeafultList.Contains($folderName))
$doesntHaveNoDocFileCondition = (-not [System.IO.File]::Exists([System.IO.Path]::Combine($folderName, $ignoreItemName)))
return $notInListCondition -and $doesntHaveNoDocFileCondition
}
function Get-SubDocFolders($Path) {
$dirs = [System.IO.Directory]::GetDirectories($Path, "*", [System.IO.SearchOption]::AllDirectories)
return $dirs | where { Get-FolderGrabCondition $_ }
}
function Get-RootDocFolder($Path){
$dirs = [System.IO.Directory]::GetDirectories($Path, "*", [System.IO.SearchOption]::TopDirectoryOnly)
return $dirs | where { Get-FolderGrabCondition $_ }
}
function Get-YamlFrontMatter([string]$mdContent, [string]$mdpath) {
$lines = $mdContent -Split [System.Environment]::NewLine
if(($lines -and ($lines.Length -gt 0) -and ($lines[0] -eq "---"))) {
$secondIndex = $lines[1..$lines.Length].IndexOf("---")
Write-Host "Indexing '$mdpath'..." -ForegroundColor DarkGray
return [string]::Join([System.Environment]::NewLine, $lines[1..$secondIndex])
}
else{
Write-Host "Front-matter (Yaml meta-data) for '$mdpath' : NOT FOUND" -ForegroundColor DarkGray
return ""
}
}
function New-TocYaml($folder, $tocFolder){
$mdFiles = [System.IO.Directory]::GetFiles($folder, "*.md", [System.IO.SearchOption]::TopDirectoryOnly)
$topLevelTocItems = New-Object Collections.Generic.List[Object]
$mdFiles | % {
if([System.IO.Path]::GetFileName($_) -ne "index.md"){
$tocItem = Get-MarkdownSingleTocItem $_ $tocfolder
$topLevelTocItems.Add($tocItem)
}
}
$subdocTopFolders = Get-RootDocFolder $folder
$indexFiles = $subdocTopFolders | % { Join-Path $_ "index.md" } | where { [System.IO.File]::Exists($_) }
$indexTocItems = New-Object Collections.Generic.List[Object]
$indexFiles | % {
$tocItem = Get-MarkdownSingleTocItem $_ $tocFolder
$dirOfIndex = [System.IO.Path]::GetDirectoryName($_)
$toc = New-TocYaml $dirOfIndex $tocFolder
$items = $toc | Sort-Object -Property order -Descending
$items = @($items)
if($null -ne $items -and $null -ne $tocItem -and $items.Length -gt 0){
$tocItem.Add("items", @($items))
}
$indexTocItems.Add($tocItem)
}
$result = ($topLevelTocItems + $indexTocItems) | Sort-Object -Property order -Descending
$result = @($result)
return ([Collections.Generic.List[Object]]$result)
}
function Get-MarkdownSingleTocItem([string]$markdownPath, $tocFolder){
$content = [System.IO.File]::ReadAllText($markdownPath)
$frontMatter = Get-YamlFrontMatter $content $markdownPath
if([string]::IsNullOrEmpty($frontMatter)){
return $null
}
$yaml = $frontMatter | ConvertFrom-Yaml
$noName = $null -eq $yaml.name
if($noName){
Write-Host "Front-matter of file '$markdownPath' needs a 'name' tag." -ForegroundColor Red
return $null
}
$relPath = [System.IO.Path]::GetRelativePath($tocFolder, $markdownPath)
$order = $yaml.order
if($null -eq $order) { $order = 0 }
$model = @{ "name" = $yaml.name; "order" = $order };
if($yaml.nocontent -ne $true) { $model.Add("href", $relPath) }
return $model
}
function Build-TocHereRecursive {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[string]
$Path=$PWD
)
foreach ($docFolder in Get-RootDocFolder $Path) {
Write-Host "==== Generating TOC for [$docFolder] ====================" -ForegroundColor DarkGray
$raw = New-TocYaml $docFolder $docFolder
$tocFile = ConvertTo-Yaml @($raw)
$tocFile > (Join-Path $docFolder "toc.yml")
Write-Host "==== End Generating for TOC [$docFolder] ================" -ForegroundColor DarkGray
Write-Host
}
}
Export-ModuleMember -Function Build-TocHereRecursive