mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: implement web worker for syntax highlighting using highlight.js
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import hljs from 'highlight.js';
|
||||||
|
|
||||||
|
type HighlightRequest = {
|
||||||
|
id: number;
|
||||||
|
code: string;
|
||||||
|
lang: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type HighlightResponse = {
|
||||||
|
id: number;
|
||||||
|
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 = '';
|
||||||
|
try {
|
||||||
|
if (lang === 'plaintext') {
|
||||||
|
html = escapeHtml(code);
|
||||||
|
} else if (lang && hljs.getLanguage && hljs.getLanguage(lang)) {
|
||||||
|
html = hljs.highlight(code, { language: lang, ignoreIllegals: true }).value;
|
||||||
|
} else {
|
||||||
|
html = hljs.highlightAuto(code).value;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
if (lang === 'plaintext') {
|
||||||
|
html = escapeHtml(code);
|
||||||
|
} else {
|
||||||
|
html = hljs.highlightAuto(code).value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const response: HighlightResponse = { id, html };
|
||||||
|
self.postMessage(response);
|
||||||
|
};
|
||||||
@@ -43,6 +43,7 @@
|
|||||||
import Dropdown from '$lib/components/Dropdown.svelte';
|
import Dropdown from '$lib/components/Dropdown.svelte';
|
||||||
import { openModal } from '$lib/stores/modal';
|
import { openModal } from '$lib/stores/modal';
|
||||||
import type { Paste } from '$lib/types/paste';
|
import type { Paste } from '$lib/types/paste';
|
||||||
|
import HighlightWorker from '$lib/workers/highlight.worker?worker';
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
|
|
||||||
let reportSiteKey: string | null = null;
|
let reportSiteKey: string | null = null;
|
||||||
@@ -59,6 +60,8 @@
|
|||||||
let contentRendered = false;
|
let contentRendered = false;
|
||||||
let downloadedPaste = false;
|
let downloadedPaste = false;
|
||||||
let copiedPaste = false;
|
let copiedPaste = false;
|
||||||
|
let highlightWorker: Worker | null = null;
|
||||||
|
let highlightRequestId = 0;
|
||||||
|
|
||||||
let scrollY = 0;
|
let scrollY = 0;
|
||||||
|
|
||||||
@@ -90,10 +93,12 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
highlightWorker = new HighlightWorker();
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
now = new Date();
|
now = new Date();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
return () => {
|
return () => {
|
||||||
|
if (highlightWorker) highlightWorker.terminate();
|
||||||
if (interval) clearInterval(interval);
|
if (interval) clearInterval(interval);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -147,6 +152,19 @@
|
|||||||
const lang = (data.paste.syntax ?? '').toLowerCase();
|
const lang = (data.paste.syntax ?? '').toLowerCase();
|
||||||
|
|
||||||
let highlighted = '';
|
let highlighted = '';
|
||||||
|
const worker = highlightWorker;
|
||||||
|
if (worker) {
|
||||||
|
const requestId = ++highlightRequestId;
|
||||||
|
highlighted = await new Promise((resolve) => {
|
||||||
|
const handler = (event: MessageEvent<{ id: number; html: string }>) => {
|
||||||
|
if (event.data.id !== requestId) return;
|
||||||
|
worker.removeEventListener('message', handler);
|
||||||
|
resolve(event.data.html);
|
||||||
|
};
|
||||||
|
worker.addEventListener('message', handler);
|
||||||
|
worker.postMessage({ id: requestId, code, lang });
|
||||||
|
});
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
if (lang === 'plaintext') {
|
if (lang === 'plaintext') {
|
||||||
const escapeHtml = (s: string) =>
|
const escapeHtml = (s: string) =>
|
||||||
@@ -166,6 +184,7 @@
|
|||||||
highlighted = hljs.highlightAuto(code).value;
|
highlighted = hljs.highlightAuto(code).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const wrapped = `<pre><code class="hljs">${highlighted}</code></pre>`;
|
const wrapped = `<pre><code class="hljs">${highlighted}</code></pre>`;
|
||||||
pasteContent = addLineNumbers(wrapped);
|
pasteContent = addLineNumbers(wrapped);
|
||||||
contentRendered = true;
|
contentRendered = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user