refactor: remove duplicate escape functions; improve escapeHtml function

This commit is contained in:
2026-04-24 08:52:14 -04:00
parent 9064aecedb
commit 38dd017dcd
3 changed files with 18 additions and 12 deletions
+12
View File
@@ -0,0 +1,12 @@
const htmlEscapeMap: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'`': '&#96;'
};
export function escapeHtml(value: string): string {
return value.replace(/[&<>"'`]/g, (char) => htmlEscapeMap[char]);
}
@@ -1,4 +1,5 @@
import hljs from 'highlight.js';
import { escapeHtml } from '../utils/html';
type HighlightRequest = {
id: number;
@@ -11,9 +12,6 @@ type HighlightResponse = {
html: string;
};
const escapeHtml = (value: string) =>
value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
self.onmessage = (event: MessageEvent<HighlightRequest>) => {
const { id, code, lang } = event.data;
let html = '';