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
1 change: 1 addition & 0 deletions src/private/Get-BloggerSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/public/Get-BloggerConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Function Get-BloggerConfig
PandocHtmlFormat = $BloggerSession.PandocHtmlFormat
PandocAdditionalArgs = $BloggerSession.PandocAdditionalArgs
BlogId = $BloggerSession.BlogId
ExcludeLabels = $BloggerSession.ExcludeLabels
}
}
5 changes: 5 additions & 0 deletions src/public/Get-BloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/public/Publish-MarkdownBloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -54,6 +58,10 @@ Function Publish-MarkdownBloggerPost
}
}

if (!$PSBoundParameters.ContainsKey("ExcludeLabels")) {
$ExcludeLabels = $BloggerSession.ExcludeLabels
}

# grab the front matter
$postInfo = Get-MarkdownFrontMatter -File $File

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/public/Set-BloggerConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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]@{}

Expand Down
42 changes: 41 additions & 1 deletion src/tests/Get-BloggerPost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Describe "Get-BloggerPost" {
title = "Test Post"
published = [datetime]"2023-10-01T17:30:00-04:00"
content = "<h1>Hello World</h1><p>This is a post.</p>"
labels = @("Azure DevOps", "Azure Pipelines")
}
}
}
Expand All @@ -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:\"
Expand All @@ -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 = "<h1>Hello World</h1><p>This is a post.</p>"
}
}
}
# 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" {
Expand Down Expand Up @@ -280,6 +319,7 @@ Describe "Get-BloggerPost" {
It "Should write file to specified formatted directory - <dateformat> - <format>" -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 = @{
Expand Down
39 changes: 39 additions & 0 deletions src/tests/Publish-MarkdownBloggerPost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/tests/Set-BloggerConfig.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Describe "Set-BloggerConfig" {
# Create a new test-specific BloggerSession
$BloggerSession = [pscustomobject]@{
BlogId = $null
ExcludeLabels = @()
UserPreferences = "TestDrive:\UserPreferences.json"
}

Expand All @@ -23,6 +24,7 @@ Describe "Set-BloggerConfig" {

It "Should persist new value to <UserPreference> to BloggerSession.UserPreferences" -TestCases @(
@{ UserPreference="BlogId"; UserPreferenceValue="12345" }
@{ UserPreference="ExcludeLabels"; UserPreferenceValue=@("personal/blog-post") }
) {

# act
Expand All @@ -37,6 +39,7 @@ Describe "Set-BloggerConfig" {

It "Should persist new value to <UserPreference> 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 "{}"
Expand Down