|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Updates markdown content to replace level 1 & 2 ATX headers to Setext headers. |
| 4 | +
|
| 5 | +.FUNCTIONALITY |
| 6 | +Markdown |
| 7 | +
|
| 8 | +.INPUTS |
| 9 | +System.String containing markdown code to update. |
| 10 | +
|
| 11 | +.OUTPUTS |
| 12 | +System.String containing updated markdown code. |
| 13 | +
|
| 14 | +.LINK |
| 15 | +https://webcoder.info/markdown-headers.html |
| 16 | +
|
| 17 | +.EXAMPLE |
| 18 | +Repair-MarkdownHeaders.ps1 -Path readme.md -Style SetextWithAtx |
| 19 | +
|
| 20 | +Updates the file with the specified header style. |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +$content = $markdown |Repair-MarkdownHeaders.ps1 -Style Atx |
| 24 | +
|
| 25 | +Returns markdown code that uses ATX headers. |
| 26 | +#> |
| 27 | + |
| 28 | +#Requires -Version 7 |
| 29 | +[CmdletBinding()][OutputType([string],ParameterSetName='InputObject')] Param( |
| 30 | +# Markdown file to update. |
| 31 | +[Parameter(ParameterSetName='Path',Position=0,Mandatory=$true)][string] $Path, |
| 32 | +# The text encoding to use when converting text to binary data. |
| 33 | +[Parameter(ParameterSetName='Path',Position=1)] |
| 34 | +[ValidateSet('ascii','utf16','utf16BE','utf32','utf32BE','utf7','utf8')] |
| 35 | +[string] $Encoding = 'utf8', |
| 36 | +# Markdown content to update. |
| 37 | +[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)][string] $InputObject, |
| 38 | +# The style of headers to use. |
| 39 | +[ValidateSet('Atx', 'AtxClosed', 'SetextWithAtx')] |
| 40 | +[string] $Style = 'SetextWithAtx', |
| 41 | +# The line endings to use. |
| 42 | +[ValidatePattern('\A\r?\n\z')][string] $NewLine = [Environment]::NewLine |
| 43 | +) |
| 44 | +Begin |
| 45 | +{ |
| 46 | + filter Repair-Atx |
| 47 | + { |
| 48 | + [CmdletBinding()] Param( |
| 49 | + [Parameter(ValueFromPipeline=$true)][string] $InputObject |
| 50 | + ) |
| 51 | + $md = ConvertFrom-Markdown -InputObject $InputObject |
| 52 | + $value = switch($md.Tokens) |
| 53 | + { |
| 54 | + {$_.Span.Length -eq 0} {} |
| 55 | + {$_ -is [Markdig.Syntax.HeadingBlock] -and $_.IsSetext} |
| 56 | + { |
| 57 | + $headertext = $InputObject.Substring($_.Span.Start, $_.Span.Length).TrimEnd(($_.Level -eq 1 ? '=' : '-')).Trim() |
| 58 | + "$(New-Object string '#',$_.Level) $headertext" |
| 59 | + } |
| 60 | + default {$InputObject.Substring($_.Span.Start, $_.Span.Length).Trim()} |
| 61 | + } |
| 62 | + return $value -join "$NewLine$NewLine" |
| 63 | + } |
| 64 | + |
| 65 | + filter Repair-AtxClosed |
| 66 | + { |
| 67 | + [CmdletBinding()] Param( |
| 68 | + [Parameter(ValueFromPipeline=$true)][string] $InputObject |
| 69 | + ) |
| 70 | + $md = ConvertFrom-Markdown -InputObject $InputObject |
| 71 | + $value = switch($md.Tokens) |
| 72 | + { |
| 73 | + {$_.Span.Length -eq 0} {} |
| 74 | + {$_ -is [Markdig.Syntax.HeadingBlock] -and $_.IsSetext} |
| 75 | + { |
| 76 | + $headertext = $InputObject.Substring($_.Span.Start, $_.Span.Length).TrimEnd(($_.Level -eq 1 ? '=' : '-')).Trim() |
| 77 | + $atx = (New-Object string '#',$_.Level) |
| 78 | + "$atx $headertext $atx" |
| 79 | + } |
| 80 | + {$_ -is [Markdig.Syntax.HeadingBlock] -and !$_.IsSetext} |
| 81 | + { |
| 82 | + $headertext = $InputObject.Substring($_.Span.Start, $_.Span.Length).TrimStart($_.HeaderChar).Trim() |
| 83 | + $atx = (New-Object string '#',$_.Level) |
| 84 | + "$atx $headertext $atx" |
| 85 | + } |
| 86 | + default {$InputObject.Substring($_.Span.Start, $_.Span.Length).Trim()} |
| 87 | + } |
| 88 | + return $value -join "$NewLine$NewLine" |
| 89 | + } |
| 90 | + |
| 91 | + filter Repair-SetextWithAtx |
| 92 | + { |
| 93 | + [CmdletBinding()] Param( |
| 94 | + [Parameter(ValueFromPipeline=$true)][string] $InputObject |
| 95 | + ) |
| 96 | + $md = ConvertFrom-Markdown -InputObject $InputObject |
| 97 | + $value = switch($md.Tokens) |
| 98 | + { |
| 99 | + {$_.Span.Length -eq 0} {} |
| 100 | + {$_ -is [Markdig.Syntax.HeadingBlock] -and $_.Level -lt 3 -and !$_.IsSetext} |
| 101 | + { |
| 102 | + $headertext = $InputObject.Substring($_.Span.Start, $_.Span.Length).TrimStart($_.HeaderChar).Trim() |
| 103 | + "$headertext$NewLine$(New-Object string ($_.Level -eq 1 ? '=' : '-'),$headertext.Length)" |
| 104 | + } |
| 105 | + default {$InputObject.Substring($_.Span.Start, $_.Span.Length).Trim()} |
| 106 | + } |
| 107 | + return $value -join "$NewLine$NewLine" |
| 108 | + } |
| 109 | +} |
| 110 | +Process |
| 111 | +{ |
| 112 | + switch($PSCmdlet.ParameterSetName) |
| 113 | + { |
| 114 | + InputObject {return $InputObject |& "Repair-$Style"} |
| 115 | + Path |
| 116 | + { |
| 117 | + $content = Get-Content $Path -Raw |
| 118 | + $content |& "Repair-$Style" |Out-File $Path $Encoding |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments