This plugin is part of the remark plugin infrastructure and meant to be used in conjunction with imagetools from Jonas Kruckenberg. A typical use case would be writing Markdown content using the mdsvex plugin for svelte. Let's say you want to use an image desert.webp with a certain width. Doing this with imagetools is quite easy:
---
title: example
---
<script>
import desertpic from './desert.jpg?w=400&format=webp';
</script>
![Nice desert picture]({desertpic.src})
Looks simple enough but I thought this could be better. Creating the URL might become cumbersome and doesn't look very nice. In addition to that you have to put this script block in there.
This is where the remark-imagetools plugin comes into play. If you add it to the list of remark plugins you can change the above code to this:
---
title: example
images:
- name: desertpic
width: 400
format: webp
---
![Nice desert picture]({desertpic.src})
This is much more readable and we got rid of the script block. However we can go much further as you typically have similar requirements for all of your images. That's where you can provide presets with the initialization of the plugin. The code than can be simplified even further (assuming the existence of a profile mobile):
---
title: example
images:
- name: desertpic
preset: mobile
---
![Nice desert picture]({desertpic.src})
In addition to that you can just make your changes to the preset in case you are making design changes. So there is no need to go through all the URLs.
Mostly if you want to use images in multiple variations without the need to setup a corresponding toolchain. imagetools makes this process pretty straight forward. Furthermore it's meant to be used in conjunction with Markdown and the svelte extension mdsvex is a perfect fit for this.
This package is ESM only. In Node.js (version 18+), install with pnpm:
pnpm i -D @kasisoft/remark-imagetools
import { Debug, remarkImagetools } from '@kasisoft/remark-imagetools';
const config = defineConfig({
...
remarkPlugins: [remarkImagetools],
...
});
const myconfig = {
debug: Debug.All,
presets: [
{
name: "mobile",
width: 400,
format: webp
},
{
name: "sourceset",
width: [100, 200, 400],
format: webp
}
]
};
const config = defineConfig({
...
remarkPlugins: [
[remarkImagetools, myconfig]
],
...
});
The configuration is fully typed using Typescript. ImagetoolsOptions is defined as followed:
export interface ImageConfigPreset {
/* The name for this preset. Must be unique. */
name : string;
/* The width(s) to use for the images. */
width? : number | number[];
/* The height(s) to use for the images. */
height? : number | number[];
/* Options allowing to do some image manipulations. */
options? : string | string[],
/* The file format to use */
format? : 'heic' | 'heif' |'avif' | 'jpeg' | 'jpg' | 'png' | 'tiff' | 'webp' | 'gif';
} /* ENDINTERFACE */
export interface ImagetoolsOptions {
/* Debug.{None, Default, RootBefore, RootAfter, ScriptBefore, ScriptAfter, All}
* It's okay to use a list of string values for the debugging levels.
* For instance: ['RootBefore', 'RootAfter']
*/
debug : Debug | string | string[];
/* Generate ts lang attribute for non existent script nodes */
scriptTS? : boolean;
/* The name for the images property in the frontmatter. Default to 'images' */
attributeName? : string;
presets? : ImageConfigPreset[];
} /* ENDINTERFACE */
lang="ts"
will be added to each create script tag. If set to false this won't happen.You can find an example project with various use cases here:
If you want to contribute I'm happy for any kind of feedback or bug reports. Please create issues and pull requests as you like but be aware that it may take some time for me to react.