1+ <#
2+ . SYNOPSIS
3+ Markdown File Transpiler.
4+ . DESCRIPTION
5+ Transpiles Markdown with Inline PipeScript into Markdown.
6+
7+ Because Markdown does not support comment blocks, PipeScript can be written inline inside of specialized Markdown code blocks.
8+
9+ PipeScript can be included in a Markdown code block that has the Language ```PipeScript{```
10+
11+ In Markdown, PipeScript can also be specified as the language using any two of the following characters ```.<>```
12+ . Example
13+ .> {
14+ $markdownContent = @'
15+ # Thinking of a Number Between 1 and 100: ```.<{Get-Random -Min 1 -Max 100}>.``` is the number
16+
17+ ### abc
18+
19+ ~~~PipeScript{
20+ '* ' + @("a", "b", "c" -join ([Environment]::Newline + '* '))
21+ }
22+ ~~~
23+
24+ #### Guess what, other code blocks are unaffected
25+ ~~~PowerShell
26+ 1 + 1 -eq 2
27+ ~~~
28+
29+
30+ '@
31+ [OutputFile('.\HelloWorld.ps1.md')]$markdownContent
32+ }
33+
34+ .> .\HelloWorld.ps1.md
35+ #>
36+ [ValidateScript ({
37+ $cmdInfo = $_
38+ if ($cmdInfo.Source -match ' \.(?>md|markdown)$' ) {
39+ return $true
40+ }
41+ return $false
42+ })]
43+ param (
44+ # The command information. This will include the path to the file.
45+ [Parameter (Mandatory , ValueFromPipeline )]
46+ $CommandInfo
47+ )
48+
49+ begin {
50+ # We start off by declaring a number of regular expressions:
51+
52+ $startComment = ' (?>
53+ (?>```|~~~) |
54+ (?<=(?>[\r\n]+){1})[\s-[\r\n]]{0,3}(?>```|~~~)
55+ ){1}(?>[\.\<\>]{2}|PipeScript)\s{0,}\{\s{0,}'
56+ $endComment = ' \}(?>[\.\<\>]{2}|PipeScript){0,1}(?>
57+ (?>```|~~~)
58+ |
59+ \s{0,}(?>[\r\n]+){1}\s{0,3}(?>```|~~~)
60+ )
61+ '
62+
63+ $startRegex = " (?<PSStart>${startComment} )"
64+ # * EndRegex ```$whitespace + '}' + $EndComment```
65+ $endRegex = " (?<PSEnd>${endComment} )"
66+
67+ $sourcePattern = [Regex ]::New(" (?>$ (
68+ $startRegex , $endRegex -join ([Environment ]::NewLine + ' |' + [Environment ]::NewLine)
69+ ) )" , " IgnoreCase, IgnorePatternWhitespace" , " 00:00:05" )
70+ }
71+
72+ process {
73+
74+ $fileInfo = $commandInfo.Source -as [IO.FileInfo ]
75+ $fileText = [IO.File ]::ReadAllText($fileInfo.Fullname )
76+
77+ .> PipeScript.Inline - SourceFile $CommandInfo.Source - SourceText $fileText - SourcePattern $sourcePattern - ForeachObject {
78+ process {
79+ if ($_ -is [string ]) {
80+ $_
81+ } elseif ($_.GetType -and $_.GetType ().IsPrimitive) {
82+ $_
83+ } else {
84+ $markdownObject = [PSObject ]::new($_ )
85+ $markdownObject.pstypenames.clear ()
86+ $markdownObject.pstypenames.add (' Markdown' )
87+ $markdownObject | Out-String - Width 1 kb
88+ }
89+ }
90+ }
91+ }
0 commit comments