Skip to content
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
7 changes: 7 additions & 0 deletions __tests__/lib/mdast/html-blocks/in.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<HTMLBlock>{`
<pre>
<code>
RegexpExtract([Address], "\\\\s*([a-zA-Z]+)", 2)
</code>
</pre>
`}</HTMLBlock>
20 changes: 20 additions & 0 deletions __tests__/lib/mdast/html-blocks/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"children": [
{
"children": [
{
"type": "text",
"value": "<pre>\n <code>\n RegexpExtract([Address], \"\\\\\\\\s*([a-zA-Z]+)\", 2)\n </code>\n</pre>"
}
],
"data": {
"hName": "html-block",
"hProperties": {
"html": "<pre>\n <code>\n RegexpExtract([Address], \"\\\\\\\\s*([a-zA-Z]+)\", 2)\n </code>\n</pre>"
}
},
"type": "html-block"
}
],
"type": "root"
}
11 changes: 8 additions & 3 deletions __tests__/lib/mdast/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import esmMdx from './esm/in.mdx?raw';
// @ts-expect-error - these are being imported as json
import esmJson from './esm/out.json';
// @ts-expect-error - these are being imported as strings
import htmlBlockMdx from './html-blocks/in.mdx?raw';
import htmlBlockJson from './html-blocks/out.json';
import inlineImagesMdx from './images/inline/in.mdx?raw';
// @ts-expect-error - these are being imported as json
import inlineImagesJson from './images/inline/out.json';
Expand All @@ -18,11 +20,8 @@ import tablesMdx from './tables/in.mdx?raw';
import tablesJson from './tables/out.json';
// @ts-expect-error - these are being imported as strings
import variablesMdx from './variables/in.mdx?raw';
// @ts-expect-error - these are being imported as json
import variablesJson from './variables/out.json';
// @ts-expect-error - these are being imported as strings
import variablesWithSpacesMdx from './variables-with-spaces/in.mdx?raw';
// @ts-expect-error - these are being imported as json
import variablesWithSpacesJson from './variables-with-spaces/out.json';

