-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublish-MarkdownBloggerPost.ps1
More file actions
133 lines (103 loc) · 3.54 KB
/
Publish-MarkdownBloggerPost.ps1
File metadata and controls
133 lines (103 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<#
.SYNOPSIS
Publishes a markdown file as a blog post to Blogger, including uploading any local images to Google Drive.
.DESCRIPTION
This function processes a markdown file to publish it as a blog post. It handles:
- Extracting front matter from the markdown file
- Finding and uploading local images to Google Drive
- Converting markdown content to HTML
- Publishing the post to Blogger
- Updating the front matter with post information
.PARAMETER File
The path to the markdown file to publish.
.PARAMETER BlogId
The ID of the blog to publish to. If not specified, uses the BlogId from the current BloggerSession.
.PARAMETER Draft
If specified, publishes the post as a draft rather than a published post.
.PARAMETER Force
If specified, will overwrite existing images in Google Drive with the same name.
.PARAMETER Open
If specified, launches a browser to view the post after publishing.
.EXAMPLE
# publish or update post
Publish-MarkdownBloggerPost -File "my-post.md"
.EXAMPLE
# publish or update a draft post
Publish-MarkdownBloggerPost -File "my-post.md" -Draft
.EXAMPLE
# publish or update a post, updating google drive images if required
Publish-MarkdownBloggerPost -File "my-post.md" -Force
.EXAMPLE
# publish or update a post, launching a web browser to view the published post
Publish-MarkdownBloggerPost -File "my-post.md" -Open
.EXAMPLE
# publish or update a draft, launching a web browser with the page preview
Publish-MarkdownBloggerPost -File "my-post.md" -Draft -Open
#>
Function Publish-MarkdownBloggerPost
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]$File,
[Parameter(Mandatory=$false)]
[int]$BlogId,
[Parameter(Mandatory=$false)]
[switch]$Draft,
[Parameter(Mandatory=$false)]
[array]$ExcludeLabels = @(),
[Parameter(Mandatory=$false)]
[switch]$Force,
[Parameter(Mandatory=$false)]
[switch]$Open
)
if (!$PSBoundParameters.ContainsKey("BlogId"))
{
$BlogId = $BloggerSession.BlogId
if (0 -eq $BlogId) {
throw "BlogId not specified."
}
}
if (!$PSBoundParameters.ContainsKey("ExcludeLabels")) {
$ExcludeLabels = $BloggerSession.ExcludeLabels
}
# grab the front matter
$postInfo = Get-MarkdownFrontMatter -File $File
# Process images: detect, upload to Google Drive, and update markdown
$imageMappings = Publish-MarkdownDriveImages -File $File -Force:$Force
# 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
}
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")
}
}
Write-Verbose "Updating front matter with post id: $($postInfo['postId'])"
Set-MarkdownFrontMatter -File $File -Replace $postInfo
return $post
}