Repository navigation
Specifying global images root directory for Markdown relative-path images #11
Replies: 2 comments 1 reply
|
Hmm, I’m not sure there’s a fix here. Maybe import aliases? But I’m not sure if those are taken into account for Markdown processing. |
|
Import aliases do work, for both Markdown body images and If you want just // astro.config.mjs
import { defineConfig } from 'astro/config';
import { fileURLToPath } from 'node:url';
const imagesDir = fileURLToPath(new URL('./src/assets/images/', import.meta.url));
export default defineConfig({
vite: {
resolve: {
alias: [
{ find: /^([\w-][\w.-]*\.(?:png|jpe?g|webp|avif|gif|svg))$/i, replacement: imagesDir + '$1' },
],
},
},
});The regex only matches specifiers without a slash, so Remote images: Markdown images on hosts listed in import { satteri } from '@astrojs/markdown-satteri';
const cdnPrefix = (base) => ({
name: 'cdn-prefix',
image(node, ctx) {
if (/^[\w-][\w.-]*\.\w+$/.test(node.url)) ctx.setProperty(node, 'url', new URL(node.url, base).href);
},
});
export default defineConfig({
image: { domains: ['cdn.example.com'] },
markdown: { processor: satteri({ mdastPlugins: [cdnPrefix('https://cdn.example.com/images/')] }) },
});A remark plugin ( |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
It can be not CMS-friendly to have a bunch of images directories:
content/posts/images/,content/pages/images/etcBut for full Astro image pipeline processing for Markdown, images in Markdown must be referenced via relative paths.
Is it possible to specify in config path for Markdown images dir root? (and same question for Markdown abspath images - e.g. for the case when images are stored at external image service URL)
I can try workarounds with some dir symlinks, but it's not very robust or requires extra build step
All reactions