mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
refactor: remove duplicate escape functions; improve escapeHtml function
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
const htmlEscapeMap: Record<string, string> = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
'`': '`'
|
||||
};
|
||||
|
||||
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, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
|
||||
self.onmessage = (event: MessageEvent<HighlightRequest>) => {
|
||||
const { id, code, lang } = event.data;
|
||||
let html = '';
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
import { syntaxes, expiresOptions } from '$lib/consts';
|
||||
import { resolve } from '$app/paths';
|
||||
import { decryptAES, encryptAES } from '$lib/utils/encryption';
|
||||
import { escapeHtml } from '$lib/utils/html';
|
||||
import { fade } from 'svelte/transition';
|
||||
import { getToken, hasRole } from '$lib/utils/auth';
|
||||
import { user } from '$lib/stores/user';
|
||||
@@ -181,8 +182,6 @@
|
||||
} else {
|
||||
try {
|
||||
if (lang === 'plaintext') {
|
||||
const escapeHtml = (s: string) =>
|
||||
s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
highlighted = escapeHtml(code);
|
||||
} else if (lang && hljs.getLanguage && hljs.getLanguage(lang)) {
|
||||
highlighted = hljs.highlight(code, { language: lang, ignoreIllegals: true }).value;
|
||||
@@ -191,8 +190,6 @@
|
||||
}
|
||||
} catch {
|
||||
if (lang === 'plaintext') {
|
||||
const escapeHtml = (s: string) =>
|
||||
s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
highlighted = escapeHtml(code);
|
||||
} else {
|
||||
highlighted = hljs.highlightAuto(code).value;
|
||||
@@ -223,19 +220,18 @@
|
||||
const rows = results.data as string[][];
|
||||
const [header, ...body] = rows;
|
||||
|
||||
const escape = (v: string) =>
|
||||
v.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
|
||||
const tableHtml = `
|
||||
<table class="csv-table">
|
||||
<thead>
|
||||
<tr>
|
||||
${header.map((c) => `<th>${c ? escape(c) : ''}</th>`).join('')}
|
||||
${header.map((c) => `<th>${c ? escapeHtml(c) : ''}</th>`).join('')}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${body
|
||||
.map((row) => `<tr>${row.map((c) => `<td>${c ? escape(c) : ''}</td>`).join('')}</tr>`)
|
||||
.map(
|
||||
(row) => `<tr>${row.map((c) => `<td>${c ? escapeHtml(c) : ''}</td>`).join('')}</tr>`
|
||||
)
|
||||
.join('')}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
Reference in New Issue
Block a user