Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
convert old images/embeds to new mdx components
  • Loading branch information
jennspencer committed Jun 27, 2024
commit 09271aa355fbcf3bc676d4084f32ebf57b585f79
86 changes: 86 additions & 0 deletions __tests__/compilers/compatability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,92 @@ describe('compatability with RDMD', () => {
expect(mdx(ast).trim()).toBe('<Glossary>parliament</Glossary>');
});

it('compiles mdx image nodes', () => {
const ast = {
type: 'figure',
data: {
hName: 'figure',
},
children: [
{
align: 'center',
width: '300px',
src: 'https://drastik.ch/wp-content/uploads/2023/06/blackcat.gif',
url: 'https://drastik.ch/wp-content/uploads/2023/06/blackcat.gif',
alt: '',
title: '',
type: 'image',
data: {
hProperties: {
align: 'center',
className: 'border',
width: '300px',
},
},
},
{
type: 'figcaption',
data: {
hName: 'figcaption',
},
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'hello ',
},
{
type: 'strong',
children: [
{
type: 'text',
value: 'cat',
},
],
},
],
},
],
},
],
};

expect(mdx(ast).trim()).toBe(
'<Image align="center" border="true" caption="hello **cat**" width="300px" src="https://drastik.ch/wp-content/uploads/2023/06/blackcat.gif" />',
Copy link
Collaborator

Choose a reason for hiding this comment

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

👋🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🐈‍⬛

);
});

it('compiles mdx embed nodes', () => {
const ast = {
data: {
hProperties: {
html: false,
url: 'https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf',
title: 'iframe',
href: 'https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf',
typeOfEmbed: 'iframe',
height: '300px',
width: '100%',
iframe: true,
},
hName: 'embed',
html: false,
url: 'https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf',
title: 'iframe',
href: 'https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf',
typeOfEmbed: 'iframe',
height: '300px',
width: '100%',
iframe: true,
},
type: 'embed',
};

expect(mdx(ast).trim()).toBe('<Embed html="false" url="https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf" title="iframe" href="https://cdn.shopify.com/s/files/1/0711/5132/1403/files/BRK0502-034178M.pdf" typeOfEmbed="iframe" height="300px" width="100%" iframe="true" />');
});

it('compiles reusable-content nodes', () => {
const ast = {
type: 'reusable-content',
Expand Down
33 changes: 32 additions & 1 deletion processor/compile/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Html } from 'mdast';
import { Html, Image, Node } from 'mdast';
import { fromHtml } from 'hast-util-from-html';
import { toMarkdown } from 'mdast-util-to-markdown';
import { toXast } from 'hast-util-to-xast';
import { toXml } from 'xast-util-to-xml';
import { NodeTypes } from '../../enums';
import { formatHProps, formatProps } from '../utils';

type CompatNodes =
| { type: NodeTypes.glossary; data: { hProperties: { term: string } } }
| { type: NodeTypes.glossary; data: { hName: 'Glossary' }; children: [{ type: 'text'; value: string }] }
| { type: NodeTypes.reusableContent; tag: string }
| { type: 'embed'; data: { hProperties: { [key: string]: string } } }
| { type: 'escape'; value: string }
| { type: 'figure'; children: [Image, { type: 'figcaption'; children: [{ type: 'text'; value: string }] }] }
| Html;

/*
Expand All @@ -27,6 +31,28 @@ const html = (node: Html) => {
return xml.replace(/<html.*<body>(.*)<\/body><\/html>/ms, '$1');
};

const figureToImageBlock = (node: any) => {
const { align, width, src, alt, title, ...image } = node.children.find((child: Node) => child.type === 'image');
const { className } = image.data.hProperties;
const figcaption = node.children.find((child: Node) => child.type === 'figcaption');

const caption = figcaption ? toMarkdown({
type: 'paragraph',
children: figcaption.children,
}).trim() : null;

const attributes = {
...(align && { align }),
...(alt && { alt }),
...(className && { border: className === 'border' }),
...(caption && { caption }),
...(title && { title }),
...(width && { width }),
src,
};
return `<Image ${formatProps(attributes)} />`;
}

const compatibility = (node: CompatNodes) => {
switch (node.type) {
case NodeTypes.glossary:
Expand All @@ -39,6 +65,11 @@ const compatibility = (node: CompatNodes) => {
return html(node);
case 'escape':
return `\\${node.value}`;
case 'figure':
return figureToImageBlock(node);
case 'embed':
const attributes = formatHProps(node);
return `<Embed ${attributes} />`;
default:
throw new Error('Unhandled node type!');
}
Expand Down
2 changes: 2 additions & 0 deletions processor/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function compilers() {
[NodeTypes.imageBlock]: image,
[NodeTypes.reusableContent]: compatibility,
[NodeTypes.variable]: variable,
embed: compatibility,
escape: compatibility,
figure: compatibility,
html: compatibility,
};

Expand Down
14 changes: 12 additions & 2 deletions processor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import { MdxJsxFlowElement, MdxJsxTextElement, MdxFlowExpression } from 'mdast-u
*/
export const formatHProps = <T>(node: Node): string => {
const hProps = getHProps<T>(node);
const hPropKeys = getHPropKeys<T>(node) as string[];
return hPropKeys.map(key => `${key}="${hProps[key]}"`).join(' ');
return formatProps(hProps);
}

/**
* Formats an object of props as a string.
*
* @param {Object} props
* @returns {string}
*/
export const formatProps = (props: Object): string => {
const keys = Object.keys(props);
return keys.map(key => `${key}="${props[key]}"`).join(' ');
}

/**
Expand Down