Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markdown): Add support for imageReference paths when collecting images #8475

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-seas-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': minor
---

feat(markdown): Add support for `imageReference` paths when collecting images
1 change: 1 addition & 0 deletions packages/markdown/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@astrojs/prism": "^3.0.0",
"github-slugger": "^2.0.0",
"import-meta-resolve": "^3.0.0",
"mdast-util-definitions": "^6.0.0",
"rehype-raw": "^6.1.1",
"rehype-stringify": "^9.0.4",
"remark-gfm": "^3.0.1",
Expand Down
16 changes: 13 additions & 3 deletions packages/markdown/remark/src/remark-collect-images.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import type { Image } from 'mdast';
import type { Image, ImageReference } from 'mdast';
import { definitions } from 'mdast-util-definitions';
import { visit } from 'unist-util-visit';
import type { MarkdownVFile } from './types';

export function remarkCollectImages() {
return function (tree: any, vfile: MarkdownVFile) {
if (typeof vfile?.path !== 'string') return;

const definition = definitions(tree);
const imagePaths = new Set<string>();
visit(tree, 'image', (node: Image) => {
if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
visit(tree, ['image', 'imageReference'], (node: Image | ImageReference) => {
if (node.type === 'image') {
if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
}
if (node.type === 'imageReference') {
const imageDefinition = definition(node.identifier);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of pre-loading the definitions(tree), we could also look them up lazily here. This would lower the performance impact when not using the additional imageReference syntax (the majority I guess), but slow down the implementation in this PR when that syntax is used.

Either way, this type of AST traversal is fast. We could traverse only direct tree.children (no further descendants) to minimize overhead.

if (imageDefinition) {
if (shouldOptimizeImage(imageDefinition.url)) imagePaths.add(imageDefinition.url);
}
}
});

vfile.data.imagePaths = imagePaths;
Expand Down
28 changes: 28 additions & 0 deletions packages/markdown/remark/test/remark-collect-images.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { renderMarkdown } from '../dist/index.js';
import { mockRenderMarkdownParams } from './test-utils.js';
import chai from 'chai';

describe('collect images', () => {
it('should collect inline image paths', async () => {
const { code, vfile } = await renderMarkdown(
`Hello ![inline image url](./img.png)`,
mockRenderMarkdownParams
);

chai
.expect(code)
.to.equal('<p>Hello <img alt="inline image url" __ASTRO_IMAGE_="./img.png"></p>');

chai.expect(Array.from(vfile.data.imagePaths)).to.deep.equal(['./img.png']);
});

it('should add image paths from definition', async () => {
const { code, vfile } = await renderMarkdown(
`Hello ![image ref][img-ref]\n\n[img-ref]: ./img.webp`,
mockRenderMarkdownParams
);

chai.expect(code).to.equal('<p>Hello <img alt="image ref" __ASTRO_IMAGE_="./img.webp"></p>');
chai.expect(Array.from(vfile.data.imagePaths)).to.deep.equal(['./img.webp']);
});
});
1 change: 1 addition & 0 deletions packages/markdown/remark/test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const mockRenderMarkdownParams = {
fileURL: 'file.md',
contentDir: new URL('file:///src/content/'),
};
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.