refactor: implement renderPaste to call per syntax rendering functions

This commit is contained in:
2026-01-30 17:15:32 -05:00
parent 15f0be5a2f
commit 79ca6ffa80
+27 -21
View File
@@ -113,32 +113,38 @@
return html.replace(code, numbered); return html.replace(code, numbered);
} }
async function renderCode(content: string | null = null) { async function renderPaste(content: string | null = null) {
if (!data?.paste) return; if (!data?.paste) return;
let code = content ?? decryptedContent ?? data.content ?? '';
const encoder = new TextEncoder(); let contentToRender = content ?? decryptedContent ?? data.content ?? '';
const buffer = encoder.encode(code).buffer;
isBinary = isBinaryData(buffer); isBinary = isBinaryData(new TextEncoder().encode(contentToRender).buffer);
if (isBinary) { if (isBinary) {
contentRendered = true; contentRendered = true;
decryptStatus = ''; decryptStatus = '';
return; return;
} }
const lang = (data.paste.syntax ?? '').toLowerCase(); switch (data.paste.syntax) {
case 'markdown':
if (lang === 'markdown') { pasteContent = await parseMarkdown(contentToRender);
pasteContent = await parseMarkdown(code);
contentRendered = true; contentRendered = true;
decryptStatus = ''; decryptStatus = '';
return; break;
case 'csv':
await renderCsv(contentToRender);
break;
default:
await renderCode(contentToRender);
break;
}
} }
if (lang === 'csv') { async function renderCode(content: string | null = null) {
await renderCsv(code); if (!data?.paste) return;
return; let code = content ?? decryptedContent ?? data.content ?? '';
}
const lang = (data.paste.syntax ?? '').toLowerCase();
let highlighted = ''; let highlighted = '';
try { try {
@@ -260,7 +266,7 @@
} }
data.content = rawContent; data.content = rawContent;
decryptedContent = rawContent; decryptedContent = rawContent;
await renderCode(rawContent); await renderPaste(rawContent);
} }
if ( if (
(metadata?.syntax !== data.paste.syntax || (metadata?.syntax !== data.paste.syntax ||
@@ -415,14 +421,14 @@
decryptError = 'Incorrect password. Please try again.'; decryptError = 'Incorrect password. Please try again.';
decryptStatus = 'Decryption failed.'; decryptStatus = 'Decryption failed.';
} else { } else {
await renderCode(); await renderPaste();
history.replaceState(null, '', window.location.pathname + window.location.search); history.replaceState(null, '', window.location.pathname + window.location.search);
} }
} else { } else {
decryptStatus = 'Decryption canceled.'; decryptStatus = 'Decryption canceled.';
} }
} else { } else {
await renderCode(); await renderPaste();
} }
return; return;
} }
@@ -435,22 +441,22 @@
decryptError = 'Incorrect password. Please try again.'; decryptError = 'Incorrect password. Please try again.';
decryptStatus = 'Decryption failed.'; decryptStatus = 'Decryption failed.';
} else { } else {
await renderCode(); await renderPaste();
} }
} else { } else {
decryptStatus = 'Decryption canceled.'; decryptStatus = 'Decryption canceled.';
} }
} else { } else {
decryptStatus = 'Rendering content...'; decryptStatus = 'Rendering content...';
await renderCode(); await renderPaste();
} }
} catch { } catch {
decryptStatus = 'Rendering content...'; decryptStatus = 'Rendering content...';
await renderCode(); await renderPaste();
} }
}); });
$: if (data?.paste && (data.paste.visibility !== 2 || decryptedContent !== null)) renderCode(); $: if (data?.paste && (data.paste.visibility !== 2 || decryptedContent !== null)) renderPaste();
$: reportSiteKey = $: reportSiteKey =
(data as unknown as { authOptions?: { cf_turnstile_site_key?: string | null } }).authOptions (data as unknown as { authOptions?: { cf_turnstile_site_key?: string | null } }).authOptions
?.cf_turnstile_site_key ?? null; ?.cf_turnstile_site_key ?? null;