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