Skip to content

Commit b1d558d

Browse files
authored
Include labels in markdown (#21)
* Get-BloggerPost markdown includes labels * Publish-MarkdownBloggerPosts now supports -ExcludeLabels * add ExcludeLabels to user preferences and use as default * include empty tags in frontmatter when labels are not present
1 parent c86835c commit b1d558d

8 files changed

Lines changed: 101 additions & 4 deletions

src/private/Get-BloggerSession.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Function Get-BloggerSession
1111
PandocTemplate = "$($env:USERPROFILE)\\.PSBlogger\\template.html"
1212
PandocAdditionalArgs = "--html-q-tags --ascii"
1313
BlogId = $null
14+
ExcludeLabels = @()
1415
}
1516

1617
if (Test-Path $session.UserPreferences)

src/public/Get-BloggerConfig.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Function Get-BloggerConfig
1111
PandocHtmlFormat = $BloggerSession.PandocHtmlFormat
1212
PandocAdditionalArgs = $BloggerSession.PandocAdditionalArgs
1313
BlogId = $BloggerSession.BlogId
14+
ExcludeLabels = $BloggerSession.ExcludeLabels
1415
}
1516
}

src/public/Get-BloggerPost.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ Function Get-BloggerPost {
116116
$frontMatter = [ordered]@{
117117
postId = $result.id
118118
}
119+
if ($result['labels']) {
120+
$frontMatter['tags'] = $result.labels
121+
} else {
122+
$frontMatter['tags'] = @()
123+
}
119124
$file = "$title.md"
120125
$filePath = Join-Path -Path $OutDirectory -ChildPath $file
121126
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath > $null

src/public/Publish-MarkdownBloggerPost.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ Function Publish-MarkdownBloggerPost
4242
[Parameter(Mandatory=$false)]
4343
[switch]$Draft,
4444

45+
[Parameter(Mandatory=$false)]
46+
[array]$ExcludeLabels = @(),
47+
4548
[Parameter(Mandatory=$false)]
4649
[switch]$Force
50+
4751
)
4852

4953
if (!$PSBoundParameters.ContainsKey("BlogId"))
@@ -54,6 +58,10 @@ Function Publish-MarkdownBloggerPost
5458
}
5559
}
5660

61+
if (!$PSBoundParameters.ContainsKey("ExcludeLabels")) {
62+
$ExcludeLabels = $BloggerSession.ExcludeLabels
63+
}
64+
5765
# grab the front matter
5866
$postInfo = Get-MarkdownFrontMatter -File $File
5967

@@ -79,7 +87,7 @@ Function Publish-MarkdownBloggerPost
7987
}
8088

8189
if ($postInfo["tags"]) {
82-
$postArgs.Labels = [array]$postInfo.tags
90+
$postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels }
8391
}
8492

8593
$post = Publish-BloggerPost @postArgs

src/public/Set-BloggerConfig.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Function Set-BloggerConfig
33
[CmdletBinding()]
44
Param(
55
[Parameter(Mandatory=$true)]
6-
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat")]
6+
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels")]
77
[string]$Name,
88

99
[Parameter(Mandatory=$true)]
1010
[AllowEmptyString()]
11-
[string]$Value
11+
$Value
1212
)
1313
$userPreferences = [pscustomobject]@{}
1414

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Describe "Get-BloggerPost" {
184184
title = "Test Post"
185185
published = [datetime]"2023-10-01T17:30:00-04:00"
186186
content = "<h1>Hello World</h1><p>This is a post.</p>"
187+
labels = @("Azure DevOps", "Azure Pipelines")
187188
}
188189
}
189190
}
@@ -200,7 +201,7 @@ Describe "Get-BloggerPost" {
200201
}
201202
}
202203

