aboutsummaryrefslogtreecommitdiff
path: root/packages/markdown/remark/test/remark-collect-images.test.js
diff options
context:
space:
mode:
authorGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
committerGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
commite586d7d704d475afe3373a1de6ae20d504f79d6d (patch)
tree7e3fa24807cebd48a86bd40f866d792181191ee9 /packages/markdown/remark/test/remark-collect-images.test.js
downloadastro-e586d7d704d475afe3373a1de6ae20d504f79d6d.tar.gz
astro-e586d7d704d475afe3373a1de6ae20d504f79d6d.tar.zst
astro-e586d7d704d475afe3373a1de6ae20d504f79d6d.zip
Sync from a8e1c0a7402940e0fc5beef669522b315052df1blatest
Diffstat (limited to 'packages/markdown/remark/test/remark-collect-images.test.js')
-rw-r--r--packages/markdown/remark/test/remark-collect-images.test.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/packages/markdown/remark/test/remark-collect-images.test.js b/packages/markdown/remark/test/remark-collect-images.test.js
new file mode 100644
index 000000000..e7ac08918
--- /dev/null
+++ b/packages/markdown/remark/test/remark-collect-images.test.js
@@ -0,0 +1,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 ![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']);
+ });
+});