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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
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({ image: { domains: ['example.com'] } });
});
it('should collect inline image paths', async () => {
const markdown = `Hello `;
const fileURL = 'file.md';
const {
code,
metadata: { localImagePaths, remoteImagePaths },
} = await processor.render(markdown, { fileURL });
assert.equal(
code,
'<p>Hello <img __ASTRO_IMAGE_="{"src":"./img.png","alt":"inline image url","index":0}"></p>',
);
assert.deepEqual(localImagePaths, ['./img.png']);
assert.deepEqual(remoteImagePaths, []);
});
it('should collect allowed remote image paths', async () => {
const markdown = `Hello `;
const fileURL = 'file.md';
const {
code,
metadata: { localImagePaths, remoteImagePaths },
} = await processor.render(markdown, { fileURL });
assert.equal(
code,
`<p>Hello <img __ASTRO_IMAGE_="{"inferSize":true,"src":"https://example.com/example.png","alt":"inline remote image url","index":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 `;
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_="{"src":"./img.webp","alt":"image ref","index":0}"> <img __ASTRO_IMAGE_="{"inferSize":true,"src":"https://example.com/example.jpg","alt":"remote image ref","index":0}"></p>',
);
assert.deepEqual(metadata.localImagePaths, ['./img.webp']);
assert.deepEqual(metadata.remoteImagePaths, ['https://example.com/example.jpg']);
});
});
|