Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg

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

When using `JSON` format, files are saved as `<postid>.json`

```
Get-BloggerPost -PostId <postid> -Format HTML
Get-BloggerPost -PostId <postid> -Format Markdown
Get-BloggerPost -PostId <postid> -Format JSON
```

You can specify an output directory where the file will be saved.
Expand Down
11 changes: 9 additions & 2 deletions src/public/Get-BloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
The ID of the post to retrieve. This parameter is required.

.PARAMETER Format
The format of the post content to retrieve. Use either Markdown or HTML.
The format of the post content to retrieve. Use either Markdown, JSON or HTML.

.PARAMETER FolderDateFormat
The folder name as expressed in a DateTime format string. For example, "YYYY/MM" which will save files
Expand Down Expand Up @@ -39,7 +39,7 @@ Function Get-BloggerPost {
[string]$PostId,

[Parameter(Mandatory, ParameterSetName = "Persist")]
[ValidateSet("HTML", "Markdown")]
[ValidateSet("HTML", "Markdown", "JSON")]
[string]$Format,

[Parameter(ParameterSetName ="Persist")]
Expand Down Expand Up @@ -122,6 +122,13 @@ Function Get-BloggerPost {
Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter
Write-Verbose "Post content saved to: $filePath"
}

"JSON" {
$fileName = "$PostId.json"
$filePath = Join-Path -Path $OutDirectory -ChildPath $fileName
$result | ConvertTo-Json | Out-File -FilePath $filePath -Encoding UTF8
Write-Verbose "Post content saved to: $filePath"
}
}

# Return the post object for further processing if needed
Expand Down
49 changes: 47 additions & 2 deletions src/tests/Get-BloggerPost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Describe "Get-BloggerPost" {
return @{
id = $postId
title = "Test Post"
published = "10/01/2023 12:00:00"
published = [datetime]"2023-10-01T17:30:00-04:00"
content = "<h1>Hello World</h1><p>This is a post.</p>"
}
}
Expand Down Expand Up @@ -211,6 +211,51 @@ Describe "Get-BloggerPost" {
}
}

Context "As Json" {
BeforeEach {
InModuleScope PSBlogger {
# Mock the session to return a test blog ID
$BloggerSession.BlogId = "test-blog-id"

$postId = "123"

# mock post retrieval
Mock Invoke-GApi {
return @{
id = $postId
title = "Test Post"
published = [datetime]"2023-10-01T17:30:00-04:00"
content = "<h1>Hello World</h1><p>This is a post.</p>"
}
}
}

$postId = "123"
$title = "Test Post"
$outFile = "TestDrive:\$postId.json"

}

AfterEach {
if (Test-Path $outFile) {
Remove-Item $outFile -Force
}
}

It "Should write json response to file" {

# act
Get-BloggerPost -PostId $postId -Format JSON -OutDirectory "TestDrive:\"

# assert
$jsonContent = Get-Content -Path $outFile -Raw | ConvertFrom-Json
$jsonContent.id | Should -Be "123"
$jsonContent.title | Should -Be "Test Post"
$jsonContent.content | Should -Not -BeNullOrEmpty
$jsonContent.published | Should -Not -BeNullOrEmpty
}
}

Context "Using FolderDateFormat" {

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