aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/remark-images-to-component.ts
diff options
context:
space:
mode:
authorGravatar Oliver Speir <115520730+OliverSpeir@users.noreply.github.com> 2024-01-23 16:18:09 -0700
committerGravatar GitHub <noreply@github.com> 2024-01-23 18:18:09 -0500
commitdf37366556d46f7abdf82b09e33b08bd94e631b3 (patch)
treef3df548cdf291e8c150810793f1ebdf578187bc5 /packages/integrations/mdx/src/remark-images-to-component.ts
parent2e58904cd44eb6a36d2b093dc3b27ee29758326b (diff)
downloadastro-df37366556d46f7abdf82b09e33b08bd94e631b3.tar.gz
astro-df37366556d46f7abdf82b09e33b08bd94e631b3.tar.zst
astro-df37366556d46f7abdf82b09e33b08bd94e631b3.zip
MDX remark image props (#9753)
* rearrange plugins and add props to Image component * add tests and update lockfile * add changeset * re-rearrange plugin order, gfm/smartypants then user defined then image related then shiki/prism * make more generic * add more/better tests * remove unused logger --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
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);
}
});