88. PARAMETER OutFile
99 The resulting html. If this parameter is not specified an HTML file with the same name of the markdown file will be created.
1010
11+ . PARAMETER PassThru
12+ If specified, the content of the HTML file will be returned as well as written to disk.
13+
14+ . EXAMPLE
15+ # obtain an HTML representation of the markdown file
16+ $html = ConvertTo-HtmlFromMarkdown -File "C:\path\to\file.md"
17+
18+ . EXAMPLE
19+ # write the HTML representation of the markdown file to disk
20+ ConvertTo-HtmlFromMarkdown -File "C:\path\to\file.md" -OutFile "C:\path\to\file.html"
21+
22+ . EXAMPLE
23+ # write the HTML representation of the markdown file to disk and return the content
24+ $html = ConvertTo-HtmlFromMarkdown -File "C:\path\to\file.md" -OutFile "C:\path\to\file.html" -PassThru
1125#>
1226function ConvertTo-HtmlFromMarkdown {
27+ [CmdletBinding (DefaultParameterSetName = " Default" )]
1328 param (
14- [Parameter (Mandatory = $true , HelpMessage = " Path to Markdown file" )]
29+ [Parameter (Mandatory = $true , ParameterSetName = " Default" )]
30+ [Parameter (Mandatory = $true , ParameterSetName = " Persist" )]
1531 [ValidateScript ({ Test-Path $_ - PathType Leaf })]
1632 [string ]$File ,
1733
18- [Parameter (HelpMessage = " File path to create" )]
19- # [ValidateScript({ Test-Path $_ -Include "*.html" -PathType Container})]
20- [string ]$OutFile
34+ [Parameter (ParameterSetName = " Persist" )]
35+ [string ]$OutFile ,
36+
37+ [Parameter (ParameterSetName = " Persist" )]
38+ [switch ]$PassThru
2139 )
2240
2341 # ensure that the file is an absolute path because pandoc.exe doesn't like powershell relative paths
24- $File = ( Resolve- Path $File ).Path
42+ $File = $ExecutionContext .SessionState. Path.GetUnresolvedProviderPathFromPSPath ( $File )
2543
2644 # Use pandoc to convert the markdown to Html
2745 $pandocArgs = " `" {0}`" " -f $File
@@ -41,8 +59,10 @@ function ConvertTo-HtmlFromMarkdown {
4159
4260 if (! ($OutFile )) {
4361 $OutFile = Join-Path (Split-Path $File - Parent) ((Split-Path $File - LeafBase) + " .html" )
62+ # ensure that the file is an absolute path because pandoc.exe doesn't like powershell relative paths
4463 Write-Verbose " Using OutFile: $OutFile "
4564 }
65+ $OutFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath ($OutFile )
4666
4767 $pandocArgs += " -o `" {0}`" " -f $OutFile
4868
@@ -60,7 +80,14 @@ function ConvertTo-HtmlFromMarkdown {
6080
6181 Set-Content - Path $OutFile - Value $content
6282
63- Remove-Item $OutFile
83+ if (! ($PSCmdlet.ParameterSetName -eq " Persist" )) {
84+ Write-Verbose " Removing temporary file: $OutFile "
85+ Remove-Item $OutFile
6486
65- return $content
87+ return $content
88+ }
89+
90+ if ($PassThru.IsPresent -and $PassThru ) {
91+ return $content
92+ }
6693}
0 commit comments