describe('mdast transformer', () => {
Expand Down Expand Up @@ -62,6 +61,12 @@ describe('mdast transformer', () => {
expect(mdast(esmMdx)).toStrictEqualExceptPosition(esmJson);
});

it('parses HTMLBlock contents', () => {
// @ts-expect-error - the custom matcher types are not being set up
// correctly
expect(mdast(htmlBlockMdx)).toStrictEqualExceptPosition(htmlBlockJson);
});

it('throws an error when a component does not exist and missingComponents === "throw"', () => {
const mdx = '<NonExistentComponent />';

Expand Down
39 changes: 0 additions & 39 deletions __tests__/migration/html-blocks.test.ts

This file was deleted.

Copy link
Member

Choose a reason for hiding this comment

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

Is it worth also adding a test for when something that has the escaped already in an html block?

[block:html]
{
  "html": "{`\n<pre>\n  <code>\n    RegexpExtract([Address], "\\\\s*([a-zA-Z]+)", 2)\n  </code>\n</pre>`}"
}
[/block]

wondering if having double of this would bust it up:

{`{`

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[block:html]
{
"html": "<pre>\n <code> \n RegexpExtract([Address], \"\\\\s*([a-zA-Z]+)\", 2)\n </code>\n</pre>"
}
[/block]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<HTMLBlock>{`
<pre>
<code>
RegexpExtract([Address], "\\\\s*([a-zA-Z]+)", 2)
</code>
</pre>
`}</HTMLBlock>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[block:html]
{
"html": "<pre><code>RegexpExtract([Address], \"\\\\s*([a-zA-Z]+)\", 2)</code></pre>"
}
[/block]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<HTMLBlock>{`
<pre><code>RegexpExtract([Address], "\\\\s*([a-zA-Z]+)", 2)</code></pre>
`}</HTMLBlock>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[block:html]
{
"html": "{`\n<pre>\n <code>\n RegexpExtract([Address], \"\\\\s*([a-zA-Z]+)\", 2)\n </code>\n</pre>\n`}"
}
[/block]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<HTMLBlock>{`
{\`
<pre>
<code>
RegexpExtract([Address], "\\\\s*([a-zA-Z]+)", 2)
</code>
</pre>
\`}
`}</HTMLBlock>
59 changes: 59 additions & 0 deletions __tests__/migration/html-blocks/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from 'node:fs';

import { migrate } from '../../helpers';

describe('migrating html blocks', () => {
it('correctly escapes back ticks', () => {
const md = `
[block:html]
{
"html": "<a href=\\"example.com\\">\`example.com\`</a>"
}
[/block]
`;

const mdx = migrate(md);
expect(mdx).toMatchInlineSnapshot(`
"<HTMLBlock>{\`
<a href="example.com">\\\`example.com\\\`</a>
\`}</HTMLBlock>
"
`);
});

it('does not escape already escaped backticks', () => {
const md = `
[block:html]
{
"html": "<a href=\\"example.com\\">${'\\\\`example.com\\\\`'}</a>"
}
[/block]
`;

const mdx = migrate(md);
expect(mdx).toMatchInlineSnapshot(`
"<HTMLBlock>{\`
<a href="example.com">\\\\\`example.com\\\\\`</a>
\`}</HTMLBlock>
"
`);
});

it('does not unescape backslashes', async () => {
const md = fs.readFileSync(`${__dirname}/fixtures/html-block-escapes/in.md`, 'utf-8');

await expect(migrate(md)).toMatchFileSnapshot(`${__dirname}/fixtures/html-block-escapes/out.mdx`);
});

it('correctly migrates html with newlines', async () => {
const md = fs.readFileSync(`${__dirname}/fixtures/html-block-escapes-newlines/in.md`, 'utf-8');

await expect(migrate(md)).toMatchFileSnapshot(`${__dirname}/fixtures/html-block-escapes-newlines/out.mdx`);
});

it('correctly migrates html with brackets and template literals', async () => {
const md = fs.readFileSync(`${__dirname}/fixtures/html-block-with-brackets/in.md`, 'utf-8');

await expect(migrate(md)).toMatchFileSnapshot(`${__dirname}/fixtures/html-block-with-brackets/out.mdx`);
});
});
18 changes: 12 additions & 6 deletions lib/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import migrateCallouts from '../processor/transform/migrate-callouts';
import migrateHtmlBlocks from '../processor/transform/migrate-html-blocks';
import migrateHtmlTags from '../processor/transform/migrate-html-tags';
import migrateLinkReferences from '../processor/transform/migrate-link-references';

Expand All @@ -9,12 +10,17 @@ import migrateComments from './utils/migrateComments';
const migrateDoc = (doc: string, { rdmd }): string => {
const ast = mdastV6(doc, { rdmd });

return mdx(ast, { remarkTransformers: [migrateCallouts, [migrateLinkReferences, { rdmd }], migrateHtmlTags], file: doc })
.replaceAll(/&#x20;/g, ' ')
// @note: I'm not sure what's happening, but I think mdx is converting an
// 'a' to '&#x61;' as a means of escaping it. I think this helps with
// parsing weird cases.
.replaceAll(/&#x61;/g, 'a');
return (
mdx(ast, {
remarkTransformers: [migrateCallouts, [migrateLinkReferences, { rdmd }], migrateHtmlTags, migrateHtmlBlocks],
file: doc,
})
.replaceAll(/&#x20;/g, ' ')
// @note: I'm not sure what's happening, but I think mdx is converting an
// 'a' to '&#x61;' as a means of escaping it. I think this helps with
// parsing weird cases.
.replaceAll(/&#x61;/g, 'a')
);
};

const migrate = (doc: string, opts): string => {
Expand Down
20 changes: 20 additions & 0 deletions processor/transform/migrate-html-blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Transform } from 'mdast-util-from-markdown';
import type { HTMLBlock } from 'types';

import { visit } from 'unist-util-visit';

const MigrateHtmlBlocks = (): Transform => tree => {
visit(tree, 'html-block', (node: HTMLBlock) => {
const { html, runScripts } = node.data?.hProperties || {};
const escaped = html.replaceAll(/\\/g, '\\\\');

node.data.hProperties = {
...(runScripts && { runScripts }),
html: escaped,
};
});

return tree;
};

export default MigrateHtmlBlocks;
1 change: 0 additions & 1 deletion processor/transform/readme-to-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const readmeToMdx = (): Transform => tree => {

visit(tree, 'figure', (figure, index, parent) => {
const [image, caption] = figure.children;

const { align, width } = image.data.hProperties;
const border = image.data.hProperties.className === 'border';

Expand Down