203-
It "Should write post details to frontmatter" {
204+
It "Should write postid to frontmatter" {
204205

205206
# act
206207
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
@@ -209,6 +210,44 @@ Describe "Get-BloggerPost" {
209210
$frontMatter = Get-MarkdownFrontMatter -File $outFile
210211
$frontMatter.postId | Should -Be "123"
211212
}
213+
214+
It "Should write labels to frontmatter" {
215+
# act
216+
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
217+
218+
# assert
219+
$frontMatter = Get-MarkdownFrontMatter -File $outFile
220+
$frontMatter.tags.Count | Should -Be 2
221+
$frontMatter.tags[0] | Should -Be "Azure DevOps"
222+
$frontMatter.tags[1] | Should -Be "Azure Pipelines"
223+
}
224+
225+
It "Should include empty tags in frontmatter if labels are not present" {
226+
# arrange
227+
InModuleScope PSBlogger {
228+
# Mock the session to return a test blog ID
229+
$BloggerSession.BlogId = "test-blog-id"
230+
231+
$postId = "123"
232+
233+
# mock post retrieval
234+
Mock Invoke-GApi {
235+
return @{
236+
id = $postId
237+
title = "Test Post"
238+
published = [datetime]"2023-10-01T17:30:00-04:00"
239+
content = "<h1>Hello World</h1><p>This is a post.</p>"
240+
}
241+
}
242+
}
243+
# act
244+
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
245+
246+
# assert
247+
$frontMatter = Get-MarkdownFrontMatter -File $outFile
248+
$frontMatter['tags'] -eq $null | Should -BeFalse
249+
$frontMatter['tags'] | Should -Be @()
250+
}
212251
}
213252

214253
Context "As Json" {
@@ -280,6 +319,7 @@ Describe "Get-BloggerPost" {
280319
It "Should write file to specified formatted directory - <dateformat> - <format>" -TestCases @(
281320
@{ DateFormat = "yyyy\\MM"; ExpectedPath = "TestDrive:\2023\10\Test Post.md"; Format = "Markdown" }
282321
@{ DateFormat = "yyyy\\MM\\dd"; ExpectedPath = "TestDrive:\2023\10\01\123.html"; Format = "HTML" }
322+
@{ DateFormat = "yyyy"; ExpectedPath = "TestDrive:\2023\123.json"; Format = "JSON"}
283323
) {
284324
# arrange
285325
$invokeArgs = @{

src/tests/Publish-MarkdownBloggerPost.Tests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,45 @@ postId: "123456"
146146
Should -InvokeVerifiable
147147
}
148148

149+
It "Should exclude certain tags from post labels when publishing " {
150+
# arrange
151+
152+
# add our tags to the file
153+
$postInfo = Get-MarkdownFrontMatter -File $validFile
154+
$postInfo.tags = @("PowerShell","Pester", "personal/blog-post")
155+
Set-MarkdownFrontMatter -File $validFile -Replace $postInfo
156+
157+
InModuleScope PSBlogger {
158+
Mock Publish-BloggerPost -Verifiable -ParameterFilter {
159+
$Labels -ne $null -and (-not (Compare-Object $Labels @("PowerShell","Pester")))} { return @{ id="123"} }
160+
}
161+
162+
# act
163+
Publish-MarkdownBloggerPost -File $validFile -BlogId "123" -ExcludeLabels "personal/blog-post"
164+
165+
# assert
166+
Should -InvokeVerifiable
167+
}
168+
169+
It "Should use excludelabels user preference when not specified" {
170+
# arrange
171+
InModuleScope PSBlogger {
172+
$BloggerSession.ExcludeLabels = @("personal/blog-post")
173+
Mock Publish-BloggerPost -Verifiable -ParameterFilter {
174+
$Labels -ne $null -and (-not (Compare-Object $Labels @("PowerShell","Pester")))} { return @{ id="123"} }
175+
}
176+
177+
# add our tags to the file
178+
$postInfo = Get-MarkdownFrontMatter -File $validFile
179+
$postInfo.tags = @("PowerShell","Pester", "personal/blog-post")
180+
Set-MarkdownFrontMatter -File $validFile -Replace $postInfo
181+
182+
# act
183+
Publish-MarkdownBloggerPost -File $validFile -BlogId "123"
184+
185+
# assert
186+
Should -InvokeVerifiable
187+
}
149188

150189
It "Should update front matter with postid after publishing" {
151190
# arrange

src/tests/Set-BloggerConfig.Tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Describe "Set-BloggerConfig" {
1313
# Create a new test-specific BloggerSession
1414
$BloggerSession = [pscustomobject]@{
1515
BlogId = $null
16+
ExcludeLabels = @()
1617
UserPreferences = "TestDrive:\UserPreferences.json"
1718
}
1819

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

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

2830
# act
@@ -37,6 +39,7 @@ Describe "Set-BloggerConfig" {
3739

3840
It "Should persist new value to <UserPreference> to empty BloggerSession.UserPreferences file" -TestCases @(
3941
@{ UserPreference="BlogId"; UserPreferenceValue="12345" }
42+
@{ UserPreference="ExcludeLabels"; UserPreferenceValue=@("personal/blog-post") }
4043
) {
4144
# arrange: empty file
4245
Set-Content TestDrive:\UserPreferences.json -Value "{}"

0 commit comments

Comments
 (0)