Skip to content

Commit 6340c60

Browse files
authored
Introduced Publish-MarkdownDriveImages (#14)
* introduced Publish-MarkdownDriveImages * additional clarity for the tests
1 parent 7ccd4dd commit 6340c60

3 files changed

Lines changed: 609 additions & 36 deletions

File tree

src/public/Publish-MarkdownBloggerPost.ps1

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
<#
2+
.SYNOPSIS
3+
Publishes a markdown file as a blog post to Blogger, including uploading any local images to Google Drive.
24
5+
.DESCRIPTION
6+
This function processes a markdown file to publish it as a blog post. It handles:
7+
- Extracting front matter from the markdown file
8+
- Finding and uploading local images to Google Drive
9+
- Converting markdown content to HTML
10+
- Publishing the post to Blogger
11+
- Updating the front matter with post information
12+
13+
.PARAMETER File
14+
The path to the markdown file to publish.
15+
16+
.PARAMETER BlogId
17+
The ID of the blog to publish to. If not specified, uses the BlogId from the current BloggerSession.
18+
19+
.PARAMETER Draft
20+
If specified, publishes the post as a draft rather than a published post.
21+
22+
.PARAMETER Force
23+
If specified, will overwrite existing images in Google Drive with the same name.
24+
25+
.EXAMPLE
26+
Publish-MarkdownBloggerPost -File "my-post.md"
27+
28+
.EXAMPLE
29+
Publish-MarkdownBloggerPost -File "my-post.md" -Draft -Force
330
#>
431
Function Publish-MarkdownBloggerPost
532
{
@@ -13,7 +40,10 @@ Function Publish-MarkdownBloggerPost
1340
[int]$BlogId,
1441

1542
[Parameter(Mandatory=$false)]
16-
[switch]$Draft
43+
[switch]$Draft,
44+
45+
[Parameter(Mandatory=$false)]
46+
[switch]$Force
1747
)
1848

1949
if (!$PSBoundParameters.ContainsKey("BlogId"))
@@ -28,41 +58,7 @@ Function Publish-MarkdownBloggerPost
2858
$postInfo = Get-MarkdownFrontMatter -File $File
2959

3060
# Process images: detect, upload to Google Drive, and update markdown
31-
$imageMappings = @()
32-
$images = Find-MarkdownImages -File $File
33-
if ($images -and $images.Count -gt 0) {
34-
Write-Verbose "Found $($images.Count) local images to upload to Google Drive"
35-
36-
$anonymous = New-GoogleDriveFilePermission -role "reader" -type "anyone"
37-
38-
foreach ($image in $images) {
39-
try {
40-
Write-Verbose "Uploading image: $($image.FileName)"
41-
$uploadResult = Add-GoogleDriveFile -FilePath $image.LocalPath -FileName $image.FileName
42-
if (!$uploadResult) {
43-
Write-Warning "Failed to upload image $($image.FileName)"
44-
continue
45-
}
46-
Set-GoogleDriveFilePermission -FileId $uploadResult.id -Permission $anonymous | Out-Null
47-
48-
$image.NewUrl = $uploadResult.PublicUrl
49-
$imageMappings += $image
50-
51-
Write-Verbose "Successfully uploaded: $($image.FileName) -> $($uploadResult.PublicUrl)"
52-
}
53-
catch {
54-
Write-Warning "Failed to upload image $($image.FileName): $($_.Exception.Message)$([Environment]::NewLine)$($_.ErrorDetails | ConvertTo-Json -Depth 10)"
55-
}
56-
}
57-
58-
# Update the markdown file with new URLs
59-
if ($imageMappings -and $imageMappings.Count -gt 0) {
60-
$updated = Update-MarkdownImages -File $File -ImageMappings $imageMappings
61-
if ($updated) {
62-
Write-Verbose "Updated markdown file with $($imageMappings.Count) new image URLs"
63-
}
64-
}
65-
}
61+
$imageMappings = Publish-MarkdownDriveImages -File $File -Force:$Force
6662

6763
# convert from markdown to html file
6864
$content = ConvertTo-HtmlFromMarkdown -File $File
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<#
2+
.SYNOPSIS
3+
Publishes local images from a markdown file to Google Drive and updates the markdown file with the new URLs.
4+
5+
.DESCRIPTION
6+
This function finds all local images referenced in a markdown file, uploads them to Google Drive,
7+
sets public permissions, and updates the markdown file with the new Google Drive URLs.
8+
9+
.PARAMETER File
10+
The path to the markdown file containing image references.
11+
12+
.PARAMETER Force
13+
If specified, will overwrite existing files in Google Drive with the same name.
14+
15+
.EXAMPLE
16+
Publish-MarkdownDriveImages -File "blog-post.md"
17+
18+
.EXAMPLE
19+
Publish-MarkdownDriveImages -File "blog-post.md" -Force
20+
#>
21+
Function Publish-MarkdownDriveImages
22+
{
23+
[CmdletBinding()]
24+
Param(
25+
[Parameter(Mandatory=$true)]
26+
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
27+
[string]$File,
28+
29+
[Parameter(Mandatory=$false)]
30+
[switch]$Force
31+
)
32+
33+
# Process images: detect, upload to Google Drive, and update markdown
34+
$imageMappings = @()
35+
$images = Find-MarkdownImages -File $File
36+
37+
if ($images -and $images.Count -gt 0) {
38+
Write-Verbose "Found $($images.Count) local images to upload to Google Drive"
39+
40+
$anonymous = New-GoogleDriveFilePermission -role "reader" -type "anyone"
41+
42+
foreach ($image in $images) {
43+
try {
44+
Write-Verbose "Uploading image: $($image.FileName)"
45+
46+
# Use the Force parameter when calling Add-GoogleDriveFile
47+
$uploadParams = @{
48+
FilePath = $image.LocalPath
49+
FileName = $image.FileName
50+
Force = $false
51+
}
52+
53+
if ($Force) {
54+
$uploadParams.Force = $true
55+
}
56+
57+
$uploadResult = Add-GoogleDriveFile @uploadParams
58+
59+
if (!$uploadResult) {
60+
Write-Warning "Failed to upload image $($image.FileName)"
61+
continue
62+
}
63+
64+
try {
65+
Set-GoogleDriveFilePermission -FileId $uploadResult.id -Permission $anonymous | Out-Null
66+
}
67+
catch {
68+
Write-Warning "Failed to set public permission for image $($image.FileName): $($_.Exception.Message)$([Environment]::NewLine)$($_.ErrorDetails | ConvertTo-Json -Depth 10)"
69+
}
70+
71+
$image.NewUrl = $uploadResult.PublicUrl
72+
$imageMappings += $image
73+
74+
Write-Verbose "Successfully uploaded: $($image.FileName) -> $($uploadResult.PublicUrl)"
75+
}
76+
catch {
77+
Write-Warning "Failed to upload image $($image.FileName): $($_.Exception.Message)$([Environment]::NewLine)$($_.ErrorDetails | ConvertTo-Json -Depth 10)"
78+
}
79+
}
80+
81+
# Update the markdown file with new URLs
82+
if ($imageMappings -and $imageMappings.Count -gt 0) {
83+
$updated = Update-MarkdownImages -File $File -ImageMappings $imageMappings
84+
if ($updated) {
85+
Write-Verbose "Updated markdown file with $($imageMappings.Count) new image URLs"
86+
}
87+
}
88+
}
89+
90+
return $imageMappings
91+
}

0 commit comments

Comments
 (0)