Skip to content

Commit 2f8eb96

Browse files
authored
Add support for -AttachmentsDirectory when publishing images (#39)
* cleaned-up Find-MarkdownImages tests * Added -AttachmentsDirectory to Find-MarkdownImages * Expanded -AttachmentsDirectory to Publish-MarkdownBloggerPost and Publish-MarkdownDriveImages * Added AttachmentsDirectory configuration setting * Find-MarkdownImages uses $BloggerSession.AttachmentsDirectory * Validation logic for BloggerSession values to ensure AttachmentsDirectory is a valid path and stored as non-relative value
1 parent 5c55d11 commit 2f8eb96

11 files changed

Lines changed: 543 additions & 160 deletions

src/private/Get-BloggerSession.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Function Get-BloggerSession
1212
PandocAdditionalArgs = "--html-q-tags --ascii"
1313
BlogId = $null
1414
ExcludeLabels = @()
15+
AttachmentsDirectory = $null
1516
}
1617

1718
if (Test-Path $session.UserPreferences)

src/public/Find-MarkdownImages.ps1

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
<#
22
.SYNOPSIS
3-
Finds all image references in a markdown file.
3+
Finds all image references in a markdown file.
44
55
.DESCRIPTION
6-
Parses a markdown file and extracts all image references including:
7-
- Standard markdown format: ![alt text](image_path "optional title")
8-
- Obsidian format: ![[image_path|alt text]]
9-
Returns information about each image including the original markdown syntax, image path, alt text, and title.
6+
Parses a markdown file and extracts all image references including:
7+
- Standard markdown format: ![alt text](image_path "optional title")
8+
- Obsidian format: ![[image_path|alt text]]
9+
Returns information about each image including the original markdown syntax, image path, alt text, and title.
1010
1111
.PARAMETER File
12-
The path to the markdown file to analyze.
12+
The path to the markdown file to analyze.
13+
14+
.PARAMETER AttachmentsDirectory
15+
The directory where attachments are stored. If specified, it will be used to resolve relative paths for images.
1316
1417
.EXAMPLE
1518
Find-MarkdownImages -File "post.md"
19+
20+
.EXAMPLE
21+
Find-MarkdownImages -File "post.md" -AttachmentsDirectory "C:\Attachments"
22+
23+
This command will find all images in the specified markdown file and resolve their paths against the provided attachments directory.
1624
#>
1725
function Find-MarkdownImages {
1826
[CmdletBinding()]
1927
param(
2028
[Parameter(Mandatory = $true)]
2129
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
22-
[string]$File
30+
[string]$File,
31+
32+
[Parameter(Mandatory = $false)]
33+
[string]$AttachmentsDirectory
2334
)
2435

2536
$content = Get-Content -Path $File -Raw
@@ -31,6 +42,17 @@ function Find-MarkdownImages {
3142
$fileDirectory = "."
3243
}
3344

45+
if ([string]::IsNullOrEmpty($AttachmentsDirectory)) {
46+
if ($BloggerSession.AttachmentsDirectory) {
47+
# Use the Blogger session's attachments directory if available
48+
Write-Verbose "Using PSBlogger config for attachments directory: $($BloggerSession.AttachmentsDirectory)"
49+
$AttachmentsDirectory = $BloggerSession.AttachmentsDirectory
50+
} else {
51+
# Default to the file's directory if no attachments directory is specified
52+
$AttachmentsDirectory = $fileDirectory
53+
}
54+
}
55+
3456
# Regex pattern for standard markdown images: ![alt text](image_path "optional title")
3557
$standardPattern = '!\[([^\]]*)\]\(([^)]+?)(?:\s+"([^"]*)")?\)'
3658

@@ -85,14 +107,12 @@ function Find-MarkdownImages {
85107
continue
86108
}
87109

88-
# Resolve relative paths
89-
if (-not [System.IO.Path]::IsPathRooted($imagePath)) {
90-
$resolvedPath = Join-Path -Path $fileDirectory -ChildPath $imagePath
91-
}
92-
else {
93-
$resolvedPath = $imagePath
94-
}
95-
110+
# Resolve the image file path
111+
$resolvedPath = Resolve-ImageFilePath `
112+
-FilePath $imagePath `
113+
-BaseDirectory $fileDirectory `
114+
-AttachmentsDirectory $AttachmentsDirectory
115+
96116
# Check if the file exists
97117
if (Test-Path -Path $resolvedPath -PathType Leaf) {
98118
$images += New-MarkdownImage `
@@ -126,4 +146,46 @@ Function New-MarkdownImage {
126146
FileName = $FileName
127147
NewUrl = $null # This will be set after uploading to Google Drive
128148
}
149+
}
150+
151+
Function Resolve-ImageFilePath
152+
{
153+
param(
154+
[string]$FilePath,
155+
[string]$BaseDirectory,
156+
[string]$AttachmentsDirectory
157+
)
158+
159+
# Test if the file path is relative to the base directory
160+
if (-not [System.IO.Path]::IsPathRooted($FilePath)) {
161+
# If the path is relative, resolve it against the base directory
162+
$resolvedPath = Join-Path -Path $BaseDirectory -ChildPath $FilePath
163+
} else {
164+
# If the path is absolute, use it as is
165+
$resolvedPath = $FilePath
166+
}
167+
if (Test-Path $resolvedPath) {
168+
Write-Verbose "Found image at base directory: $resolvedPath"
169+
return $resolvedPath
170+
}
171+
172+
#Test if the file path is relative to the attachments directory
173+
$resolvedPath = Join-Path -Path $AttachmentsDirectory -ChildPath $FilePath
174+
if (Test-Path $resolvedPath) {
175+
Write-Verbose "Found image at attachments directory: $resolvedPath"
176+
return $resolvedPath
177+
}
178+
179+
# # If not found, recursively search the attachments directory
180+
$files = @(Get-ChildItem -Path $AttachmentsDirectory -Recurse -File -Filter $FilePath)
181+
if ($files.Count -gt 0) {
182+
if ($files.Count -gt 1) {
183+
Write-Warning "Multiple files found matching '$FilePath' in '$AttachmentsDirectory'. Returning the first match."
184+
}
185+
Write-Verbose "Found image in attachments directory: $($files[0].FullName)"
186+
return $files[0].FullName
187+
}
188+
189+
# If still not found, return the original file path
190+
return $FilePath
129191
}

