Skip to content

Commit 0e92b4b

Browse files
committed
Add -PassThru to ConvertTo-MarkdownFromHtml
1 parent c2e9657 commit 0e92b4b

3 files changed

Lines changed: 82 additions & 37 deletions

File tree

src/public/ConvertTo-MarkdownFromHtml.ps1

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@
1111
.PARAMETER OutFile
1212
The resulting markdown file, if specified.
1313
14+
.PARAMETER PassThru
15+
Return markdown content as output, in addition to writing it to disk.
16+
17+
.EXAMPLE
18+
# Convert HTML content to Markdown and save to a file
19+
ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>" -OutFile "C:\path\to\file.md"
20+
21+
.EXAMPLE
22+
# Convert a HTML file to Markdown and save to a file
23+
ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html" -OutFile "C:\path\to\file.md"
24+
25+
.EXAMPLE
26+
# Convert a HTML file to Markdown and return the content
27+
$content = ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html"
28+
29+
.EXAMPLE
30+
# Convert HTML content to Markdown and return the content
31+
$content = ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>"
32+
33+
.EXAMPLE
34+
# Convert HTML content to Markdown and save to a file, returning the content
35+
$content = ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>" -OutFile "C:\path\to\file.md" -PassThru
36+
37+
.EXAMPLE
38+
# Convert a HTML file to Markdown and save to a file, returning the content
39+
$content = ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html" -OutFile "C:\path\to\file.md" -PassThru
1440
#>
1541
function ConvertTo-MarkdownFromHtml {
1642
param(
@@ -22,8 +48,12 @@ function ConvertTo-MarkdownFromHtml {
2248
[string]$Content,
2349

2450
[Parameter(ParameterSetName = "FromFile")]
25-
[Parameter(Mandatory=$false, ParameterSetName = "FromContent")]
26-
[string]$OutFile
51+
[Parameter(ParameterSetName = "FromContent")]
52+
[string]$OutFile,
53+
54+
[Parameter(ParameterSetName = "FromFile")]
55+
[Parameter(ParameterSetName = "FromContent")]
56+
[switch]$PassThru
2757
)
2858

2959
# when FromContent is specified, write the content to a temporary file
@@ -69,5 +99,9 @@ function ConvertTo-MarkdownFromHtml {
6999
Remove-Item $OutFile
70100
}
71101

72-
return $content
102+
# return output if not persisting to disk or PassThru is specified
103+
if (!$PSBoundParameters.ContainsKey("OutFile") -or ($PassThru.IsPresent -and $PassThru)) {
104+
Write-Verbose "Returning content"
105+
return $content
106+
}
73107
}

src/public/Get-BloggerPost.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Function Get-BloggerPost {
143143
$file = "$title.md"
144144

145145
$filePath = Join-Path -Path $OutDirectory -ChildPath $file
146-
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath > $null
146+
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath
147147
Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter
148148
Write-Verbose "Post content saved to: $filePath"
149149
}
Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,90 @@
11
Describe "ConvertTo-MarkdownFromHtml" {
22
BeforeAll {
33
Import-Module $PSScriptRoot\_TestHelpers.ps1 -Force
4-
4+
$script:inFile = "TestDrive:\123.html"
5+
$script:outFile = "TestDrive:\123.md"
6+
$script:htmlContent = "<h1>Hello World</h1>"
7+
Set-Content -Path $script:inFile -Value $script:htmlContent
58
}
69

710
BeforeEach {
811
Import-Module $PSScriptRoot\..\PSBlogger.psm1 -Force
912
}
1013

11-
Context "Using Content" {
12-
13-
BeforeEach {
14-
$outFile = "TestDrive:\123.md"
15-
$htmlContent = "<h1>Hello World</h1>"
14+
AfterEach {
15+
if (Test-Path $outFile) {
16+
Remove-Item $outFile -Force
1617
}
18+
}
1719

18-
AfterEach {
19-
if (Test-Path $outFile) {
20-
Remove-Item $outFile -Force
21-
}
22-
}
20+
Context "Using Content" {
2321

24-
It "Should save HTML content to a markdown file" {
22+
It "Should convert HTML content to Markdown file" {
2523
# act
26-
ConvertTo-MarkdownFromHtml -Content $htmlContent -OutFile $outFile
24+
ConvertTo-MarkdownFromHtml -Content $script:htmlContent -OutFile $script:outFile
2725

2826
# assert
29-
Test-Path $outFile | Should -BeTrue
27+
Test-Path $script:outFile | Should -BeTrue
28+
$content = (Get-Content -Path $script:outFile -Raw).Split("`r")
29+
$content[0] | Should -Be "# Hello World"
3030
}
3131

32-
It "Should convert HTML content to Markdown file" {
32+
It "Should not persist to disk if OutFile is not specified" {
3333
# act
34-
$content = ConvertTo-MarkdownFromHtml -Content $htmlContent -OutFile $outFile
34+
$content = ConvertTo-MarkdownFromHtml -Content $script:htmlContent
3535

3636
# assert
37-
$content = (Get-Content -Path $outFile -Raw).Split("`r")
38-
$content[0] | Should -Be "# Hello World"
37+
$content | Should -Not -BeNullOrEmpty
38+
Test-Path $script:outFile | Should -BeFalse
3939
}
4040

41-
It "Should not persist to disk if OutFile is not specified" {
41+
It "Should return content when writing to disk if PassThru is specified" {
4242
# act
43-
$content = ConvertTo-MarkdownFromHtml -Content $htmlContent
43+
$content = ConvertTo-MarkdownFromHtml -Content $script:htmlContent -OutFile $script:outFile -PassThru
4444

4545
# assert
4646
$content | Should -Not -BeNullOrEmpty
47-
Test-Path $outFile | Should -BeFalse
47+
Test-Path $script:outFile | Should -BeTrue
4848
}
4949
}
5050

5151
Context "Using File" {
5252

53-
BeforeEach {
54-
$htmlContent = "<h1>Hello World</h1>"
55-
$htmlFile = "TestDrive:\123.html"
56-
$markdownFile = "TestDrive:\123.md"
57-
Set-Content -Path $htmlFile -Value $htmlContent
53+
It "Should convert HTML content to Markdown and persist to OutFile" {
54+
# act
55+
ConvertTo-MarkdownFromHtml -File $script:inFile -OutFile $script:outFile
56+
57+
# assert
58+
Test-Path $script:outFile | Should -BeTrue
59+
$content = (Get-Content -Path $script:outFile -Raw).Split("`r")
60+
$content[0] | Should -Be "# Hello World"
5861
}
5962

6063
It "Should convert HTML file to Markdown" {
6164
# act
62-
$content = ConvertTo-MarkdownFromHtml -File $htmlFile -OutFile $markdownFile
65+
$content = ConvertTo-MarkdownFromHtml -File $script:inFile
6366

6467
# assert
65-
Test-Path $markdownFile | Should -BeTrue
66-
$content = (Get-Content -Path $markdownFile -Raw).Split("`r")
67-
$content[0] | Should -Be "# Hello World"
68+
$content | Should -Not -BeNullOrEmpty
69+
$content | Should -BeLike "# Hello World*"
70+
}
71+
72+
It "Should delete temporary OutFile if OutFile is not specified" {
73+
# act
74+
$content = ConvertTo-MarkdownFromHtml -File $script:inFile
75+
76+
# assert
77+
$content | Should -Not -BeNullOrEmpty
78+
Test-Path $script:outFile | Should -BeFalse
6879
}
6980

70-
It "Should delete temporary file" {
81+
It "Should return content when writing to disk if PassThru is specified" {
7182
# act
72-
$content = ConvertTo-MarkdownFromHtml -File $htmlFile
83+
$content = ConvertTo-MarkdownFromHtml -File $script:inFile -OutFile $script:outFile -PassThru
7384

7485
# assert
7586
$content | Should -Not -BeNullOrEmpty
76-
Test-Path $markdownFile | Should -BeFalse
87+
Test-Path $script:outFile | Should -BeTrue
7788
}
7889
}
7990
}

0 commit comments

Comments
 (0)