Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 76 additions & 39 deletions src/public/Publish-MarkdownBloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
.PARAMETER Force
If specified, will overwrite existing images in Google Drive with the same name.

.PARAMETER PreserveOriginal
If specified, preserves the original markdown file with local image references. The file with Google Drive URLs
is used only for HTML conversion and blog publishing.

.PARAMETER Open
If specified, launches a browser to view the post after publishing.

Expand All @@ -42,8 +46,12 @@
Publish-MarkdownBloggerPost -File "my-post.md" -Open

.EXAMPLE
# publish or update a draft, launching a web browser with the page preview
# publish or update a post, launching a web browser with the page preview
Publish-MarkdownBloggerPost -File "my-post.md" -Draft -Open

.EXAMPLE
# publish or update a post while preserving local image references in the original file
Publish-MarkdownBloggerPost -File "my-post.md" -PreserveOriginal
#>
Function Publish-MarkdownBloggerPost
{
Expand All @@ -68,11 +76,15 @@ Function Publish-MarkdownBloggerPost
[Parameter(Mandatory=$false)]
[switch]$Force,

[Parameter(Mandatory=$false)]
[switch]$PreserveOriginal,

[Parameter(Mandatory=$false)]
[switch]$Open

)

# Obtain -BlogId from User Preferences if available
if (!$PSBoundParameters.ContainsKey("BlogId"))
{
$BlogId = $BloggerSession.BlogId
Expand All @@ -81,6 +93,7 @@ Function Publish-MarkdownBloggerPost
}
}

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

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

# convert from markdown to html file
$content = ConvertTo-HtmlFromMarkdown -File $File

# TODO: Extension point to apply corrections to HTML
# - eg: remove instances of <pre><code> from the content

# construct args
$postArgs = @{
BlogId = $BlogId
Title = $postInfo.title
Content = $content
Draft = $Draft
Open = $Open
}
try {
if ($PreserveOriginal) {
# Create temporary file for modified content
$tempFile = [System.IO.Path]::GetTempFileName()
$tempFile = [System.IO.Path]::ChangeExtension($tempFile, ".md")
Write-Verbose "Using temporary file for image processing: $tempFile"

# publishes markdown drive images to google drive and writes the output to the specified OutFile
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force -OutFile $tempFile
$fileForConversion = $tempFile
}
else {
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force
}

# convert from markdown to html file
$content = ConvertTo-HtmlFromMarkdown -File $fileForConversion

# TODO: Extension point to apply corrections to HTML
# - eg: remove instances of <pre><code> from the content

# construct args
$postArgs = @{
BlogId = $BlogId
Title = $postInfo.title
Content = $content
Draft = $Draft
Open = $Open
}

if ($postInfo["postId"]) {
$postArgs.PostId = $postInfo.postid
}
if ($postInfo["postId"]) {
$postArgs.PostId = $postInfo.postid
}

if ($postInfo["tags"]) {
$postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels }
}

Write-Verbose "Publishing blogger post with args: $($postArgs | ConvertTo-Json -Depth 5)"
$post = Publish-BloggerPost @postArgs

# update post id
$postInfo["postId"] = $post.id
if ($Draft) {
Write-Verbose "Adding 'wip' to front matter"
$postInfo["wip"] = $true
} else {
if ($postInfo["wip"]) {
Write-Verbose "Removing 'wip' from front matter"
$postInfo.Remove("wip")
if ($postInfo["tags"]) {
$postArgs.Labels = [array]$postInfo.tags | Where-Object { $_ -notin $ExcludeLabels }
}

Write-Verbose "Publishing blogger post with args: $($postArgs | ConvertTo-Json -Depth 5)"
$post = Publish-BloggerPost @postArgs

# update post id
$postInfo["postId"] = $post.id
if ($Draft) {
Write-Verbose "Adding 'wip' to front matter"
$postInfo["wip"] = $true
} else {
if ($postInfo["wip"]) {
Write-Verbose "Removing 'wip' from front matter"
$postInfo.Remove("wip")
}
}
}

Write-Verbose "Updating front matter with post id: $($postInfo['postId'])"
Set-MarkdownFrontMatter -File $File -Replace $postInfo
Write-Verbose "Updating front matter with post id: $($postInfo['postId'])"
Set-MarkdownFrontMatter -File $File -Replace $postInfo

return $post
return $post
}
finally {
# Clean up temporary file if it was created
if ($tempFile -and (Test-Path $tempFile)) {
Write-Verbose "Cleaning up temporary file: $tempFile"
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
}
}
}
36 changes: 24 additions & 12 deletions src/public/Publish-MarkdownDriveImages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
.PARAMETER Force
If specified, will overwrite existing files in Google Drive with the same name.

