-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(markdown): Move
astro:assets
-specific code out of the main…
… Vite plugin
- Loading branch information
1 parent
c3572fd
commit ec952b4
Showing
2 changed files
with
41 additions
and
25 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,34 @@ | ||
export type MarkdownImagePath = { raw: string; resolved: string; safeName: string }; | ||
|
||
export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: string) { | ||
return ` | ||
import { getImage } from "astro:assets"; | ||
${imagePaths | ||
.map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`) | ||
.join('\n')} | ||
const images = async function() { | ||
return { | ||
${imagePaths | ||
.map((entry) => `"${entry.raw}": await getImage({src: Astro__${entry.safeName}})`) | ||
.join(',\n')} | ||
} | ||
} | ||
async function updateImageReferences(html) { | ||
return images().then((images) => { | ||
return html.replaceAll(/__ASTRO_IMAGE_="([^"]+)"/gm, (full, imagePath) => | ||
spreadAttributes({ | ||
src: images[imagePath].src, | ||
...images[imagePath].attributes, | ||
}) | ||
); | ||
}); | ||
} | ||
// NOTE: This causes a top-level await to appear in the user's code, which can break very easily due to a Rollup | ||
// bug and certain adapters not supporting it correctly. See: https://github.com/rollup/rollup/issues/4708 | ||
// Tread carefully! | ||
const html = await updateImageReferences(${JSON.stringify(html)}); | ||
`; | ||
} |
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