Skip to content

Commit c9521f3

Browse files
authored
Add -ExcludeExternal to Find-MarkdownImages (#52)
* wip * minor refactorings to support platform agnostic paths
1 parent 245ddcc commit c9521f3

5 files changed

Lines changed: 103 additions & 44 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" {

0 commit comments

Comments
 (0)