diff --git a/core/src/__test__/htmlEncode.test.ts b/core/src/__test__/htmlEncode.test.ts new file mode 100644 index 00000000..82071301 --- /dev/null +++ b/core/src/__test__/htmlEncode.test.ts @@ -0,0 +1,17 @@ +import rehypePrism from 'rehype-prism-plus'; +import type { PluggableList } from 'unified'; +import { htmlEncode, processHtml } from '../utils'; + +it.each([ + '```html\n
\n```', + '
< & "quoted"
', + '```js\nif (a < b && b > 0) {}\n```', +])('preserves preview text and escapes markup: %s', (value) => { + const configurations: PluggableList[] = [[], [[rehypePrism, { ignoreMissing: true }]]]; + for (const plugins of configurations) { + const container = document.createElement('div'); + container.innerHTML = processHtml(`
${htmlEncode(value)}
`, plugins); + expect(container.querySelector('code')?.textContent).toBe(value); + expect(container.querySelector('code > div')).toBeNull(); + } +}); diff --git a/core/src/utils.ts b/core/src/utils.ts index 0c9764ec..15c83520 100644 --- a/core/src/utils.ts +++ b/core/src/utils.ts @@ -11,12 +11,6 @@ export const processHtml = (html: string, plugins: PluggableList = [] as Pluggab export function htmlEncode(sHtml: string) { return sHtml - .replace(/```(tsx?|jsx?|html|xml)(.*)\s+([\s\S]*?)(\s.+)?```/g, (str: string) => { - return str.replace( - /[<&"]/g, - (c: string) => (({ '<': '<', '>': '>', '&': '&', '"': '"' }) as Record)[c], - ); - }) .replace( /[<&"]/g, (c: string) => (({ '<': '<', '>': '>', '&': '&', '"': '"' }) as Record)[c],