diff --git a/src/private/Get-BloggerSession.ps1 b/src/private/Get-BloggerSession.ps1 index 68473ce..e3212bb 100644 --- a/src/private/Get-BloggerSession.ps1 +++ b/src/private/Get-BloggerSession.ps1 @@ -11,6 +11,7 @@ Function Get-BloggerSession PandocTemplate = "$($env:USERPROFILE)\\.PSBlogger\\template.html" PandocAdditionalArgs = "--html-q-tags --ascii" BlogId = $null + ExcludeLabels = @() } if (Test-Path $session.UserPreferences) diff --git a/src/public/Get-BloggerConfig.ps1 b/src/public/Get-BloggerConfig.ps1 index 7ab3291..0eefe29 100644 --- a/src/public/Get-BloggerConfig.ps1 +++ b/src/public/Get-BloggerConfig.ps1 @@ -11,5 +11,6 @@ Function Get-BloggerConfig PandocHtmlFormat = $BloggerSession.PandocHtmlFormat PandocAdditionalArgs = $BloggerSession.PandocAdditionalArgs BlogId = $BloggerSession.BlogId + ExcludeLabels = $BloggerSession.ExcludeLabels } } \ No newline at end of file diff --git a/src/public/Get-BloggerPost.ps1 b/src/public/Get-BloggerPost.ps1 index 91665e0..b570367 100644 --- a/src/public/Get-BloggerPost.ps1 +++ b/src/public/Get-BloggerPost.ps1 @@ -116,6 +116,11 @@ Function Get-BloggerPost { $frontMatter = [ordered]@{ postId = $result.id } + if ($result['labels']) { + $frontMatter['tags'] = $result.labels + } else { + $frontMatter['tags'] = @() + } $file = "$title.md" $filePath = Join-Path -Path $OutDirectory -ChildPath $file ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath > $null diff --git a/src/public/Publish-MarkdownBloggerPost.ps1 b/src/public/Publish-MarkdownBloggerPost.ps1 index 4d2f686..cf1b4d3 100644 --- a/src/public/Publish-MarkdownBloggerPost.ps1 +++ b/src/public/Publish-MarkdownBloggerPost.ps1 @@ -42,8 +42,12 @@ Function Publish-MarkdownBloggerPost [Parameter(Mandatory=$false)] [switch]$Draft, + [Parameter(Mandatory=$false)] + [array]$ExcludeLabels = @(), + [Parameter(Mandatory=$false)] [switch]$Force + ) if (!$PSBoundParameters.ContainsKey("BlogId")) @@ -54,6 +58,10 @@ Function Publish-MarkdownBloggerPost } } + if (!$PSBoundParameters.ContainsKey("ExcludeLabels")) { + $ExcludeLabels = $BloggerSession.ExcludeLabels + } + # grab the front matter $postInfo = Get-MarkdownFrontMatter -File $File @@ -79,7 +87,7 @@ Function Publish-MarkdownBloggerPost } if ($postInfo["tags"]) { - $postArgs.Labels = [array]$postInfo.tags + $postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels } } $post = Publish-BloggerPost @postArgs diff --git a/src/public/Set-BloggerConfig.ps1 b/src/public/Set-BloggerConfig.ps1 index d54c928..d4e9619 100644 --- a/src/public/Set-BloggerConfig.ps1 +++ b/src/public/Set-BloggerConfig.ps1 @@ -3,12 +3,12 @@ Function Set-BloggerConfig [CmdletBinding()] Param( [Parameter(Mandatory=$true)] - [ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat")] + [ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels")] [string]$Name, [Parameter(Mandatory=$true)] [AllowEmptyString()] - [string]$Value + $Value ) $userPreferences = [pscustomobject]@{} diff --git a/src/tests/Get-BloggerPost.Tests.ps1 b/src/tests/Get-BloggerPost.Tests.ps1 index 84ad198..0d396fc 100644 --- a/src/tests/Get-BloggerPost.Tests.ps1 +++ b/src/tests/Get-BloggerPost.Tests.ps1 @@ -184,6 +184,7 @@ Describe "Get-BloggerPost" { title = "Test Post" published = [datetime]"2023-10-01T17:30:00-04:00" content = "

Hello World

This is a post.

" + labels = @("Azure DevOps", "Azure Pipelines") } } } @@ -200,7 +201,7 @@ Describe "Get-BloggerPost" { } } - It "Should write post details to frontmatter" { + It "Should write postid to frontmatter" { # act Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\" @@ -209,6 +210,44 @@ Describe "Get-BloggerPost" { $frontMatter = Get-MarkdownFrontMatter -File $outFile $frontMatter.postId | Should -Be "123" } + + It "Should write labels to frontmatter" { + # act + Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\" + + # assert + $frontMatter = Get-MarkdownFrontMatter -File $outFile + $frontMatter.tags.Count | Should -Be 2 + $frontMatter.tags[0] | Should -Be "Azure DevOps" + $frontMatter.tags[1] | Should -Be "Azure Pipelines" + } + + It "Should include empty tags in frontmatter if labels are not present" { + # arrange + 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 = "

