Skip to content

Commit c86835c

Browse files
authored
Get-BloggerPost supports -Format Markdown (#20)
* Get-BloggerPost supports -Format JSON * updated tests and readme * fixed indentation for tests
1 parent 5c83436 commit c86835c

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg
6464

6565
When using `Markdown` format, files are saved as `<title>.md`
6666

67+
When using `JSON` format, files are saved as `<postid>.json`
68+
6769
```
6870
Get-BloggerPost -PostId <postid> -Format HTML
6971
Get-BloggerPost -PostId <postid> -Format Markdown
72+
Get-BloggerPost -PostId <postid> -Format JSON
7073
```
7174

7275
You can specify an output directory where the file will be saved.

src/public/Get-BloggerPost.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
The ID of the post to retrieve. This parameter is required.
1010
1111
.PARAMETER Format
12-
The format of the post content to retrieve. Use either Markdown or HTML.
12+
The format of the post content to retrieve. Use either Markdown, JSON or HTML.
1313
1414
.PARAMETER FolderDateFormat
1515
The folder name as expressed in a DateTime format string. For example, "YYYY/MM" which will save files
@@ -39,7 +39,7 @@ Function Get-BloggerPost {
3939
[string]$PostId,
4040

4141
[Parameter(Mandatory, ParameterSetName = "Persist")]
42-
[ValidateSet("HTML", "Markdown")]
42+
[ValidateSet("HTML", "Markdown", "JSON")]
4343
[string]$Format,
4444

4545
[Parameter(ParameterSetName ="Persist")]
@@ -122,6 +122,13 @@ Function Get-BloggerPost {
122122
Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter
123123
Write-Verbose "Post content saved to: $filePath"
124124
}
125+
126+
"JSON" {
127+
$fileName = "$PostId.json"
128+
$filePath = Join-Path -Path $OutDirectory -ChildPath $fileName
129+
$result | ConvertTo-Json | Out-File -FilePath $filePath -Encoding UTF8
130+
Write-Verbose "Post content saved to: $filePath"
131+
}
125132
}
126133

127134
# Return the post object for further processing if needed

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Describe "Get-BloggerPost" {
182182
return @{
183183
id = $postId
184184
title = "Test Post"
185-
published = "10/01/2023 12:00:00"
185+
published = [datetime]"2023-10-01T17:30:00-04:00"
186186
content = "<h1>Hello World</h1><p>This is a post.</p>"
187187
}
188188
}
@@ -211,6 +211,51 @@ Describe "Get-BloggerPost" {
211211
}
212212
}
213213

214+
Context "As Json" {
215+
BeforeEach {
216+
InModuleScope PSBlogger {
217+
# Mock the session to return a test blog ID
218+
$BloggerSession.BlogId = "test-blog-id"
219+
220+
$postId = "123"
221+
222+
# mock post retrieval
223+
Mock Invoke-GApi {
224+
return @{
225+
id = $postId
226+
title = "Test Post"
227+
published = [datetime]"2023-10-01T17:30:00-04:00"
228+
content = "<h1>Hello World</h1><p>This is a post.</p>"
229+
}
230+
}
231+
}
232+
233+
$postId = "123"
234+
$title = "Test Post"
235+
$outFile = "TestDrive:\$postId.json"
236+
237+
}
238+
239+
AfterEach {
240+
if (Test-Path $outFile) {
241+
Remove-Item $outFile -Force
242+
}
243+
}
244+
245+
It "Should write json response to file" {
246+
247+
# act
248+
Get-BloggerPost -PostId $postId -Format JSON -OutDirectory "TestDrive:\"
249+
250+
# assert
251+
$jsonContent = Get-Content -Path $outFile -Raw | ConvertFrom-Json
252+
$jsonContent.id | Should -Be "123"
253+
$jsonContent.title | Should -Be "Test Post"
254+
$jsonContent.content | Should -Not -BeNullOrEmpty
255+
$jsonContent.published | Should -Not -BeNullOrEmpty
256+
}
257+
}
258+
214259
Context "Using FolderDateFormat" {
215260

216261
BeforeEach {
@@ -225,7 +270,7 @@ Describe "Get-BloggerPost" {
225270
return @{
226271
id = $postId
227272
title = "Test Post"
228-
published = [datetime]"10/01/2023 12:00:00"
273+
published = [datetime]"2023-10-01T17:30:00-04:00"
229274
content = "<h1>Hello World</h1><p>This is a post.</p>"
230275
}
231276
}

0 commit comments

Comments
 (0)