-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-toc.ps1
More file actions
56 lines (47 loc) · 2.01 KB
/
create-toc.ps1
File metadata and controls
56 lines (47 loc) · 2.01 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
function Get-MarkdownToc {
[CmdletBinding()]
param (
[Parameter()]
[string]
$Indent = " ",
[Parameter()]
[switch]
$IncludeTitles
)
$Path = Read-Host -Prompt "Please enter the path to the Markdown file"
#Check if the path is present
if(Test-Path -Path $Path) {
$Content = Get-Content -Path $Path
#Check if the file is valid
if($Content.Length -gt 1) {
#Print message that we are now generating the TOC
Write-Host -ForegroundColor Green "Here is your TOC: ( Do not wonder, the items will enumerate themselfes in Markdown ;) )"
foreach ($Line in $Content) {
$TocLine = ""
#When line starts with "#" we know that this is a heading line
if($Line -like "#*") {
$TmpLine = $Line.Split('#')
#Calculate indent level based on "#"
$IndentLevel = $TmpLine.length - 1
#Our first Heading of the page will always be # - Check if we should include it or not
if($IncludeTitles) {
$IndentLevel += 1
} elseif ($IndentLevel -lt 2) {continue}
if($IndentLevel -gt 2) {
for($i = 0; $i -lt $IndentLevel; $i++) {
$TocLine += $Indent
}
}
$TmpLine = ($TmpLine[-1].Trim()) -replace "([\s]+$)|([#\*])|(\<\w+\>)"
$TocLine += ("1. [" + $TmpLine + "](" + ($TmpLine -replace "[#?\{\[\(\)\]\}\'\/]").Replace(" ","-").ToLower() + ")")
Write-Host $TocLine
}
}
} else {
Write-Warning ("Inavlid file size. File has to be larger than 1. File Size: " + $Content.Length)
}
} else {
Write-Warning ("Invalid Path: " + $Path)
}
}
Get-MarkdownToc