Skip to content

Commit 0fdc51e

Browse files
committed
wip
1 parent 245ddcc commit 0fdc51e

4 files changed

Lines changed: 76 additions & 17 deletions

File tree

src/public/Find-MarkdownImages.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
.PARAMETER AttachmentsDirectory
1515
The directory where attachments are stored. If specified, it will be used to resolve relative paths for images.
1616
17+
.PARAMETER ExcludeExternal
18+
If specified, excludes images with external URLs (starting with https). Otherwise, all images are returned.
19+
1720
.EXAMPLE
1821
Find-MarkdownImages -File "post.md"
1922
@@ -30,7 +33,10 @@ function Find-MarkdownImages {
3033
[string]$File,
3134

3235
[Parameter(Mandatory = $false)]
33-
[string]$AttachmentsDirectory
36+
[string]$AttachmentsDirectory,
37+
38+
[Parameter(Mandatory = $false)]
39+
[switch]$ExcludeExternal
3440
)
3541

3642
$content = Get-Content -Path $File -Raw
@@ -102,8 +108,8 @@ function Find-MarkdownImages {
102108
$title = "" # Obsidian format doesn't support titles
103109
}
104110

105-
# Skip URLs (images already hosted online)
106-
if ($imagePath -match '^https?://') {
111+
# Conditionally skip external URLs if -ExcludeExternal is specified
112+
if ($ExcludeExternal -and ($imagePath -match '^http(s)?://')) {
107113
continue
108114
}
109115

@@ -114,7 +120,7 @@ function Find-MarkdownImages {
114120
-AttachmentsDirectory $AttachmentsDirectory
115121

116122
# Check if the file exists
117-
if (Test-Path -Path $resolvedPath -PathType Leaf) {
123+
if ($resolvedPath -match '^http(s)?://' -or (Test-Path -Path $resolvedPath -PathType Leaf)) {
118124
$images += New-MarkdownImage `
119125
-OriginalMarkdown $match.Value `
120126
-AltText $altText `
@@ -164,6 +170,10 @@ Function Resolve-ImageFilePath
164170
# If the path is absolute, use it as is
165171
$resolvedPath = $FilePath
166172
}
173+
if ($FilePath -match '^http(s)?://') {
174+
Write-Verbose "Found external image URL: $FilePath"
175+
return $FilePath
176+
}
167177
if (Test-Path $resolvedPath) {
168178
Write-Verbose "Found image at base directory: $resolvedPath"
169179
return $resolvedPath

src/public/Publish-MarkdownDriveImages.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Function Publish-MarkdownDriveImages
4848

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

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

src/tests/ConvertTo-MarkdownFromHtml.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Describe "ConvertTo-MarkdownFromHtml" {
2626
# assert
2727
Test-Path $script:outFile | Should -BeTrue
2828
$content = (Get-Content -Path $script:outFile -Raw).Split("`r")
29-
$content[0] | Should -Be "# Hello World"
29+
$content[0] | Should -BeLike "# Hello World*"
3030
}
3131

3232
It "Should not persist to disk if OutFile is not specified" {
@@ -57,7 +57,7 @@ Describe "ConvertTo-MarkdownFromHtml" {
5757
# assert
5858
Test-Path $script:outFile | Should -BeTrue
5959
$content = (Get-Content -Path $script:outFile -Raw).Split("`r")
60-
$content[0] | Should -Be "# Hello World"
60+
$content[0] | Should -BeLike "# Hello World*"
6161
}
6262

6363
It "Should convert HTML file to Markdown" {

src/tests/Find-MarkdownImages.Tests.ps1

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,49 @@ Describe "Find-MarkdownImages" {
1515
}
1616

1717
Context "Basic image detection" {
18+
It "Should include external images by default" {
19+
# arrange
20+
$markdownFile = "TestDrive:\external.md"
21+
$markdownContent = @(
22+
"# Test Post"
23+
""
24+
"External image:"
25+
"![External](https://example.com/image.png)"
26+
) -join [Environment]::NewLine
27+
Set-MarkdownFile $markdownFile $markdownContent
28+
29+
# act
30+
$result = Find-MarkdownImages -File $markdownFile
31+
32+
# assert
33+
$result.Count | Should -Be 1
34+
$result[0].AltText | Should -Be "External"
35+
$result[0].RelativePath | Should -Be "https://example.com/image.png"
36+
$result[0].LocalPath | Should -Be "https://example.com/image.png"
37+
}
38+
39+
It "Should exclude external images when -ExcludeExternal is used" {
40+
# arrange
41+
$markdownFile = "TestDrive:\external-exclude.md"
42+
$markdownContent = @(
43+
"# Test Post"
44+
""
45+
"External image:"
46+
"![External](https://example.com/image.png)"
47+
"Local image:"
48+
"![Local](test-image1.png)"
49+
) -join [Environment]::NewLine
50+
Set-MarkdownFile $markdownFile $markdownContent
51+
52+
# act
53+
$result = Find-MarkdownImages -File $markdownFile -ExcludeExternal
54+
55+
# assert
56+
$result.Count | Should -Be 1
57+
$result[0].AltText | Should -Be "Local"
58+
$result[0].FileName | Should -Be "test-image1.png"
59+
$result[0].RelativePath | Should -Be "test-image1.png"
60+
}
1861

1962
It "Should return an empty array if there are no images" {
2063
# arrange
@@ -105,6 +148,12 @@ Describe "Find-MarkdownImages" {
105148
}
106149

107150
Context "Image Path resolution" {
151+
It "Should return the original value for external URLs in Resolve-ImageFilePath" {
152+
InModuleScope PSBlogger {
153+
$result = Resolve-ImageFilePath -FilePath "https://example.com/image.png" -BaseDirectory "TestDrive:/" -AttachmentsDirectory "TestDrive:/attachments"
154+
$result | Should -Be "https://example.com/image.png"
155+
}
156+
}
108157
It "Should resolve relative paths correctly" {
109158
# arrange
110159
$markdownFile = "TestDrive:\relative.md"
@@ -174,7 +223,7 @@ Describe "Find-MarkdownImages" {
174223

175224
It "Should resolve absolute image in the attachments directory" {
176225
# arrange
177-
$markdownFile = "TestDrive:\attachments.md"
226+
$markdownFile = Join-Path "TestDrive" "attachments.md"
178227
$markdownContent = @(
179228
""
180229
"![Image in attachments](test-attachment1.png)"
@@ -187,12 +236,12 @@ Describe "Find-MarkdownImages" {
187236
# assert
188237
$result.Count | Should -Be 1
189238
$result[0].FileName | Should -Be "test-attachment1.png"
190-
$result[0].LocalPath | Should -BeLike "*attachments\test-attachment1.png"
239+
$result[0].LocalPath | Should -BeLike "*attachments*test-attachment1.png"
191240
}
192241

193242
It "Should resolve absolute image with subfolder relative to the attachments directory" {
194243
# arrange
195-
$markdownFile = "TestDrive:\attachments-subfolder.md"
244+
$markdownFile = Join-Path "TestDrive" "attachments-subfolder.md"
196245
$markdownContent = @(
197246
""
198247
"![Image in subfolder](subfolder/test-attachment2.jpg)"
@@ -205,12 +254,12 @@ Describe "Find-MarkdownImages" {
205254
# assert
206255
$result.Count | Should -Be 1
207256
$result[0].FileName | Should -Be "test-attachment2.jpg"
208-
$result[0].LocalPath | Should -BeLike "*attachments\subfolder\test-attachment2.jpg"
257+
$result[0].LocalPath | Should -BeLike "*attachments*subfolder*test-attachment2.jpg"
209258
}
210259

211260
It "Should find images in subfolders of the attachments directory when markdown does not specify a subfolder" {
212261
# arrange
213-
$markdownFile = "TestDrive:\attachments-subfolder.md"
262+
$markdownFile = Join-Path "TestDrive" "attachments-subfolder.md"
214263
$markdownContent = @(
215264
""
216265
"![Image in subfolder](test-attachment2.jpg)" # this is in the subfolder
@@ -223,7 +272,7 @@ Describe "Find-MarkdownImages" {
223272
# assert
224273
$result.Count | Should -Be 1
225274
$result[0].FileName | Should -Be "test-attachment2.jpg"
226-
$result[0].LocalPath | Should -BeLike "*attachments\subfolder\test-attachment2.jpg"
275+
$result[0].LocalPath | Should -BeLike "*attachments*subfolder*test-attachment2.jpg"
227276
}
228277

229278
It "Should use folder of file if attachments directory is not specified" {
@@ -241,7 +290,7 @@ Describe "Find-MarkdownImages" {
241290
# assert
242291
$result.Count | Should -Be 1
243292
$result[0].FileName | Should -Be "test-attachment1.png"
244-
$result[0].LocalPath | Should -BeLike "*attachments\test-attachment1.png"
293+
$result[0].LocalPath | Should -BeLike "*attachments*test-attachment1.png"
245294
}
246295

247296
It "Should use attachments directory user preference if available" {
@@ -265,7 +314,7 @@ Describe "Find-MarkdownImages" {
265314
# assert
266315
$result.Count | Should -Be 1
267316
$result[0].FileName | Should -Be "test-attachment1.png"
268-
$result[0].LocalPath | Should -BeLike "*attachments\test-attachment1.png"
317+
$result[0].LocalPath | Should -BeLike "*attachments*test-attachment1.png"
269318
}
270319
}
271320

@@ -282,7 +331,7 @@ Describe "Find-MarkdownImages" {
282331
Set-MarkdownFile $markdownFile $markdownContent
283332

284333
# act
285-
$result = Find-MarkdownImages -File $markdownFile
334+
$result = Find-MarkdownImages -File $markdownFile -ExcludeExternal
286335

287336
# assert
288337
$result.Count | Should -Be 1
@@ -511,7 +560,7 @@ Describe "Find-MarkdownImages" {
511560
Set-MarkdownFile $markdownFile $markdownContent
512561

513562
# act
514-
$result = Find-MarkdownImages -File $markdownFile
563+
$result = Find-MarkdownImages -File $markdownFile -ExcludeExternal
515564

516565
# assert
517566
$result.Count | Should -Be 1

0 commit comments

Comments
 (0)