-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-toc.ps1
72 lines (58 loc) · 1.52 KB
/
merge-toc.ps1
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
param(
[String] $refDocPath = "azure-cli-docs",
[String] $conceptDocPath = "conceptual-docs"
)
Write-Host "Start merging TOC in folder: " + $refDocPath + " and " + $conceptDocPath
$conceptTocFile = [System.IO.Path]::Combine($conceptDocPath, "TOC.md")
if(-not (Test-Path $conceptTocFile))
{ return }
$conceptLines = Get-Content $conceptTocFile
$refTocFile = [System.IO.Path]::Combine($refDocPath, "refTOC.md")
$refLines = Get-Content $refTocFile
$finalTocLines = New-Object System.Collections.Generic.List[System.String]
$level = 0
foreach($line in $conceptLines)
{
if([System.String]::IsNullOrWhiteSpace($line))
{ break }
$curLevel = 0
$index = 0;
while($index -lt $line.Length -and $line[$index] -eq '#')
{
++$index
++$curLevel
}
if($curLevel -eq 0)
{
Write-Host "Unexpected toc content: " + $line
}
if($level -eq 0 -and $curLevel -ne 1)
{
Write-Host "First toc line must be start with only one #: " + $line
return
}
if($curLevel -gt $level + 1)
{
Write-Host "Invalid toc line: " + $line
return
}
$level = $curLevel
$line = $line.Replace("](", "](..\" + $conceptDocPath + "\");
$finalTocLines.Add($line)
}
$indent = ""
if($level -ne 0)
{
$indent = "#"
}
foreach($line in $refLines)
{
$line = $line.TrimEnd()
if(-not $line.EndsWith(".md)", [System.StringComparison]::OrdinalIgnoreCase))
{
$finalTocLines.Add($indent + $line)
}
}
$tocFile = [System.IO.Path]::Combine($refDocPath, "TOC.md")
Set-Content $tocFile $finalTocLines
Write-Host "Complete merging TOC"