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
17 changes: 17 additions & 0 deletions core/src/__test__/htmlEncode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import rehypePrism from 'rehype-prism-plus';
import type { PluggableList } from 'unified';
import { htmlEncode, processHtml } from '../utils';

it.each([
'```html\n<div title="x&y" />\n```',
'<div>&lt; & "quoted"</div>',
'```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(`<pre><code class="language-markdown">${htmlEncode(value)}</code></pre>`, plugins);
expect(container.querySelector('code')?.textContent).toBe(value);
expect(container.querySelector('code > div')).toBeNull();
}
});
6 changes: 0 additions & 6 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' }) as Record<string, string>)[c],
);
})
.replace(
/[<&"]/g,
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' }) as Record<string, string>)[c],
Expand Down
Loading