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
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#>
4856Function 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}
0 commit comments