Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConstantineK/972fc537e1196e9e5ca0aaa74649cbe2 to your computer and use it in GitHub Desktop.
Save ConstantineK/972fc537e1196e9e5ca0aaa74649cbe2 to your computer and use it in GitHub Desktop.
#
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
function Convert-StringToMarkdownTableOfContents {
param ([string]$MarkdownDocument)
$nl = [System.Environment]::NewLine
$TOC = "## Table Of Contents$nl$nl"
foreach ($line in $MarkdownDocument.Split( $nl )){
if ($line -match "^(#+)\s(.*)$") {
$Count = $Matches[1].Length
$Content = $($Matches[2])
$EncodedContent = ($Content -replace " ",'-').ToLower()
if ($Count -eq 1){
$TOC += "- [$Content](#$EncodedContent)$nl"
}
else {
$TOC += "$(" " * $Count)* [$Content](#$EncodedContent)$nl"
}
$MarkdownDocument = $MarkdownDocument -replace "(($line)\w*)",$(
'<a href = "#'+ $EncodedContent +'"></a>' +
$nl +
$line
)
}
}
return $TOC + $nl + $MarkdownDocument
}
Set-Alias -Value "Convert-StringToMarkdownTableOfContents" -Name "Convert-StringToMdToc"
$MdExamples = @(
@'
# Initial Idea
Let''s generate a table of contents for a Github flavored Markdown document in PowerShell (because nobody else seems to have.)
## Clairfying Our Requirements
After talking with Claudio, he didn''t just want a TOC for a particular markdown file (though that''s nice), but he also wanted a TOC for a set of files in a folder.
I decided to do the first one, and it looks like Claudio just finished the second.
I did some googling and found that Github flavored markdown supports a special form of anchor linking as it isn''t supported in vanilla MD.
I also found that beyond this there wasn't a good anchor link MD shortcut, so I took this tact (which cares more about readability than speed or length.)
You can use the function below, which was also run on this blog post to generate itself.
## The Code
``` PowerShell
function Convert-StringToMarkdownTableOfContents {
param ([string]$MarkdownDocument)
$nl = [System.Environment]::NewLine
$TOC = "## Table Of Contents$nl$nl"
foreach ($line in $MarkdownDocument.Split( $nl )){
if ($line -match "^(#+)\s(.*)$") {
$Count = $Matches[1].Length
$Content = $($Matches[2])
$EncodedContent = ($Content -replace " ",''-'').ToLower()
if ($Count -eq 1){
$TOC += "- [$Content](#$EncodedContent)$nl"
}
else {
$TOC += "$(" " * $Count)* [$Content](#$EncodedContent)$nl"
}
$MarkdownDocument = $MarkdownDocument -replace "(($line)\w*)",$(
''<a href = "#'+ $EncodedContent +''"></a>'' +
$nl +
$line
)
}
}
return $TOC + $nl + $MarkdownDocument
}
```
## Special Thanks
Special thanks to [Cláudio Silva](https://claudioessilva.eu/) (PowerShell MVP) for giving me this fun idea!
'@
)
foreach ($eg in $MdExamples){
Convert-StringToMarkdownTableOfContents -MarkdownDocument $eg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment