Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrating html with backticks #1046

Merged
merged 1 commit into from
Jan 23, 2025
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
39 changes: 39 additions & 0 deletions __tests__/migration/html-blocks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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>
"
`);
});
});
3 changes: 0 additions & 3 deletions processor/compile/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 { formatProps } from '../utils';

Expand Down
1 change: 1 addition & 0 deletions processor/transform/readme-to-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const readmeToMdx = (): Transform => tree => {
const html = node.value;
const isScriptOrStyleTag = [!!html.match(/^<(?:style|script)/i), !!html.match(/<\/(?:style|script)>$/i)];
if (!isScriptOrStyleTag.includes(true)) return;

parent.children.splice(index, 1, {
type: 'html-block',
children: [{ type: 'text', value: html }],
Expand Down
4 changes: 2 additions & 2 deletions processor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const isMDXElement = (node: Node): node is MdxJsxFlowElement | MdxJsxText
*/
export const isMDXEsm = (node: Node): node is MdxjsEsm => {
return node.type === 'mdxjsEsm';
}
};

/**
* Takes an HTML string and formats it for display in the editor. Removes leading/trailing newlines
Expand Down Expand Up @@ -153,7 +153,7 @@ export const formatHTML = (html: string): string => {
*/
export const reformatHTML = (html: string, indent: number = 2): string => {
// Remove leading/trailing newlines
const cleaned = html.replace(/^\s*\n|\n\s*$/g, '');
const cleaned = html.replace(/^\s*\n|\n\s*$/g, '').replaceAll(/(?<!\\(\\\\)*)`/g, '\\`');

// // Create a tab/indent with the specified number of spaces
// const tab = ' '.repeat(indent);
Expand Down
Loading