diff --git a/README.md b/README.md index 47e5d91..ff15082 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,12 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg When using `Markdown` format, files are saved as `.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. diff --git a/src/public/Get-BloggerPost.ps1 b/src/public/Get-BloggerPost.ps1 index da19289..91665e0 100644 --- a/src/public/Get-BloggerPost.ps1 +++ b/src/public/Get-BloggerPost.ps1 @@ -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 @@ -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")] @@ -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 diff --git a/src/tests/Get-BloggerPost.Tests.ps1 b/src/tests/Get-BloggerPost.Tests.ps1 index c5abc49..84ad198 100644 --- a/src/tests/Get-BloggerPost.Tests.ps1 +++ b/src/tests/Get-BloggerPost.Tests.ps1 @@ -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>" } } @@ -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 { @@ -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>" } }