-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHeading.regex.ps1
81 lines (74 loc) · 2.25 KB
/
Heading.regex.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
73
74
75
76
77
78
79
80
81
<#
.Synopsis
Matches Markdown Heading
.Description
Matches Markdown Headings. Can provide a -HeadingName, -HeadingLevel, and -IncludeContent.
#>
param(
# The name of the heading. By default, will match any heading
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$HeadingName = '[^#]+?',
# The level(s) of the heading. If not provided, will match a heading of any level.
[ValidateRange(0,6)]
[int[]]
$HeadingLevel = 0,
# If set, will include the content of the heading. This will match until the next heading start of any level.
[switch]
$IncludeContent
)
if (-not $HeadingLevel) {
$HeadingLevel = 6..1
}
$HeadingLevel = $HeadingLevel | Sort-Object -Descending
"
(?m) # Set multline mode
^ # Anchor at line start
\s{0,3} # Match Up to three whitespace characters, then one of:
(?>
" + (@(
foreach ($hl in $HeadingLevel) {
if ($hl -eq 1) {
@'
(?<HeadingName>\S.+?)$ # Match a line followed by
\s+ # Some whitespace (in this case, the newline)
^\s{0,3}(?<h1>=+)\s{0,}$ # A line of all equals
'@
} elseif ($hl -eq 2) {
@'
# Match SETEXT heading 1:
(?<HeadingName>\S.+?)$
# A line of with at least one character of non-whitespace followed by
\s+ # Some whitespace (in this case, the newline)
^\s{0,3}(?<h2>\-+)\s{0,}$ # A line of all dashes (with up to 3 leading spaces)
'@
}
@"
# Match ATX heading $hl
(?<h$hl>\#{$hl})
\s # A single whitespace followed by the heading name
(?<HeadingName>
$($HeadingName -replace '\s', '\s')
)
(?:[\#\s]{0,})$ # Ignore any remaining #s or whitespace"
"@
}
) -join ([Environment]::NewLine + (' ' * 4) + '|' + [Environment]::NewLine)) + '
)
' + $(
if ($IncludeContent) {
@"
(?<Content> # If including Content
(?:.|\s){0,}? # Match anything until
(?=
^\s{0,3}
(?> # The start of another block
[\#\-=~``]
)
| # or
\z # the end of the string.
)
)
"@
}
)