|
| 1 | +import type { MarkdownVFile } from '@astrojs/markdown-remark'; |
| 2 | +import { type Image, type Parent } from 'mdast'; |
| 3 | +import type { MdxJsxFlowElement, MdxjsEsm } from 'mdast-util-mdx'; |
| 4 | +import { visit } from 'unist-util-visit'; |
| 5 | +import { jsToTreeNode } from './utils.js'; |
| 6 | + |
| 7 | +export function remarkImageToComponent() { |
| 8 | + return function (tree: any, file: MarkdownVFile) { |
| 9 | + if (!file.data.imagePaths) return; |
| 10 | + |
| 11 | + const importsStatements: MdxjsEsm[] = []; |
| 12 | + const importedImages = new Map<string, string>(); |
| 13 | + |
| 14 | + visit(tree, 'image', (node: Image, index: number | null, parent: Parent | null) => { |
| 15 | + // Use the imagePaths set from the remark-collect-images so we don't have to duplicate the logic for |
| 16 | + // checking if an image should be imported or not |
| 17 | + if (file.data.imagePaths?.has(node.url)) { |
| 18 | + let importName = importedImages.get(node.url); |
| 19 | + |
| 20 | + // If we haven't already imported this image, add an import statement |
| 21 | + if (!importName) { |
| 22 | + importName = `__${importedImages.size}_${node.url.replace(/\W/g, '_')}__`; |
| 23 | + |
| 24 | + importsStatements.push({ |
| 25 | + type: 'mdxjsEsm', |
| 26 | + value: '', |
| 27 | + data: { |
| 28 | + estree: { |
| 29 | + type: 'Program', |
| 30 | + sourceType: 'module', |
| 31 | + body: [ |
| 32 | + { |
| 33 | + type: 'ImportDeclaration', |
| 34 | + source: { type: 'Literal', value: node.url, raw: JSON.stringify(node.url) }, |
| 35 | + specifiers: [ |
| 36 | + { |
| 37 | + type: 'ImportDefaultSpecifier', |
| 38 | + local: { type: 'Identifier', name: importName }, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + importedImages.set(node.url, importName); |
| 47 | + } |
| 48 | + |
| 49 | + // Build a component that's equivalent to <Image src={importName} alt={node.alt} title={node.title} /> |
| 50 | + const componentElement: MdxJsxFlowElement = { |
| 51 | + name: '__AstroImage__', |
| 52 | + type: 'mdxJsxFlowElement', |
| 53 | + attributes: [ |
| 54 | + { |
| 55 | + name: 'src', |
| 56 | + type: 'mdxJsxAttribute', |
| 57 | + value: { |
| 58 | + type: 'mdxJsxAttributeValueExpression', |
| 59 | + value: importName, |
| 60 | + data: { |
| 61 | + estree: { |
| 62 | + type: 'Program', |
| 63 | + sourceType: 'module', |
| 64 | + comments: [], |
| 65 | + body: [ |
| 66 | + { |
| 67 | + type: 'ExpressionStatement', |
| 68 | + expression: { type: 'Identifier', name: importName }, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { name: 'alt', type: 'mdxJsxAttribute', value: node.alt || '' }, |
| 76 | + ], |
| 77 | + children: [], |
| 78 | + }; |
| 79 | + |
| 80 | + if (node.title) { |
| 81 | + componentElement.attributes.push({ |
| 82 | + type: 'mdxJsxAttribute', |
| 83 | + name: 'title', |
| 84 | + value: node.title, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + parent!.children.splice(index!, 1, componentElement); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + // Add all the import statements to the top of the file for the images |
| 93 | + tree.children.unshift(...importsStatements); |
| 94 | + |
| 95 | + // Add an import statement for the Astro Image component, we rename it to avoid conflicts |
| 96 | + tree.children.unshift(jsToTreeNode(`import { Image as __AstroImage__ } from "astro:assets";`)); |
| 97 | + }; |
| 98 | +} |
0 commit comments