summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src/remark-content-rel-image-error.ts
blob: 3e3664b204389db47daed73423cfb75ec82105d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { Image } from 'mdast';
import { visit } from 'unist-util-visit';
import { pathToFileURL } from 'url';
import type { VFile } from 'vfile';

/**
 * `src/content/` does not support relative image paths.
 * This plugin throws an error if any are found
 */
export default function toRemarkContentRelImageError({ contentDir }: { contentDir: URL }) {
	return function remarkContentRelImageError() {
		return (tree: any, vfile: VFile) => {
			if (typeof vfile?.path !== 'string') return;

			const isContentFile = pathToFileURL(vfile.path).href.startsWith(contentDir.href);
			if (!isContentFile) return;

			const relImagePaths = new Set<string>();
			visit(tree, 'image', function raiseError(node: Image) {
				if (isRelativePath(node.url)) {
					relImagePaths.add(node.url);
				}
			});
			if (relImagePaths.size === 0) return;

			const errorMessage =
				`Relative image paths are not supported in the content/ directory. Place local images in the public/ directory and use absolute paths (see https://docs.astro.build/en/guides/images/#in-markdown-files)\n` +
				[...relImagePaths].map((path) => JSON.stringify(path)).join(',\n');

			// Throw raw string to use `astro:markdown` default formatting
			throw errorMessage;
		};
	};
}

// Following utils taken from `packages/astro/src/core/path.ts`:

function isRelativePath(path: string) {
	return startsWithDotDotSlash(path) || startsWithDotSlash(path);
}

function startsWithDotDotSlash(path: string) {
	const c1 = path[0];
	const c2 = path[1];
	const c3 = path[2];
	return c1 === '.' && c2 === '.' && c3 === '/';
}

function startsWithDotSlash(path: string) {
	const c1 = path[0];
	const c2 = path[1];
	return c1 === '.' && c2 === '/';
}