Skip to content

Commit c2e9657

Browse files
committed
Added -PassThru to ConvertTo-HtmlFromMarkdown and tests
1 parent 78bb0c6 commit c2e9657

3 files changed

Lines changed: 99 additions & 8 deletions

File tree

src/public/ConvertTo-HtmlFromMarkdown.ps1

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,38 @@
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
#>
1226
function 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
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Describe "ConvertTo-HtmlFromMarkdown" {
2+
BeforeAll {
3+
Import-Module $PSScriptRoot\_TestHelpers.ps1 -Force
4+
5+
$script:InFile = "TestDrive:\123.md"
6+
$script:OutFile = "TestDrive:\123.html"
7+
8+
Set-MarkdownFile -Path $script:InFile -Content "# Hello World"
9+
}
10+
11+
BeforeEach {
12+
Import-Module $PSScriptRoot\..\PSBlogger.psm1 -Force
13+
}
14+
15+
AfterEach {
16+
if (Test-Path $script:OutFile) {
17+
Remove-Item $script:OutFile -Force
18+
}
19+
}
20+
21+
It "Should convert Markdown file to HTML" {
22+
# act
23+
$result = ConvertTo-HtmlFromMarkdown -File $script:InFile
24+
25+
# assert
26+
$result | Should -Not -BeNullOrEmpty
27+
$result | Should -BeLike "<h1*>Hello World</h1>*"
28+
}
29+
30+
It "Should not produce an HTML file if OutFile is not specified" {
31+
# act
32+
$result = ConvertTo-HtmlFromMarkdown -File $script:InFile
33+
34+
# assert
35+
$result | Should -Not -BeNullOrEmpty
36+
Test-Path $script:OutFile | Should -BeFalse
37+
}
38+
39+
It "Should create an HTML file when OutFile is specified" {
40+
# act
41+
ConvertTo-HtmlFromMarkdown -File $script:InFile -OutFile $script:OutFile
42+
43+
# assert
44+
Test-Path $script:OutFile | Should -BeTrue
45+
}
46+
47+
It "Should not return content if OutFile is specified without PassThru" {
48+
# act
49+
$result = ConvertTo-HtmlFromMarkdown -File $script:InFile -OutFile $script:OutFile
50+
51+
# assert
52+
$result | Should -BeNullOrEmpty
53+
Test-Path $script:OutFile | Should -BeTrue
54+
}
55+
56+
It "Should return content if PassThru is specified with OutFile" {
57+
# act
58+
$result = ConvertTo-HtmlFromMarkdown -File $script:InFile -OutFile $script:OutFile -PassThru
59+
60+
# assert
61+
$result | Should -Not -BeNullOrEmpty
62+
Test-Path $script:OutFile | Should -BeTrue
63+
}
64+
}

src/tests/_TestHelpers.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function New-BlogPost($id) {
1111
.SYNOPSIS
1212
Blogger Blog post
1313
#>
14-
@{ id=$id }
14+
[pscustomobject]@{ id=$id }
1515
}
1616

1717

0 commit comments

Comments
 (0)