Skip to content

Commit 2d6bbf3

Browse files
committed
Added -PreserveOriginal to Publish-MarkdownBloggerPost
1 parent d9103ef commit 2d6bbf3

2 files changed

Lines changed: 208 additions & 39 deletions

File tree

src/public/Publish-MarkdownBloggerPost.ps1

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
.PARAMETER Force
2323
If specified, will overwrite existing images in Google Drive with the same name.
2424
25+
.PARAMETER PreserveOriginal
26+
If specified, preserves the original markdown file with local image references. The file with Google Drive URLs
27+
is used only for HTML conversion and blog publishing.
28+
2529
.PARAMETER Open
2630
If specified, launches a browser to view the post after publishing.
2731
@@ -42,8 +46,12 @@
4246
Publish-MarkdownBloggerPost -File "my-post.md" -Open
4347
4448
.EXAMPLE
45-
# publish or update a draft, launching a web browser with the page preview
49+
# publish or update a post, launching a web browser with the page preview
4650
Publish-MarkdownBloggerPost -File "my-post.md" -Draft -Open
51+
52+
.EXAMPLE
53+
# publish or update a post while preserving local image references in the original file
54+
Publish-MarkdownBloggerPost -File "my-post.md" -PreserveOriginal
4755
#>
4856
Function Publish-MarkdownBloggerPost
4957
{
@@ -68,11 +76,15 @@ Function Publish-MarkdownBloggerPost
6876
[Parameter(Mandatory=$false)]
6977
[switch]$Force,
7078

79+
[Parameter(Mandatory=$false)]
80+
[switch]$PreserveOriginal,
81+
7182
[Parameter(Mandatory=$false)]
7283
[switch]$Open
7384

7485
)
7586

87+
# Obtain -BlogId from User Preferences if available
7688
if (!$PSBoundParameters.ContainsKey("BlogId"))
7789
{
7890
$BlogId = $BloggerSession.BlogId
@@ -81,6 +93,7 @@ Function Publish-MarkdownBloggerPost
8193
}
8294
}
8395

96+
# Obtain -ExcludeLabesl from User Preferences if availabe
8497
if (!$PSBoundParameters.ContainsKey("ExcludeLabels")) {
8598
$ExcludeLabels = $BloggerSession.ExcludeLabels
8699
}
@@ -89,48 +102,72 @@ Function Publish-MarkdownBloggerPost
89102
$postInfo = Get-MarkdownFrontMatter -File $File
90103

91104
# Process images: detect, upload to Google Drive, and update markdown
92-
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force
105+
$tempFile = $null
106+
$fileForConversion = $File
93107

94-
# convert from markdown to html file
95-
$content = ConvertTo-HtmlFromMarkdown -File $File
96-
97-
# TODO: Extension point to apply corrections to HTML
98-
# - eg: remove instances of <pre><code> from the content
99-
100-
# construct args
101-
$postArgs = @{
102-
BlogId = $BlogId
103-
Title = $postInfo.title
104-
Content = $content
105-
Draft = $Draft
106-
Open = $Open
107-
}
108+
try {
109+
if ($PreserveOriginal) {
110+
# Create temporary file for modified content
111+
$tempFile = [System.IO.Path]::GetTempFileName()
112+
$tempFile = [System.IO.Path]::ChangeExtension($tempFile, ".md")
113+
Write-Verbose "Using temporary file for image processing: $tempFile"
114+
115+
# publishes markdown drive images to google drive and writes the output to the specified OutFile
116+
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force -OutFile $tempFile
117+
$fileForConversion = $tempFile
118+
}
119+
else {
120+
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force
121+
}
122+
123+
# convert from markdown to html file
124+
$content = ConvertTo-HtmlFromMarkdown -File $fileForConversion
125+
126+
# TODO: Extension point to apply corrections to HTML
127+
# - eg: remove instances of <pre><code> from the content
128+
129+
# construct args
130+
$postArgs = @{
131+
BlogId = $BlogId
132+
Title = $postInfo.title
133+
Content = $content
134+
Draft = $Draft
135+
Open = $Open
136+
}
108137

109-
if ($postInfo["postId"]) {
110-
$postArgs.PostId = $postInfo.postid
111-
}
138+
if ($postInfo["postId"]) {
139+
$postArgs.PostId = $postInfo.postid
140+
}
112141

113-
if ($postInfo["tags"]) {
114-
$postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels }
115-
}
116-
117-
Write-Verbose "Publishing blogger post with args: $($postArgs | ConvertTo-Json -Depth 5)"
118-
$post = Publish-BloggerPost @postArgs
119-
120-
# update post id
121-
$postInfo["postId"] = $post.id
122-
if ($Draft) {
123-
Write-Verbose "Adding 'wip' to front matter"
124-
$postInfo["wip"] = $true
125-
} else {
126-
if ($postInfo["wip"]) {
127-
Write-Verbose "Removing 'wip' from front matter"
128-
$postInfo.Remove("wip")
142+
if ($postInfo["tags"]) {
143+
$postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels }
144+
}
145+
146+
Write-Verbose "Publishing blogger post with args: $($postArgs | ConvertTo-Json -Depth 5)"
147+
$post = Publish-BloggerPost @postArgs
148+
149+
# update post id
150+
$postInfo["postId"] = $post.id
151+
if ($Draft) {
152+
Write-Verbose "Adding 'wip' to front matter"
153+
$postInfo["wip"] = $true
154+
} else {
155+
if ($postInfo["wip"]) {
156+
Write-Verbose "Removing 'wip' from front matter"
157+
$postInfo.Remove("wip")
158+
}
129159
}
130-
}
131160

