feat: implement paste decryption

refactor: switch back to highlight.js
This commit is contained in:
2025-12-29 04:28:44 -05:00
parent a9e1e82359
commit 3a549511f9
7 changed files with 155 additions and 623 deletions
@@ -14,12 +14,16 @@
$: returnParam = currentPath !== '/' ? `?return=${encodeURIComponent($page.url.pathname + $page.url.search)}` : '';
</script>
<div class="fixed top-0 left-0 h-15 w-full bg-neutral-900">
<div class="fixed top-0 left-0 h-15 w-full bg-neutral-900 z-100">
<div class="mx-auto flex h-15 max-w-5xl items-center px-4 py-2">
<a href={resolve('/')} class="text-xl font-bold text-white hover:text-neutral-300">Sharpbin</a>
{#if $user}
<div class="ml-auto flex items-center space-x-4">
<span class="text-neutral-400">{$user.username}</span>
<a
href={resolve(`/dashboard`)}
class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800"
>Profile</a
>
<button
on:click={logout}
class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800 cursor-pointer">Logout</button
-23
View File
@@ -1,23 +0,0 @@
import { createHighlighter } from 'shiki';
import type { Highlighter } from 'shiki';
let highlighterPromise: Promise<Highlighter> | null = null;
async function loadHighlighter() {
const res = await fetch('http://localhost:5050/api/paste/create/options');
if (!res.ok) throw new Error('Failed to fetch paste options');
const data = await res.json();
return createHighlighter({
themes: ['github-dark', 'github-light'],
langs: data.syntaxes
});
}
export function getShiki() {
if (!highlighterPromise) {
highlighterPromise = loadHighlighter();
}
return highlighterPromise;
}
@@ -1,6 +1,5 @@
import type { PageServerLoad } from './$types';
import type { Paste } from '$lib/types/paste';
import { getShiki } from '$lib/server/shiki';
export const load: PageServerLoad = async ({ params, fetch }) => {
const { id } = params;
@@ -18,14 +17,5 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
const options = await pasteOptions.json();
const content = await pasteContent.text();
let highlighted = '';
if (pasteData && content) {
const shiki = await getShiki();
highlighted = shiki.codeToHtml(content, {
lang: pasteData.syntax || 'plaintext',
theme: 'github-dark'
});
}
return { paste: pasteData, content, options, highlighted };
return { paste: pasteData, content, options };
};
+135 -19
View File
@@ -1,4 +1,6 @@
<script lang="ts">
import hljs from 'highlight.js';
import { onMount } from 'svelte';
import type { PageData } from './$types';
import { FileBox, Eye, User, CalendarDays, CalendarOff, Code } from '@lucide/svelte/icons/index';
import {
@@ -8,10 +10,17 @@
tooltip
} from '$lib/utils/misc';
import { displayNames } from '$lib/consts';
import { fade } from 'svelte/transition';
import { resolve } from '$app/paths';
import { decryptAES } from '$lib/utils/encryption';
export let data: PageData;
let codeElement: HTMLElement;
let showPasswordModal = false;
let passwordInput = '';
let decryptedContent: string | null = null;
let decryptError = '';
let contentRendered = false;
function addLineNumbers(html: string): string {
const match = html.match(/<pre.*?>[\s\S]*?<code.*?>([\s\S]*?)<\/code><\/pre>/);
if (!match) return html;
@@ -26,17 +35,66 @@
.join('');
return html.replace(code, numbered);
}
function renderCode() {
if (!codeElement || !data?.paste) return;
let code = decryptedContent ?? data.content ?? '';
if (data.paste.visibility === 2 && JSON.parse(data.content)?.kdf) {
let json = JSON.parse(data.content);
if (json?.version === 1) {
}
}
const lang = data.paste.syntax ?? '';
let highlighted = '';
try {
if (lang && hljs.getLanguage && hljs.getLanguage(lang)) {
highlighted = hljs.highlight(code, { language: lang, ignoreIllegals: true }).value;
} else {
highlighted = hljs.highlightAuto(code).value;
}
} catch {
highlighted = hljs.highlightAuto(code).value;
}
const wrapped = `<pre><code class="hljs">${highlighted}</code></pre>`;
codeElement.innerHTML = addLineNumbers(wrapped);
contentRendered = true;
}
onMount(async () => {
try {
if (data?.paste && data.paste.visibility === 2) {
contentRendered = false;
const urlHash = window.location.hash.slice(1);
if (urlHash) {
const passwordFromUrl = atob(urlHash);
decryptedContent = await decryptAES(data.content, passwordFromUrl);
if (decryptedContent === null) {
showPasswordModal = true;
} else {
renderCode();
}
return;
}
showPasswordModal = true;
} else {
renderCode();
}
} catch {
renderCode();
}
});
$: if (data?.paste && (data.paste.visibility !== 2 || decryptedContent !== null)) renderCode();
</script>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
>
<div
class="max-h-[80vh] w-[95%] max-w-5xl rounded border-2 border-neutral-800 bg-neutral-900 p-4"
class="max-h-[80vh] w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-4"
>
{#if data.paste}
<h1
use:tooltip={data.paste.title || 'Untitled Paste'}
class="mb-4 truncate text-center text-4xl font-bold"
use:tooltip={data.paste.title && data.paste.title.length > 40 ? data.paste.title : ''}
>
@@ -62,7 +120,7 @@
<CalendarDays class="mr-2 h-6 w-6 text-neutral-400" />
<span
class="text-neutral-400"
title={extractDateFromUUIDv7(data.paste.uuid)?.toLocaleString() ?? 'Unknown Date'}
use:tooltip={extractDateFromUUIDv7(data.paste.uuid)?.toLocaleString() ?? 'Unknown Date'}
>
{extractDateFromUUIDv7(data.paste.uuid)?.toLocaleDateString() ?? 'Unknown Date'}
</span>
@@ -97,41 +155,99 @@
</span>
</div>
</div>
<div
class="codeblock-with-lines max-w-full overflow-x-auto overflow-y-auto"
<div class="h-15 w-full rounded bg-neutral-800" class:hidden={contentRendered}></div>
<code
class="codeblock-with-lines hidden max-w-full overflow-x-auto overflow-y-auto"
style="max-width:100vw; min-width:0;"
class:hidden={!contentRendered}
bind:this={codeElement}
>
{@html addLineNumbers(data.highlighted ?? '')}
</div>
</code>
{:else}
<h1 class="mb-4 text-center text-4xl font-bold">Paste not found</h1>
<h2 class="mt-2 text-center text-xl">The paste you are looking for does not exist.</h2>
{/if}
</div>
{#if showPasswordModal}
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm">
<form
on:submit|preventDefault={async () => {
decryptError = '';
try {
const decrypted = await decryptAES(data.content ?? '', passwordInput);
if (decrypted === null) {
decryptError = 'Incorrect password. Please try again.';
} else {
decryptedContent = decrypted;
showPasswordModal = false;
renderCode();
history.replaceState(null, '', window.location.pathname + window.location.search);
}
} catch {
decryptError = 'An error occurred during decryption. Please try again.';
}
}}
class="w-full max-w-md rounded bg-neutral-900 p-6"
>
<h2 class="mb-4 text-xl font-semibold">Enter password to decrypt</h2>
<input
type="text"
bind:value={passwordInput}
class="mb-3 w-full rounded border border-neutral-700 bg-neutral-800 p-2"
/>
{#if decryptError}
<div class="mb-3 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
{decryptError}
</div>
{/if}
<div class="flex gap-2">
<button
type="submit"
class="flex-1 cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white hover:bg-neutral-800"
>Decrypt</button
>
<button
type="button"
on:click={() => {
showPasswordModal = false;
decryptError = '';
}}
class="cursor-pointer rounded border border-neutral-700 px-4 py-2 hover:bg-neutral-950"
>Cancel</button
>
</div>
</form>
</div>
{/if}
<style>
.hljs {
background-color: var(--color-neutral-800) !important;
}
.codeblock-with-lines {
max-width: 100vw;
min-width: 0;
overflow-x: auto;
overflow-y: auto;
display: block;
width: 100%;
max-width: 100%;
overflow: visible;
}
.codeblock-with-lines pre {
display: block;
position: relative;
padding: 1em;
margin: 0;
background: none;
border-radius: 0.25em;
background-color: var(--color-neutral-800) !important;
overflow-x: auto;
min-width: max-content;
overflow: auto;
box-sizing: border-box;
min-width: 0;
width: 100%;
max-height: 60vh;
}
.code-row {
display: flex;
align-items: flex-start;
min-width: max-content;
min-width: 0;
padding: 0.1em 0;
border-radius: 0.15em;
}
@@ -151,9 +267,9 @@
}
.code-line {
display: block;
white-space: pre;
word-break: normal;
overflow-wrap: normal;
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
padding-left: 0.25em;
min-width: 0;
}
+2
View File
@@ -1,4 +1,6 @@
@import 'tailwindcss';
@import 'highlight.js/styles/github-dark.css';
* {
box-sizing: border-box;