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: 
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: 
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#>
1725function 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: 
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}
0 commit comments