132-
Write-Verbose "Updating front matter with post id: $($postInfo['postId'])"
133-
Set-MarkdownFrontMatter -File $File -Replace $postInfo
161+
Write-Verbose "Updating front matter with post id: $($postInfo['postId'])"
162+
Set-MarkdownFrontMatter -File $File -Replace $postInfo
134163

135-
return $post
164+
return $post
165+
}
166+
finally {
167+
# Clean up temporary file if it was created
168+
if ($tempFile -and (Test-Path $tempFile)) {
169+
Write-Verbose "Cleaning up temporary file: $tempFile"
170+
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
171+
}
172+
}
136173
}

src/tests/Publish-MarkdownBloggerPost.Tests.ps1

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,136 @@ postId: "123456"
327327
}
328328

329329
}
330+
331+
Context "PreserveOriginal Parameter" {
332+
333+
BeforeEach {
334+
InModuleScope PSBlogger {
335+
$script:capturedTempFile = $null
336+
337+
# Simulate the behavior of Publish-MarkdownDriveImages with our temporary file
338+
# and capture the tempoary file name
339+
Mock Publish-MarkdownDriveImages -ParameterFilter {
340+
$OutFile -ne $null -and $OutFile -ne "TestDrive:\validfile.md"
341+
} {
342+
$script:capturedTempFile = $OutFile
343+
# simulate creating the OutFile
344+
New-Item -Path $OutFile -ItemType File -Force | Out-Null
345+
return @()
346+
} -Verifiable
347+
348+
# ensure temporary file is converted to html
349+
Mock ConvertTo-HtmlFromMarkdown -ParameterFilter { $File -ne "TestDrive:\validfile.md" } {
350+
return "dummy"
351+
} -Verifiable
352+
}
353+
}
354+
355+
It "Should not modify original file when PreserveOriginal is specified" {
356+
# arrange
357+
InModuleScope PSBlogger {
358+
359+
# front matter should be modified on the original file
360+
Mock Set-MarkdownFrontMatter -ParameterFilter { $File -like "*validfile.md"} -Verifiable
361+
}
362+
363+
# act
364+
Publish-MarkdownBloggerPost -File $validFile -BlogId 1234 -PreserveOriginal
365+
366+
# assert
367+
Should -InvokeVerifiable
368+
}
369+
370+
It "Should create and clean up temporary file when PreserveOriginal is used" {
371+
# arrange
372+
373+
# act
374+
Publish-MarkdownBloggerPost -File $validFile -BlogId 1234 -PreserveOriginal
375+
376+
# assert
377+
InModuleScope PSBlogger {
378+
$script:capturedTempFile | Should -Not -BeNullOrEmpty
379+
$script:capturedTempFile | Should -Match "tmp.*\.md$"
380+
Test-Path $script:capturedTempFile | Should -Be $false # Should be cleaned up
381+
}
382+
}
383+
384+
It "Should clean up temporary file even when conversion fails" {
385+
# arrange
386+
InModuleScope PSBlogger {
387+
# override existing mock to simulate conversion failure
388+
Mock ConvertTo-HtmlFromMarkdown -ParameterFilter { $File -ne "TestDrive:\validfile.md" } {
389+
throw "Conversion failed"
390+
}
391+
}
392+
393+
# act & assert
394+
{
395+
Publish-MarkdownBloggerPost -File $validFile -BlogId 1234 -PreserveOriginal
396+
} | Should -Throw "Conversion failed"
397+
398+
# assert cleanup occurred
399+
InModuleScope PSBlogger {
400+
if ($script:capturedTempFile) {
401+
Test-Path $script:capturedTempFile | Should -Be $false
402+
}
403+
}
404+
}
405+
406+
It "Should update front matter in original file even with PreserveOriginal" {
407+
# arrange
408+
409+
InModuleScope PSBlogger {
410+
Mock Set-MarkdownFrontMatter -ParameterFilter { $Replace.postId -eq "123" } { } -Verifiable
411+
}
412+
413+
# act
414+
Publish-MarkdownBloggerPost -File $validFile -BlogId 1234 -PreserveOriginal
415+
416+
# assert
417+
Should -InvokeVerifiable
418+
}
419+
}
420+
421+
Context "Image Resolution when using PreserveOriginal" {
422+
423+
It "Should locate images relative to the original file" {
424+
# arrange
425+
# setup a markdown file with an image that refers to a file relative to the markdown file
426+
$imageFile = New-TestImage "TestDrive:\note\image.png"
427+
$markdownFile = "TestDrive:\note\post.md"
428+
$markdownContent = @(
429+
"# Test Post"
430+
""
431+
"Image reference: ![Image Relative to note](image.png)"
432+
) -join "`n"
433+
Set-MarkdownFile $markdownFile $markdownContent
434+
435+
InModuleScope PSBlogger {
436+
# mock the image upload to google drive
437+
438+
Mock Add-GoogleDriveFile -Verifiable {
439+
return @{
440+
id = "12345"
441+
PublicUrl = "https://drive.google.com/uc?export=view&id=12345"
442+
}
443+
}
444+
Mock Set-GoogleDriveFilePermission {}
445+
446+
Mock Publish-BloggerPost {
447+
# return the passed in content in the result
448+
return @{ id = "123"; content = $Content}
449+
}
450+
451+
# remove existing mock defined for ConvertTo-HtmlFromMarkdown
452+
Remove-Alias ConvertTo-HtmlFromMarkdown
453+
}
454+
455+
# act
456+
$result = Publish-MarkdownBloggerPost -File $markdownFile -BlogId 1234 -PreserveOriginal
457+
458+
# assert
459+
$result.content | Should -BeLike "*https://drive.google.com*"
460+
}
461+
}
330462
}

0 commit comments

Comments
 (0)