Skip to content

Commit 723f455

Browse files
committed
✨ Add script to correct markdown headers
- resolves brianary#72
1 parent 906cb87 commit 723f455

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

Repair-MarkdownHeaders.ps1

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

test/data/rehead.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# *Test* `code` [link](to.md)
2+
3+
A test of heading conversion.
4+
5+
## .
6+
7+
Dot!
8+
9+
```ps1
10+
# comment
11+
Get-PSDrive env
12+
```
13+
14+
Alpha
15+
-----
16+
17+
### Eh
18+
19+
1. A
20+
2. B
21+
3. C
22+
23+
### Bee ###
24+
25+
🐝
26+
27+
### Sea #########################################
28+
29+
🌊
30+
31+
## Test 🗝️ Key
32+
33+
```bash
34+
# output
35+
echo OK
36+
```

0 commit comments

Comments
 (0)