Skip to content

Commit 2af0d90

Browse files
authored
remove special characters from file (#66)
* include title in frontmatter * remove special characters from file name --------- Co-authored-by: bryan cook <3217452+bryancook@users.noreply.github.com>
1 parent 557b6f8 commit 2af0d90

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/public/Get-BloggerPost.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Function Get-BloggerPost {
135135
$title = $result.title
136136
$frontMatter = [ordered]@{
137137
postId = $result.id
138+
title = $result.title
138139
}
139140
if ($result.PSObject.Properties.Name -contains "labels") {
140141
Write-Verbose "Using post labels: $($result.labels)"
@@ -144,7 +145,8 @@ Function Get-BloggerPost {
144145
$frontMatter['tags'] = @()
145146
}
146147
Write-Verbose "Saving frontmatter: $($frontMatter | ConvertTo-Json -Depth 10)"
147-
$file = "$title.md"
148+
$sanitizedTitle = $title -replace '[\\\/:\*\?"<>\|]', '_' -replace '\s+', ' ' -replace '^\s+|\s+$',''
149+
$file = "$sanitizedTitle.md"
148150

149151
$filePath = Join-Path -Path $OutDirectory -ChildPath $file
150152
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ Describe "Get-BloggerPost" {
213213
$frontMatter.postId | Should -Be "123"
214214
}
215215

216+
It "Should write title to frontmatter" {
217+
# act
218+
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
219+
220+
# assert
221+
$frontMatter = Get-MarkdownFrontMatter -File $outFile
222+
$frontMatter.title | Should -Be "Test Post"
223+
}
224+
216225
It "Should write labels to frontmatter" {
217226
# act
218227
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
@@ -250,6 +259,54 @@ Describe "Get-BloggerPost" {
250259
$frontMatter['tags'] -eq $null | Should -BeFalse
251260
$frontMatter['tags'] | Should -Be @()
252261
}
262+
263+
Context "Illegal characters in title" {
264+
It "Should sanitize filename when title contains special character (<character>)" -TestCases @(
265+
@{ character = '?'; }
266+
@{ character = '/'; }
267+
@{ character = ':'; }
268+
@{ character = '*'; }
269+
@{ character = '"'; }
270+
@{ character = '<'; }
271+
@{ character = '>'; }
272+
@{ character = '|'; }
273+
) {
274+
$title = "Test$($character)Post"
275+
276+
# arrange
277+
InModuleScope PSBlogger -Parameters @{ testTitle = $title } {
278+
param($testTitle)
279+
280+
$script:currentTitle = $testTitle
281+
282+
# Mock the session to return a test blog ID
283+
$BloggerSession.BlogId = "test-blog-id"
284+
285+
$postId = "123"
286+
287+
# mock post retrieval
288+
Mock Invoke-GApi -MockWith {
289+
[pscustomobject]@{
290+
id = "123"
291+
title = $script:currentTitle
292+
published = [datetime]"2023-10-01T17:30:00-04:00"
293+
content = "<h1>Hello World</h1><p>This is a post.</p>"
294+
}
295+
}
296+
}
297+
298+
$expectedFileName = "Test_Post.md"
299+
$outFile = Get-TestFilePath $expectedFileName
300+
301+
# act
302+
Get-BloggerPost -PostId $postId -Format Markdown -OutDirectory "TestDrive:\"
303+
304+
# assert
305+
Test-Path $outFile | Should -BeTrue
306+
$frontMatter = Get-MarkdownFrontMatter -File $outFile
307+
$frontMatter.title | Should -Be $title
308+
}
309+
}
253310
}
254311

255312
Context "As Json" {

0 commit comments

Comments
 (0)