-
-
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.
[Content] Throw on relative image usage (#5648)
* chore: add rel image error plugin * deps: mdast, mdast types * chore: add rel image throw to mdx * refactor: doc rel image path plugin * fix: respect experimental flag in md remark * chore: changeset * deps: remove mdast package * fix: resolve contentDir from config * fix: apply MDX plugin after user plugins * fix: stub out contentDir
- Loading branch information
1 parent
b64081d
commit 853081d
Showing
15 changed files
with
151 additions
and
2 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,7 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/mdx': patch | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Prevent relative image paths in `src/content/` |
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
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
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
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
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
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
52 changes: 52 additions & 0 deletions
52
packages/markdown/remark/src/remark-content-rel-image-error.ts
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,52 @@ | ||
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) => { | ||
const isContentFile = pathToFileURL(vfile.path).href.startsWith(contentDir.href); | ||
if (!isContentFile) return; | ||
|
||
const relImagePaths = new Set<string>(); | ||
visit(tree, 'image', function raiseError(node: Image) { | ||
console.log(node.url); | ||
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 === '/'; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.