Home / File/ remark-collect-images.test.js — astro Source File

remark-collect-images.test.js — astro Source File

Architecture documentation for remark-collect-images.test.js, a javascript file in the astro codebase. 4 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  ab3eb283_6658_77a0_de0c_15cb597b2346["remark-collect-images.test.js"]
  8350d254_ac08_0a3a_4696_d1a19f979b3a["../dist/index.js"]
  ab3eb283_6658_77a0_de0c_15cb597b2346 --> 8350d254_ac08_0a3a_4696_d1a19f979b3a
  e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607["strict"]
  ab3eb283_6658_77a0_de0c_15cb597b2346 --> e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607
  6b0635f9_51ea_77aa_767b_7857878e98a6["node:test"]
  ab3eb283_6658_77a0_de0c_15cb597b2346 --> 6b0635f9_51ea_77aa_767b_7857878e98a6
  d7b51bf7_4a46_1479_0cea_09e174fc7c48["unist-util-visit"]
  ab3eb283_6658_77a0_de0c_15cb597b2346 --> d7b51bf7_4a46_1479_0cea_09e174fc7c48
  style ab3eb283_6658_77a0_de0c_15cb597b2346 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { visit } from 'unist-util-visit';
import { createMarkdownProcessor } from '../dist/index.js';

describe('collect images', async () => {
	let processor;
	let processorWithHastProperties;

	before(async () => {
		processor = await createMarkdownProcessor({ image: { domains: ['example.com'] } });
		processorWithHastProperties = await createMarkdownProcessor({
			rehypePlugins: [
				() => {
					return (tree) => {
						visit(tree, 'element', (node) => {
							if (node.tagName === 'img') {
								node.properties.className = ['image-class'];
								node.properties.htmlFor = 'some-id';
							}
						});
					};
				},
			],
		});
	});

	it('should collect inline image paths', async () => {
		const markdown = `Hello ![inline image url](./img.png)`;
		const fileURL = 'file.md';

		const {
			code,
			metadata: { localImagePaths, remoteImagePaths },
		} = await processor.render(markdown, { fileURL });

		assert.equal(
			code,
			'<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.png&#x22;,&#x22;alt&#x22;:&#x22;inline image url&#x22;,&#x22;index&#x22;:0}"></p>',
		);

		assert.deepEqual(localImagePaths, ['./img.png']);
		assert.deepEqual(remoteImagePaths, []);
	});

	it('should collect allowed remote image paths', async () => {
		const markdown = `Hello ![inline remote image url](https://example.com/example.png)`;
		const fileURL = 'file.md';

		const {
			code,
			metadata: { localImagePaths, remoteImagePaths },
		} = await processor.render(markdown, { fileURL });
		assert.equal(
			code,
			`<p>Hello <img __ASTRO_IMAGE_="{&#x22;inferSize&#x22;:true,&#x22;src&#x22;:&#x22;https://example.com/example.png&#x22;,&#x22;alt&#x22;:&#x22;inline remote image url&#x22;,&#x22;index&#x22;:0}"></p>`,
		);

		assert.deepEqual(localImagePaths, []);
		assert.deepEqual(remoteImagePaths, ['https://example.com/example.png']);
	});

	it('should not collect other remote image paths', async () => {
		const markdown = `Hello ![inline remote image url](https://google.com/google.png)`;
		const fileURL = 'file.md';

		const {
			code,
			metadata: { localImagePaths, remoteImagePaths },
		} = await processor.render(markdown, { fileURL });
		assert.equal(
			code,
			`<p>Hello <img src="https://google.com/google.png" alt="inline remote image url"></p>`,
		);

		assert.deepEqual(localImagePaths, []);
		assert.deepEqual(remoteImagePaths, []);
	});

	it('should add image paths from definition', async () => {
		const markdown = `Hello ![image ref][img-ref] ![remote image ref][remote-img-ref]\n\n[img-ref]: ./img.webp\n[remote-img-ref]: https://example.com/example.jpg`;
		const fileURL = 'file.md';

		const { code, metadata } = await processor.render(markdown, { fileURL });

		assert.equal(
			code,
			'<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.webp&#x22;,&#x22;alt&#x22;:&#x22;image ref&#x22;,&#x22;index&#x22;:0}"> <img __ASTRO_IMAGE_="{&#x22;inferSize&#x22;:true,&#x22;src&#x22;:&#x22;https://example.com/example.jpg&#x22;,&#x22;alt&#x22;:&#x22;remote image ref&#x22;,&#x22;index&#x22;:0}"></p>',
		);

		assert.deepEqual(metadata.localImagePaths, ['./img.webp']);
		assert.deepEqual(metadata.remoteImagePaths, ['https://example.com/example.jpg']);
	});

	it('should preserve className as HTML class attribute', async () => {
		const markdown = `Hello ![image with class](./img.png)`;
		const fileURL = 'file.md';

		const { code } = await processorWithHastProperties.render(markdown, { fileURL });

		assert.equal(
			code,
			'<p>Hello <img class="image-class" for="some-id" __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.png&#x22;,&#x22;alt&#x22;:&#x22;image with class&#x22;,&#x22;index&#x22;:0}"></p>',
		);
	});
});

Domain

Dependencies

  • ../dist/index.js
  • node:test
  • strict
  • unist-util-visit

Frequently Asked Questions

What does remark-collect-images.test.js do?
remark-collect-images.test.js is a source file in the astro codebase, written in javascript. It belongs to the CoreAstro domain.
What does remark-collect-images.test.js depend on?
remark-collect-images.test.js imports 4 module(s): ../dist/index.js, node:test, strict, unist-util-visit.
Where is remark-collect-images.test.js in the architecture?
remark-collect-images.test.js is located at packages/markdown/remark/test/remark-collect-images.test.js (domain: CoreAstro, directory: packages/markdown/remark/test).

Analyze Your Own Codebase

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

Try Supermodel Free