summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/test/remark-collect-images.test.js
blob: cdfccf9c876e9bb1d04d4d159cdb0c8e7a534881 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { createMarkdownProcessor } from '../dist/index.js';

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

	before(async () => {
		processor = await createMarkdownProcessor();
	});

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

		const {
			code,
			metadata: { imagePaths },
		} = 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(Array.from(imagePaths), ['./img.png']);
	});

	it('should add image paths from definition', async () => {
		const markdown = `Hello ![image ref][img-ref]\n\n[img-ref]: ./img.webp`;
		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}"></p>'
		);

		assert.deepEqual(Array.from(metadata.imagePaths), ['./img.webp']);
	});
});