Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for custom HTML markup #4

Merged
merged 2 commits into from
Feb 8, 2023
Merged

Conversation

seesjays
Copy link
Owner

@seesjays seesjays commented Feb 8, 2023

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

export function defaultMarkup({ src, sources, width, height, alt }: MarkupValues)
{
    return `
        <picture>
        ${sources}
        <img
            src="${src}"
            width="${width}"
            height="${height}"
            alt="${alt}"
            loading="lazy"
            decoding="async">
    </picture>`;
}

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

import { defineConfig } from 'astro/config';
import { remarkEleventyImage } from "astro-remark-eleventy-image";

function customMarkup({ src, width, height, alt, format, sources, isRemote, mdFilePath }) {
    return `
        <picture>
        ${sources}
        <img
            src="${src}"
            width="${width}"
            height="${height}"
            alt="${alt}"
            loading="lazy"
            decoding="async">
    </picture>`;
}

// https://astro.build/config
export default defineConfig({
  markdown: {
    remarkPlugins: [remarkEleventyImage],
    remarkImages: {
      customMarkup: customMarkup
    }
  }
});
type MarkupValues = {
    src: string,
    width: number,
    height: number,
    alt: string,
    format: string,
    sources: string,
    isRemote: boolean,
    mdFilePath: string,
}

src: The path to the image in your outDir. 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 be jpeg 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).
Screen Shot 2023-02-08 at 1 44 50 PM

isRemote: A Boolean that's true if the image was originally remote (don't worry about this if you don't have remoteImages enabled.

mdFilePath: The path to the Markdown file referencing this image You could use Node's path.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

function customMarkup({ src, width, height, alt, format, sources, isRemote, mdFilePath }) {
	return `
	<img
		src="${src}"
		width="${width}"
		height="${height}"
		alt="${alt}"
		loading="lazy"
		decoding="async">
	`;
}

Removing the width and height attributes from the <img> node

@scottaw66's use case

export function customMarkup({ src, sources, width, height, alt }: MarkupValues)
{
    return `
        <picture>
        ${sources}
        <img
            src="${src}"
            alt="${alt}"
            loading="lazy"
            decoding="async">
    </picture>`;
}

Adding a class

export function customMarkup({ src, sources, width, height, alt }: MarkupValues)
{
    return `
        <picture class="custom-class">
        ${sources}
        <img
            src="${src}"
            alt="${alt}"
            loading="lazy"
            decoding="async">
    </picture>`;
}

@seesjays seesjays added the enhancement New feature or request label Feb 8, 2023
@seesjays seesjays merged commit d8f07bb into main Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant