A Vite plugin for SvelteKit projects that provides enhanced image optimization for both .svelte and .md (mdsvex) files. This is a fork of the official @sveltejs/enhanced-img plugin, extended to support the specific needs of Markdown-based blogs.
WARNING: This package is experimental. It uses pre-1.0 versioning and may introduce breaking changes with every minor version release.
The transformations occur in the following order:
enforce: 'pre', meaning it sees the raw .md or .svelte source before any other processors.<enhanced:img>, and optionally  or <img>).sharp and vite-imagetools to generate responsive source sets and modern formats (AVIF, WebP).<picture> elements.import statements into the file's <script> block.<picture> tags and added imports, MDSvex simply treats them as regular Svelte components/logic..svelte file) is compiled into high-performance JavaScript.You can configure the plugin in your vite.config.ts:
import { mdEnhancedImages } from 'md-enhanced-images';
export default defineConfig({
plugins: [
mdEnhancedImages({
/**
* Automatically convert markdown image syntax ``
* to optimized <picture> tags.
* @default false
*/
convertMarkdownSyntax: true,
/**
* Automatically convert regular <img> tags in markdown
* to optimized <picture> tags if the src is a local asset.
* @default false
*/
convertRegularImgTags: true
}),
// ...
]
});
@sveltejs/enhanced-imgThis implementation includes several key modifications to the original @sveltejs/enhanced-img logic:
.md files.parse function, this plugin uses a robust regex-based parser for Markdown files. This avoids "unexpected token" errors when Markdown contains content that isn't valid Svelte template syntax.![]() and HTML <img> tags into optimized images.<script> block.This package is a fork of @sveltejs/enhanced-img, which is maintained by the Svelte team.
We'd like to thank the authors of svelte-preprocess-import-assets, which the original code is partially based off of. We'd also like to thank the authors of vite-imagetools which is used in this package.