-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(images): Simpler logic for collecting images in Markdown (#6744)
- Loading branch information
1 parent
366decb
commit a1a4f45
Showing
7 changed files
with
54 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Fix remote images in Markdown throwing errors when using `experimental.assets` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/astro/test/fixtures/core-image/src/pages/httpImage.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
![Remote image](https://example.com/image.png) | ||
![/public image](/image.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import type { Image } from 'mdast'; | ||
import { visit } from 'unist-util-visit'; | ||
import type { VFile } from 'vfile'; | ||
import type { MarkdownVFile } from './types'; | ||
|
||
export default function toRemarkCollectImages() { | ||
return () => | ||
async function (tree: any, vfile: VFile) { | ||
async function (tree: any, vfile: MarkdownVFile) { | ||
if (typeof vfile?.path !== 'string') return; | ||
|
||
const imagePaths = new Set<string>(); | ||
visit(tree, 'image', function raiseError(node: Image) { | ||
imagePaths.add(node.url); | ||
visit(tree, 'image', (node: Image) => { | ||
if (shouldOptimizeImage(node.url)) imagePaths.add(node.url); | ||
}); | ||
|
||
vfile.data.imagePaths = Array.from(imagePaths); | ||
vfile.data.imagePaths = imagePaths; | ||
}; | ||
} | ||
|
||
function shouldOptimizeImage(src: string) { | ||
// Optimize anything that is NOT external or an absolute path to `public/` | ||
return !isValidUrl(src) && !src.startsWith('/'); | ||
} | ||
|
||
function isValidUrl(str: string): boolean { | ||
try { | ||
new URL(str); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters