Skip to content

Commit 94ff67c

Browse files
authored
Add -PassThru support for methods to persist to disk (#32)
* Add -PassThru to Get-BloggerPost #26 * Added -PassThru to ConvertTo-HtmlFromMarkdown and tests * Add -PassThru to ConvertTo-MarkdownFromHtml
1 parent 86e889a commit 94ff67c

8 files changed

Lines changed: 261 additions & 62 deletions

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg
5555
1. Fetch an individual post from your blog
5656

5757
```
58-
Get-BloggerPost -PostId <postid>
58+
$post = Get-BloggerPost -PostId <postid>
5959
```
60-
61-
You can also persist the post to disk as HTML or markdown in the current directory.
62-
60+
61+
You can also persist the post to disk as HTML or markdown in the current directory.
62+
6363
When using `HTML` format, files are saved as `<postid>.html`
6464

6565
When using `Markdown` format, files are saved as `<title>.md`
@@ -84,6 +84,12 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg
8484
Get-BloggerPost -PostId <postId> -OutDirectory ".\Blog" -FolderDateFormat "YYYY\\MM" -Format Markdown
8585
```
8686

87+
When persisting to disk, the post object is not returned unless `-PassThru` is specified.
88+
89+
```
90+
$post = Get-BloggerPost -PostId <postid> -Format Markdown -PassThru
91+
```
92+
8793
1. Publish a markdown file to your blog as draft
8894

8995
```

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
}

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: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@
1818
.PARAMETER OutDirectory
1919
The directory where the HTML file will be saved. If not specified, uses the current directory.
2020
21+
.PARAMETER PassThru
22+
If specified, the function will return the post object instead of just saving it to a file
23+
2124
.EXAMPLE
22-
Get-BloggerPost -PostId "1234567890123456789"
25+
# obtain a post from the blog defined in the user preferences
26+
$post = Get-BloggerPost -PostId "1234567890123456789"
2327
2428
.EXAMPLE
29+
# obtain a post from a specified blog and save it as HTML in a specific directory
2530
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format HTML -OutDirectory "C:\temp"
2631
2732
.EXAMPLE
33+
# obtain a post from a specified blog and save it as Markdown in a specific directory with a date-based folder structure
2834
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -DateFormat "YYYY\\MM" -OutDirectory "C:\blogposts"
35+
36+
.EXAMPLE
37+
# obtain a post from a specified blog and save it as JSON in the current directory
38+
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format JSON
39+
40+
.EXAMPLE
41+
# obtain a post from a specified blog, write it to disk and return the post object
42+
$post = Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -PassThru
2943
#>
3044
Function Get-BloggerPost {
3145
[CmdletBinding()]
@@ -46,7 +60,10 @@ Function Get-BloggerPost {
4660
[string]$FolderDateFormat,
4761

4862
[Parameter(ParameterSetName = "Persist")]
49-
[string]$OutDirectory = (Get-Location).Path
63+
[string]$OutDirectory = (Get-Location).Path,
64+
65+
[Parameter(ParameterSetName = "Persist")]
66+
[switch]$PassThru
5067
)
5168

5269
if (!$PSBoundParameters.ContainsKey("BlogId")) {
@@ -56,10 +73,6 @@ Function Get-BloggerPost {
5673
}
5774
}
5875

59-
if ([string]::IsNullOrEmpty($PostId)) {
60-
throw "PostId is required."
61-
}
62-
6376
try {
6477
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts/$PostId"
6578

@@ -130,7 +143,7 @@ Function Get-BloggerPost {
130143
$file = "$title.md"
131144

132145
$filePath = Join-Path -Path $OutDirectory -ChildPath $file
133-
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath > $null
146+
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath
134147
Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter
135148
Write-Verbose "Post content saved to: $filePath"
136149
}
@@ -144,7 +157,10 @@ Function Get-BloggerPost {
144157
}
145158

146159
# Return the post object for further processing if needed
147-
return $result
160+
if (!($PSCmdlet.ParameterSetName -eq "Persist") -or ($PassThru.IsPresent -and $PassThru)) {
161+
Write-Verbose "Returning blog post object"
162+
return $result
163+
}
148164
}
149165
catch {
150166
throw "Failed to save post content: $($_.Exception.Message)"
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+
}

0 commit comments

Comments
 (0)