Home / File/ rehype-images.ts — astro Source File

rehype-images.ts — astro Source File

Architecture documentation for rehype-images.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 3 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  48b62bf2_8b05_a54d_92ee_3c8a524eed54["rehype-images.ts"]
  f76e2597_e3e8_c502_d293_a666b44d49ce["hast"]
  48b62bf2_8b05_a54d_92ee_3c8a524eed54 --> f76e2597_e3e8_c502_d293_a666b44d49ce
  d7b51bf7_4a46_1479_0cea_09e174fc7c48["unist-util-visit"]
  48b62bf2_8b05_a54d_92ee_3c8a524eed54 --> d7b51bf7_4a46_1479_0cea_09e174fc7c48
  b909a1d2_5b96_acd8_d198_1f106f44e2c3["vfile"]
  48b62bf2_8b05_a54d_92ee_3c8a524eed54 --> b909a1d2_5b96_acd8_d198_1f106f44e2c3
  style 48b62bf2_8b05_a54d_92ee_3c8a524eed54 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Properties, Root } from 'hast';
import { visit } from 'unist-util-visit';
import type { VFile } from 'vfile';

/**
 * HAST properties to preserve on the node
 */
const HAST_PRESERVED_PROPERTIES = [
	// HAST: className -> HTML: class
	'className',

	// HAST: htmlFor -> HTML: for
	'htmlFor',
];

export function rehypeImages() {
	return function (tree: Root, file: VFile) {
		if (!file.data.astro?.localImagePaths?.length && !file.data.astro?.remoteImagePaths?.length) {
			// No images to transform, nothing to do.
			return;
		}

		const imageOccurrenceMap = new Map();

		visit(tree, 'element', (node) => {
			if (node.tagName !== 'img') return;
			if (typeof node.properties?.src !== 'string') return;

			const src = decodeURI(node.properties.src);
			let imageProperties: Properties;

			if (file.data.astro?.localImagePaths?.includes(src)) {
				// Override the original `src` with the new, decoded `src` that Astro will better understand.
				imageProperties = { ...node.properties, src };
			} else if (file.data.astro?.remoteImagePaths?.includes(src)) {
				imageProperties = {
					// By default, markdown images won't have width and height set. However, just in case another user plugin does set these, we should respect them.
					inferSize: 'width' in node.properties && 'height' in node.properties ? undefined : true,
					...node.properties,
					src,
				};
			} else {
				// Not in localImagePaths or remoteImagePaths, we should not transform.
				return;
			}

			// Separate HAST-only properties from image processing properties
			const hastProperties: Properties = {};
			for (const key of HAST_PRESERVED_PROPERTIES) {
				if (key in imageProperties) {
					hastProperties[key] = imageProperties[key];
					delete imageProperties[key];
				}
			}

			// Initialize or increment occurrence count for this image
			const index = imageOccurrenceMap.get(node.properties.src) || 0;
			imageOccurrenceMap.set(node.properties.src, index + 1);

			// Set a special property on the image so later Astro code knows to process this image.
			node.properties = {
				...hastProperties,
				__ASTRO_IMAGE_: JSON.stringify({ ...imageProperties, index }),
			};
		});
	};
}

Domain

Subdomains

Functions

Dependencies

  • hast
  • unist-util-visit
  • vfile

Frequently Asked Questions

What does rehype-images.ts do?
rehype-images.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in rehype-images.ts?
rehype-images.ts defines 1 function(s): rehypeImages.
What does rehype-images.ts depend on?
rehype-images.ts imports 3 module(s): hast, unist-util-visit, vfile.
Where is rehype-images.ts in the architecture?
rehype-images.ts is located at packages/markdown/remark/src/rehype-images.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/markdown/remark/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free