diff --git a/__tests__/migration/html-blocks.test.ts b/__tests__/migration/html-blocks.test.ts new file mode 100644 index 000000000..8404d7017 --- /dev/null +++ b/__tests__/migration/html-blocks.test.ts @@ -0,0 +1,39 @@ +import { migrate } from '../helpers'; + +describe('migrating html blocks', () => { + it('correctly escapes back ticks', () => { + const md = ` +[block:html] +{ + "html": "\`example.com\`" +} +[/block] +`; + + const mdx = migrate(md); + expect(mdx).toMatchInlineSnapshot(` + "{\` + ${'\\`example.com\\`'} + \`} + " + `); + }); + + it('does not escape already escaped backticks', () => { + const md = ` +[block:html] +{ + "html": "${'\\\\`example.com\\\\`'}" +} +[/block] +`; + + const mdx = migrate(md); + expect(mdx).toMatchInlineSnapshot(` + "{\` + ${'\\`example.com\\`'} + \`} + " + `); + }); +}); diff --git a/processor/compile/compatibility.ts b/processor/compile/compatibility.ts index 7ba832311..21addf336 100644 --- a/processor/compile/compatibility.ts +++ b/processor/compile/compatibility.ts @@ -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'; diff --git a/processor/transform/readme-to-mdx.ts b/processor/transform/readme-to-mdx.ts index 18eacc966..41987bd95 100644 --- a/processor/transform/readme-to-mdx.ts +++ b/processor/transform/readme-to-mdx.ts @@ -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 }], diff --git a/processor/utils.ts b/processor/utils.ts index 981396e1c..3e157c9c5 100644 --- a/processor/utils.ts +++ b/processor/utils.ts @@ -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 @@ -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(/(?