aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/remark-images-to-component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/mdx/src/remark-images-to-component.ts')
-rw-r--r--packages/integrations/mdx/src/remark-images-to-component.ts49
1 files changed, 48 insertions, 1 deletions
diff --git a/packages/integrations/mdx/src/remark-images-to-component.ts b/packages/integrations/mdx/src/remark-images-to-component.ts
index 810056def..86f04aa47 100644
--- a/packages/integrations/mdx/src/remark-images-to-component.ts
+++ b/packages/integrations/mdx/src/remark-images-to-component.ts
@@ -1,6 +1,6 @@
import type { MarkdownVFile } from '@astrojs/markdown-remark';
import type { Image, Parent } from 'mdast';
-import type { MdxJsxFlowElement, MdxjsEsm } from 'mdast-util-mdx';
+import type { MdxJsxFlowElement, MdxjsEsm, MdxJsxAttribute } from 'mdast-util-mdx';
import { visit } from 'unist-util-visit';
import { jsToTreeNode } from './utils.js';
@@ -89,6 +89,53 @@ export function remarkImageToComponent() {
});
}
+ if (node.data && node.data.hProperties) {
+ const createArrayAttribute = (name: string, values: string[]): MdxJsxAttribute => {
+ return {
+ type: 'mdxJsxAttribute',
+ name: name,
+ value: {
+ type: 'mdxJsxAttributeValueExpression',
+ value: name,
+ data: {
+ estree: {
+ type: 'Program',
+ body: [
+ {
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'ArrayExpression',
+ elements: values.map((value) => ({
+ type: 'Literal',
+ value: value,
+ raw: String(value),
+ })),
+ },
+ },
+ ],
+ sourceType: 'module',
+ comments: [],
+ },
+ },
+ },
+ };
+ };
+ // Go through every hProperty and add it as an attribute of the <Image>
+ Object.entries(node.data.hProperties as Record<string, string | string[]>).forEach(
+ ([key, value]) => {
+ if (Array.isArray(value)) {
+ componentElement.attributes.push(createArrayAttribute(key, value));
+ } else {
+ componentElement.attributes.push({
+ name: key,
+ type: 'mdxJsxAttribute',
+ value: String(value),
+ });
+ }
+ }
+ );
+ }
+
parent!.children.splice(index!, 1, componentElement);
}
});