Skip to content

Commit d0233d0

Browse files
committed
remove special characters from file name
1 parent eb3872d commit d0233d0

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/public/Get-BloggerPost.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ Function Get-BloggerPost {
145145
$frontMatter['tags'] = @()
146146
}
147147
Write-Verbose "Saving frontmatter: $($frontMatter | ConvertTo-Json -Depth 10)"
148-
$file = "$title.md"
148+
$sanitizedTitle = $title -replace '[\\\/:\*\?"<>\|]', '_' -replace '\s+', ' ' -replace '^\s+|\s+$',''
149+
$file = "$sanitizedTitle.md"
149150

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

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,54 @@ Describe "Get-BloggerPost" {
259259
$frontMatter['tags'] -eq $null | Should -BeFalse
260260
$frontMatter['tags'] | Should -Be @()
261261
}
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+
}
262310
}
263311

264312
Context "As Json" {

0 commit comments

Comments
 (0)