src/public/Get-BloggerConfig.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ Function Get-BloggerConfig
1212
PandocAdditionalArgs = $BloggerSession.PandocAdditionalArgs
1313
BlogId = $BloggerSession.BlogId
1414
ExcludeLabels = $BloggerSession.ExcludeLabels
15+
AttachmentsDirectory = $BloggerSession.AttachmentsDirectory
1516
}
1617
}

src/public/Publish-MarkdownBloggerPost.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Function Publish-MarkdownBloggerPost
6262
[Parameter(Mandatory=$false)]
6363
[array]$ExcludeLabels = @(),
6464

65+
[Parameter(Mandatory=$false)]
66+
[string]$AttachmentsDirectory,
67+
6568
[Parameter(Mandatory=$false)]
6669
[switch]$Force,
6770

@@ -86,7 +89,7 @@ Function Publish-MarkdownBloggerPost
8689
$postInfo = Get-MarkdownFrontMatter -File $File
8790

8891
# Process images: detect, upload to Google Drive, and update markdown
89-
$imageMappings = Publish-MarkdownDriveImages -File $File -Force:$Force
92+
$imageMappings = Publish-MarkdownDriveImages -File $File -AttachmentsDirectory $AttachmentsDirectory -Force:$Force
9093

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

src/public/Publish-MarkdownDriveImages.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
.PARAMETER File
1010
The path to the markdown file containing image references.
1111
12+
.PARAMETER AttachmentsDirectory
13+
Optional. The directory where images are stored. If not specified, the function will look for
14+
images in the same directory as the markdown file.
15+
1216
.PARAMETER Force
1317
If specified, will overwrite existing files in Google Drive with the same name.
1418
@@ -26,13 +30,16 @@ Function Publish-MarkdownDriveImages
2630
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
2731
[string]$File,
2832

33+
[Parameter(Mandatory=$false)]
34+
[string]$AttachmentsDirectory,
35+
2936
[Parameter(Mandatory=$false)]
3037
[switch]$Force
3138
)
3239

3340
# Process images: detect, upload to Google Drive, and update markdown
3441
$imageMappings = @()
35-
$images = Find-MarkdownImages -File $File
42+
$images = Find-MarkdownImages -File $File -AttachmentsDirectory $AttachmentsDirectory
3643

3744
if ($images -and $images.Count -gt 0) {
3845
Write-Verbose "Found $($images.Count) local images to upload to Google Drive"

src/public/Set-BloggerConfig.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- PandocHtmlFormat: The HTML format to use when converting Markdown to HTML.
1313
- PandocMarkdownFormat: The Markdown format to use when converting HTML to Markdown.
1414
- ExcludeLabels: Labels to exclude when publishing to Blogger.
15+
- AttachmentsDirectory: Folder path to attachments
1516
1617
.PARAMETER Value
1718
The value to set for the specified user preference. Specify an empty string to remove the preference.
@@ -21,7 +22,7 @@ Function Set-BloggerConfig
2122
[CmdletBinding()]
2223
Param(
2324
[Parameter(Mandatory=$true)]
24-
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels")]
25+
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels","AttachmentsDirectory")]
2526
[string]$Name,
2627

2728
[Parameter(Mandatory=$true)]
@@ -37,6 +38,17 @@ Function Set-BloggerConfig
3738
$userPreferences | Out-String | Write-Verbose
3839
}
3940

41+
if ($Value) {
42+
switch ($Name) {
43+
"AttachmentsDirectory" {
44+
if (-not (Test-Path -Path $Value -PathType Container)) {
45+
throw "AttachmentsDirectory must be a valid directory path."
46+
}
47+
$Value = (Resolve-Path -Path $Value).Path
48+
}
49+
}
50+
}
51+
4052
if (@($userPreferences.PsObject.Properties).Count -eq 0 -or $Name -notin $userPreferences.PsObject.Properties.Name)
4153
{
4254
Write-Verbose "Set-BloggerConfig: Adding Property $Name"

0 commit comments

Comments
 (0)