Skip to content

Commit 330609e

Browse files
authored
Add -PreserveOriginal to Publish-MarkdownBloggerPost (#41)
* fix for leaky global state in find-markdownimages.tests * always write output file if specified, regardless of changes * Added -OutFile to Publish-MarkdownDriveImages * Added -PreserveOriginal to Publish-MarkdownBloggerPost
1 parent 284a32c commit 330609e

7 files changed

Lines changed: 331 additions & 61 deletions

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/public/Publish-MarkdownDriveImages.ps1

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
.PARAMETER Force
1717
If specified, will overwrite existing files in Google Drive with the same name.
1818
19+
.PARAMETER OutFile
20+
If specified, writes the updated markdown content with Google Drive URLs to this file instead of modifying the original file.
21+
1922
.EXAMPLE
2023
Publish-MarkdownDriveImages -File "blog-post.md"
2124
2225
.EXAMPLE
2326
Publish-MarkdownDriveImages -File "blog-post.md" -Force
27+
28+
.EXAMPLE
29+
Publish-MarkdownDriveImages -File "blog-post.md" -OutFile "blog-post-published.md"
2430
#>
2531
Function Publish-MarkdownDriveImages
2632
{
@@ -34,7 +40,10 @@ Function Publish-MarkdownDriveImages
3440
[string]$AttachmentsDirectory,
3541

3642
[Parameter(Mandatory=$false)]
37-
[switch]$Force
43+
[switch]$Force,
44+
45+
[Parameter(Mandatory=$false)]
46+
[string]$OutFile
3847
)
3948

4049
# Process images: detect, upload to Google Drive, and update markdown
@@ -54,11 +63,7 @@ Function Publish-MarkdownDriveImages
5463
$uploadParams = @{
5564
FilePath = $image.LocalPath
5665
FileName = $image.FileName
57-
Force = $false
58-
}
59-
60-
if ($Force) {
61-
$uploadParams.Force = $true
66+
Force = $Force
6267
}
6368

6469
$uploadResult = Add-GoogleDriveFile @uploadParams
@@ -84,13 +89,20 @@ Function Publish-MarkdownDriveImages
8489
Write-Warning "Failed to upload image $($image.FileName): $($_.Exception.Message)$([Environment]::NewLine)$($_.ErrorDetails | ConvertTo-Json -Depth 10)"
8590
}
8691
}
92+
}
93+
94+
# Update the markdown file with new URLs, or ensure that OutFile is created if specified
95+
if (($imageMappings -and $imageMappings.Count -gt 0) -or $OutFile) {
96+
$updateParams = @{
97+
File = $File
98+
ImageMappings = $imageMappings
99+
OutFile = $OutFile
100+
}
87101

88-
# Update the markdown file with new URLs
89-
if ($imageMappings -and $imageMappings.Count -gt 0) {
90-
$updated = Update-MarkdownImages -File $File -ImageMappings $imageMappings
91-
if ($updated) {
92-
Write-Verbose "Updated markdown file with $($imageMappings.Count) new image URLs"
93-
}
102+
$updated = Update-MarkdownImages @updateParams
103+
if ($updated) {
104+
$targetFile = if ($OutFile) { $OutFile } else { $File }
105+
Write-Verbose "Updated markdown file $targetFile with $($imageMappings.Count) new image URLs"
94106
}
95107
}
96108

src/public/Update-MarkdownImages.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function Update-MarkdownImages {
3636
[string]$File,
3737

3838
[Parameter(Mandatory = $true)]
39+
[AllowEmptyCollection()]
3940
[array]$ImageMappings,
4041

4142
[Parameter()]
@@ -71,11 +72,11 @@ function Update-MarkdownImages {
7172
$TargetFile = $OutFile
7273
}
7374

74-
# Only write the file if content has changed
75-
if ($content -ne $originalContent) {
75+
# Only write the file if content has changed or if OutFile is specified
76+
if ($content -ne $originalContent -or $OutFile) {
7677
Set-Content -Path $TargetFile -Value $content -NoNewline
7778
Write-Verbose "Updated markdown file: $TargetFile"
78-
return $true
79+
return ($content -ne $originalContent)
7980
}
8081
else {
8182
Write-Verbose "No changes made to markdown file: $File"

src/tests/Find-MarkdownImages.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Describe "Find-MarkdownImages" {
77
New-TestImage "TestDrive:\test-image1.png"
88
New-TestImage "TestDrive:\subfolder\test-image2.jpg"
99
New-TestImage "TestDrive:\absolute-image.gif"
10+
11+
InModuleScope PSBlogger {
12+
# reset blogger session to ensure that user preferences are not carried over into test
13+
$BloggerSession.AttachmentsDirectory = $null
14+
}
1015
}
1116

1217
Context "Basic image detection" {

0 commit comments

Comments
 (0)