0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/markdown/remark/src/remark-collect-images.ts
2023-04-13 09:56:50 +00:00

30 lines
751 B
TypeScript

import type { Image } from 'mdast';
import { visit } from 'unist-util-visit';
import type { MarkdownVFile } from './types';
export function remarkCollectImages() {
return function (tree: any, vfile: MarkdownVFile) {
if (typeof vfile?.path !== 'string') return;
const imagePaths = new Set<string>();
visit(tree, 'image', (node: Image) => {
if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
});
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;
}
}