Hello World

This is a post.

" + } + } + } + # act + Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\" + + # assert + $frontMatter = Get-MarkdownFrontMatter -File $outFile + $frontMatter['tags'] -eq $null | Should -BeFalse + $frontMatter['tags'] | Should -Be @() + } } Context "As Json" { @@ -280,6 +319,7 @@ Describe "Get-BloggerPost" { It "Should write file to specified formatted directory - - " -TestCases @( @{ DateFormat = "yyyy\\MM"; ExpectedPath = "TestDrive:\2023\10\Test Post.md"; Format = "Markdown" } @{ DateFormat = "yyyy\\MM\\dd"; ExpectedPath = "TestDrive:\2023\10\01\123.html"; Format = "HTML" } + @{ DateFormat = "yyyy"; ExpectedPath = "TestDrive:\2023\123.json"; Format = "JSON"} ) { # arrange $invokeArgs = @{ diff --git a/src/tests/Publish-MarkdownBloggerPost.Tests.ps1 b/src/tests/Publish-MarkdownBloggerPost.Tests.ps1 index 06b7171..372a4e9 100644 --- a/src/tests/Publish-MarkdownBloggerPost.Tests.ps1 +++ b/src/tests/Publish-MarkdownBloggerPost.Tests.ps1 @@ -146,6 +146,45 @@ postId: "123456" Should -InvokeVerifiable } + It "Should exclude certain tags from post labels when publishing " { + # arrange + + # add our tags to the file + $postInfo = Get-MarkdownFrontMatter -File $validFile + $postInfo.tags = @("PowerShell","Pester", "personal/blog-post") + Set-MarkdownFrontMatter -File $validFile -Replace $postInfo + + InModuleScope PSBlogger { + Mock Publish-BloggerPost -Verifiable -ParameterFilter { + $Labels -ne $null -and (-not (Compare-Object $Labels @("PowerShell","Pester")))} { return @{ id="123"} } + } + + # act + Publish-MarkdownBloggerPost -File $validFile -BlogId "123" -ExcludeLabels "personal/blog-post" + + # assert + Should -InvokeVerifiable + } + + It "Should use excludelabels user preference when not specified" { + # arrange + InModuleScope PSBlogger { + $BloggerSession.ExcludeLabels = @("personal/blog-post") + Mock Publish-BloggerPost -Verifiable -ParameterFilter { + $Labels -ne $null -and (-not (Compare-Object $Labels @("PowerShell","Pester")))} { return @{ id="123"} } + } + + # add our tags to the file + $postInfo = Get-MarkdownFrontMatter -File $validFile + $postInfo.tags = @("PowerShell","Pester", "personal/blog-post") + Set-MarkdownFrontMatter -File $validFile -Replace $postInfo + + # act + Publish-MarkdownBloggerPost -File $validFile -BlogId "123" + + # assert + Should -InvokeVerifiable + } It "Should update front matter with postid after publishing" { # arrange diff --git a/src/tests/Set-BloggerConfig.Tests.ps1 b/src/tests/Set-BloggerConfig.Tests.ps1 index f296688..f7ae6a8 100644 --- a/src/tests/Set-BloggerConfig.Tests.ps1 +++ b/src/tests/Set-BloggerConfig.Tests.ps1 @@ -13,6 +13,7 @@ Describe "Set-BloggerConfig" { # Create a new test-specific BloggerSession $BloggerSession = [pscustomobject]@{ BlogId = $null + ExcludeLabels = @() UserPreferences = "TestDrive:\UserPreferences.json" } @@ -23,6 +24,7 @@ Describe "Set-BloggerConfig" { It "Should persist new value to to BloggerSession.UserPreferences" -TestCases @( @{ UserPreference="BlogId"; UserPreferenceValue="12345" } + @{ UserPreference="ExcludeLabels"; UserPreferenceValue=@("personal/blog-post") } ) { # act @@ -37,6 +39,7 @@ Describe "Set-BloggerConfig" { It "Should persist new value to to empty BloggerSession.UserPreferences file" -TestCases @( @{ UserPreference="BlogId"; UserPreferenceValue="12345" } + @{ UserPreference="ExcludeLabels"; UserPreferenceValue=@("personal/blog-post") } ) { # arrange: empty file Set-Content TestDrive:\UserPreferences.json -Value "{}"