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
1 change: 1 addition & 0 deletions src/private/Get-BloggerSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Function Get-BloggerSession
PandocAdditionalArgs = "--html-q-tags --ascii"
BlogId = $null
ExcludeLabels = @()
AttachmentsDirectory = $null
}

if (Test-Path $session.UserPreferences)
Expand Down
92 changes: 77 additions & 15 deletions src/public/Find-MarkdownImages.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
<#
.SYNOPSIS
Finds all image references in a markdown file.
Finds all image references in a markdown file.

.DESCRIPTION
Parses a markdown file and extracts all image references including:
- Standard markdown format: ![alt text](image_path "optional title")
- Obsidian format: ![[image_path|alt text]]
Returns information about each image including the original markdown syntax, image path, alt text, and title.
Parses a markdown file and extracts all image references including:
- Standard markdown format: ![alt text](image_path "optional title")
- Obsidian format: ![[image_path|alt text]]
Returns information about each image including the original markdown syntax, image path, alt text, and title.

.PARAMETER File
The path to the markdown file to analyze.
The path to the markdown file to analyze.

.PARAMETER AttachmentsDirectory
The directory where attachments are stored. If specified, it will be used to resolve relative paths for images.

.EXAMPLE
Find-MarkdownImages -File "post.md"

.EXAMPLE
Find-MarkdownImages -File "post.md" -AttachmentsDirectory "C:\Attachments"

This command will find all images in the specified markdown file and resolve their paths against the provided attachments directory.
#>
function Find-MarkdownImages {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]$File
[string]$File,

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

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

if ([string]::IsNullOrEmpty($AttachmentsDirectory)) {
if ($BloggerSession.AttachmentsDirectory) {
# Use the Blogger session's attachments directory if available
Write-Verbose "Using PSBlogger config for attachments directory: $($BloggerSession.AttachmentsDirectory)"
$AttachmentsDirectory = $BloggerSession.AttachmentsDirectory
} else {
# Default to the file's directory if no attachments directory is specified
$AttachmentsDirectory = $fileDirectory
}
}

# Regex pattern for standard markdown images: ![alt text](image_path "optional title")
$standardPattern = '!\[([^\]]*)\]\(([^)]+?)(?:\s+"([^"]*)")?\)'

Expand Down Expand Up @@ -85,14 +107,12 @@ function Find-MarkdownImages {
continue
}

# Resolve relative paths
if (-not [System.IO.Path]::IsPathRooted($imagePath)) {
$resolvedPath = Join-Path -Path $fileDirectory -ChildPath $imagePath
}
else {
$resolvedPath = $imagePath
}

# Resolve the image file path
$resolvedPath = Resolve-ImageFilePath `
-FilePath $imagePath `
-BaseDirectory $fileDirectory `
-AttachmentsDirectory $AttachmentsDirectory

# Check if the file exists
if (Test-Path -Path $resolvedPath -PathType Leaf) {
$images += New-MarkdownImage `
Expand Down Expand Up @@ -126,4 +146,46 @@ Function New-MarkdownImage {
FileName = $FileName
NewUrl = $null # This will be set after uploading to Google Drive
}
}

Function Resolve-ImageFilePath
{
param(
[string]$FilePath,
[string]$BaseDirectory,
[string]$AttachmentsDirectory
)

# Test if the file path is relative to the base directory
if (-not [System.IO.Path]::IsPathRooted($FilePath)) {
# If the path is relative, resolve it against the base directory
$resolvedPath = Join-Path -Path $BaseDirectory -ChildPath $FilePath
} else {
# If the path is absolute, use it as is
$resolvedPath = $FilePath
}
if (Test-Path $resolvedPath) {
Write-Verbose "Found image at base directory: $resolvedPath"
return $resolvedPath
}

#Test if the file path is relative to the attachments directory
$resolvedPath = Join-Path -Path $AttachmentsDirectory -ChildPath $FilePath
if (Test-Path $resolvedPath) {
Write-Verbose "Found image at attachments directory: $resolvedPath"
return $resolvedPath
}

# # If not found, recursively search the attachments directory
$files = @(Get-ChildItem -Path $AttachmentsDirectory -Recurse -File -Filter $FilePath)
if ($files.Count -gt 0) {
if ($files.Count -gt 1) {
Write-Warning "Multiple files found matching '$FilePath' in '$AttachmentsDirectory'. Returning the first match."
}
Write-Verbose "Found image in attachments directory: $($files[0].FullName)"
return $files[0].FullName
}

# If still not found, return the original file path
return $FilePath
}
1 change: 1 addition & 0 deletions src/public/Get-BloggerConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Function Get-BloggerConfig
PandocAdditionalArgs = $BloggerSession.PandocAdditionalArgs
BlogId = $BloggerSession.BlogId
ExcludeLabels = $BloggerSession.ExcludeLabels
AttachmentsDirectory = $BloggerSession.AttachmentsDirectory
}
}
5 changes: 4 additions & 1 deletion src/public/Publish-MarkdownBloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Function Publish-MarkdownBloggerPost
[Parameter(Mandatory=$false)]
[array]$ExcludeLabels = @(),

[Parameter(Mandatory=$false)]
[string]$AttachmentsDirectory,

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

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

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

# convert from markdown to html file
$content = ConvertTo-HtmlFromMarkdown -File $File
Expand Down
9 changes: 8 additions & 1 deletion src/public/Publish-MarkdownDriveImages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
.PARAMETER File
The path to the markdown file containing image references.

.PARAMETER AttachmentsDirectory
Optional. The directory where images are stored. If not specified, the function will look for
images in the same directory as the markdown file.

.PARAMETER Force
If specified, will overwrite existing files in Google Drive with the same name.

Expand All @@ -26,13 +30,16 @@ Function Publish-MarkdownDriveImages
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]$File,

[Parameter(Mandatory=$false)]
[string]$AttachmentsDirectory,

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

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

if ($images -and $images.Count -gt 0) {
Write-Verbose "Found $($images.Count) local images to upload to Google Drive"
Expand Down
14 changes: 13 additions & 1 deletion src/public/Set-BloggerConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- PandocHtmlFormat: The HTML format to use when converting Markdown to HTML.
- PandocMarkdownFormat: The Markdown format to use when converting HTML to Markdown.
- ExcludeLabels: Labels to exclude when publishing to Blogger.
- AttachmentsDirectory: Folder path to attachments

.PARAMETER Value
The value to set for the specified user preference. Specify an empty string to remove the preference.
Expand All @@ -21,7 +22,7 @@ Function Set-BloggerConfig
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels")]
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat","ExcludeLabels","AttachmentsDirectory")]
[string]$Name,

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

if ($Value) {
switch ($Name) {
"AttachmentsDirectory" {
if (-not (Test-Path -Path $Value -PathType Container)) {
throw "AttachmentsDirectory must be a valid directory path."
}
$Value = (Resolve-Path -Path $Value).Path
}
}
}

if (@($userPreferences.PsObject.Properties).Count -eq 0 -or $Name -notin $userPreferences.PsObject.Properties.Name)
{
Write-Verbose "Set-BloggerConfig: Adding Property $Name"
Expand Down
Loading