Adding support for custom HTML markup #4
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Users can now control how the plugin replaces image nodes with HTML markup in the config. This is helpful for people who want to have a different set of attributes, modify alt text, set custom classes, want a different style of markup for their output, and other use cases.
Default Markup
Writing your own markup function.
Just create a function that returns a template string with the HTML markup you need. It'll get called in the plugin with the following values passed into the props as an object.
astro-config-mjs
src
: The path to the image in youroutDir
. Very recommended to NOT modify this and pass it into your <img> tag as is. (see the defaultMarkup)width
: The width of the optimized image. Keep in mind that this will be the same as the width of the original image.height
: The height of the optimized image. Keep in mind that this will be the same as the height of the original image.alt
: The alt text associated with the image in Markdown.format
: The base format of the optimized image. By default this should bejpeg
for local images and whatever format the image came with for remote images.sources
: A string containing the proper <source> tag markup for the image (the outlined markup).isRemote
: A Boolean that's true if the image was originally remote (don't worry about this if you don't haveremoteImages
enabled.mdFilePath
: The path to the Markdown file referencing this image You could use Node'spath.basename
to get the md file name from this, among other things you might find a use for.Custom Markup Examples (
astro.config.mjs
)Just an <img> node with the optimized image + alt, nothing else
In case you want some simpler markup
Removing the
width
andheight
attributes from the <img> node@scottaw66's use case
Adding a class