.PARAMETER OutFile
If specified, writes the updated markdown content with Google Drive URLs to this file instead of modifying the original file.

.EXAMPLE
Publish-MarkdownDriveImages -File "blog-post.md"

.EXAMPLE
Publish-MarkdownDriveImages -File "blog-post.md" -Force

.EXAMPLE
Publish-MarkdownDriveImages -File "blog-post.md" -OutFile "blog-post-published.md"
#>
Function Publish-MarkdownDriveImages
{
Expand All @@ -34,7 +40,10 @@ Function Publish-MarkdownDriveImages
[string]$AttachmentsDirectory,

[Parameter(Mandatory=$false)]
[switch]$Force
[switch]$Force,

[Parameter(Mandatory=$false)]
[string]$OutFile
)

# Process images: detect, upload to Google Drive, and update markdown
Expand All @@ -54,11 +63,7 @@ Function Publish-MarkdownDriveImages
$uploadParams = @{
FilePath = $image.LocalPath
FileName = $image.FileName
Force = $false
}

if ($Force) {
$uploadParams.Force = $true
Force = $Force
}

$uploadResult = Add-GoogleDriveFile @uploadParams
Expand All @@ -84,13 +89,20 @@ Function Publish-MarkdownDriveImages
Write-Warning "Failed to upload image $($image.FileName): $($_.Exception.Message)$([Environment]::NewLine)$($_.ErrorDetails | ConvertTo-Json -Depth 10)"
}
}
}

# Update the markdown file with new URLs, or ensure that OutFile is created if specified
if (($imageMappings -and $imageMappings.Count -gt 0) -or $OutFile) {
$updateParams = @{
File = $File
ImageMappings = $imageMappings
OutFile = $OutFile
}

# Update the markdown file with new URLs
if ($imageMappings -and $imageMappings.Count -gt 0) {
$updated = Update-MarkdownImages -File $File -ImageMappings $imageMappings
if ($updated) {
Write-Verbose "Updated markdown file with $($imageMappings.Count) new image URLs"
}
$updated = Update-MarkdownImages @updateParams
if ($updated) {
$targetFile = if ($OutFile) { $OutFile } else { $File }
Write-Verbose "Updated markdown file $targetFile with $($imageMappings.Count) new image URLs"
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/public/Update-MarkdownImages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Update-MarkdownImages {
[string]$File,

[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$ImageMappings,

[Parameter()]
Expand Down Expand Up @@ -71,11 +72,11 @@ function Update-MarkdownImages {
$TargetFile = $OutFile
}

# Only write the file if content has changed
if ($content -ne $originalContent) {
# Only write the file if content has changed or if OutFile is specified
if ($content -ne $originalContent -or $OutFile) {
Set-Content -Path $TargetFile -Value $content -NoNewline
Write-Verbose "Updated markdown file: $TargetFile"
return $true
return ($content -ne $originalContent)
}
else {
Write-Verbose "No changes made to markdown file: $File"
Expand Down
5 changes: 5 additions & 0 deletions src/tests/Find-MarkdownImages.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Describe "Find-MarkdownImages" {
New-TestImage "TestDrive:\test-image1.png"
New-TestImage "TestDrive:\subfolder\test-image2.jpg"
New-TestImage "TestDrive:\absolute-image.gif"

InModuleScope PSBlogger {
# reset blogger session to ensure that user preferences are not carried over into test
$BloggerSession.AttachmentsDirectory = $null
}
}

Context "Basic image detection" {
Expand Down
Loading