From 3f27a3a3e726e941ad6e996db53f9366506a7012 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 23 Jan 2026 02:06:54 -0500 Subject: [PATCH] feat: preview markdown in paste creation; prevent external option in parseMarkdown --- SharpbinV3.Client/src/lib/utils/markdown.ts | 140 +++++------ SharpbinV3.Client/src/routes/+page.svelte | 224 +++++++++++------- .../src/routes/[id]/+page.svelte | 84 ------- SharpbinV3.Client/src/routes/layout.css | 85 +++++++ 4 files changed, 297 insertions(+), 236 deletions(-) diff --git a/SharpbinV3.Client/src/lib/utils/markdown.ts b/SharpbinV3.Client/src/lib/utils/markdown.ts index 9cc2c0f..57bb967 100644 --- a/SharpbinV3.Client/src/lib/utils/markdown.ts +++ b/SharpbinV3.Client/src/lib/utils/markdown.ts @@ -9,7 +9,7 @@ const tagMap: Record = { IFRAME: 'iframe' }; -export async function parseMarkdown(md: string): Promise { +export async function parseMarkdown(md: string, preventExternal: boolean = true): Promise { const html = await marked.parse(md); let dom: WindowLike & { document: Document }; @@ -24,77 +24,79 @@ export async function parseMarkdown(md: string): Promise { const DOMPurify = createDOMPurify(dom); - DOMPurify.addHook('afterSanitizeElements', (node) => { - if (node.nodeType !== 1) return; - const element = node as Element; + if (preventExternal) { + DOMPurify.addHook('afterSanitizeElements', (node) => { + if (node.nodeType !== 1) return; + const element = node as Element; - const tag = element.nodeName; - if (tag === 'IMG' || tag === 'VIDEO' || tag === 'IFRAME' || tag === 'AUDIO') { - const src = element.getAttribute('src'); - if (!src) return; + const tag = element.nodeName; + if (tag === 'IMG' || tag === 'VIDEO' || tag === 'IFRAME' || tag === 'AUDIO') { + const src = element.getAttribute('src'); + if (!src) return; - let hostname = 'external server'; - try { - hostname = new URL(src).hostname; - } catch { - //might be a relative url + let hostname = 'external server'; + try { + hostname = new URL(src).hostname; + } catch { + //might be a relative url + } + + const placeholder = dom.document.createElement('span'); + placeholder.className = 'media-placeholder'; + placeholder.setAttribute( + 'style', + 'border: 1px solid #404040; padding: 1rem; margin: 1rem 0; border-radius: 0.5rem; background-color: #1e1e1e; text-align: center; display: block; font-style: normal; color: white; cursor: default; text-decoration: none; user-select: none;' + ); + + const warning = dom.document.createElement('span'); + warning.textContent = `External ${tagMap[tag] || tag.toLowerCase()} hidden for privacy.`; + warning.setAttribute( + 'style', + 'margin-bottom: 0.5rem; font-weight: bold; display: block; text-decoration: none;' + ); + + const subWarning = dom.document.createElement('span'); + subWarning.textContent = `Loading this will expose your IP address to ${hostname}.`; + subWarning.setAttribute( + 'style', + 'font-size: 0.75rem; color: #a3a3a3; margin-bottom: 1rem; display: block; text-decoration: none;' + ); + + const button = dom.document.createElement('button'); + button.textContent = `Load ${tagMap[tag] || tag.toLowerCase()}`; + button.className = 'load-media-btn'; + button.setAttribute('data-src', src); + button.setAttribute('data-tag', tag.toLowerCase()); + button.setAttribute( + 'style', + 'background-color: #404040; color: white; padding: 0.25rem 0.75rem; border-radius: 0.25rem; cursor: pointer; text-decoration: none;' + ); + + if (element.getAttribute('alt')) + button.setAttribute('data-alt', element.getAttribute('alt') || ''); + if (element.getAttribute('title')) + button.setAttribute('data-title', element.getAttribute('title') || ''); + if (element.getAttribute('width')) + button.setAttribute('data-width', element.getAttribute('width') || ''); + if (element.getAttribute('height')) + button.setAttribute('data-height', element.getAttribute('height') || ''); + + placeholder.appendChild(warning); + placeholder.appendChild(subWarning); + placeholder.appendChild(button); + + const parent = element.parentNode; + if (parent && parent.nodeName === 'A') { + const anchor = parent as HTMLElement; + button.setAttribute('data-href', anchor.getAttribute('href') || ''); + button.setAttribute('data-target', anchor.getAttribute('target') || ''); + anchor.parentNode?.replaceChild(placeholder, anchor); + } else { + element.parentNode?.replaceChild(placeholder, element); + } } - - const placeholder = dom.document.createElement('span'); - placeholder.className = 'media-placeholder'; - placeholder.setAttribute( - 'style', - 'border: 1px solid #404040; padding: 1rem; margin: 1rem 0; border-radius: 0.5rem; background-color: #1e1e1e; text-align: center; display: block; font-style: normal; color: white; cursor: default; text-decoration: none; user-select: none;' - ); - - const warning = dom.document.createElement('span'); - warning.textContent = `External ${tagMap[tag] || tag.toLowerCase()} hidden for privacy.`; - warning.setAttribute( - 'style', - 'margin-bottom: 0.5rem; font-weight: bold; display: block; text-decoration: none;' - ); - - const subWarning = dom.document.createElement('span'); - subWarning.textContent = `Loading this will expose your IP address to ${hostname}.`; - subWarning.setAttribute( - 'style', - 'font-size: 0.75rem; color: #a3a3a3; margin-bottom: 1rem; display: block; text-decoration: none;' - ); - - const button = dom.document.createElement('button'); - button.textContent = `Load ${tagMap[tag] || tag.toLowerCase()}`; - button.className = 'load-media-btn'; - button.setAttribute('data-src', src); - button.setAttribute('data-tag', tag.toLowerCase()); - button.setAttribute( - 'style', - 'background-color: #404040; color: white; padding: 0.25rem 0.75rem; border-radius: 0.25rem; cursor: pointer; text-decoration: none;' - ); - - if (element.getAttribute('alt')) - button.setAttribute('data-alt', element.getAttribute('alt') || ''); - if (element.getAttribute('title')) - button.setAttribute('data-title', element.getAttribute('title') || ''); - if (element.getAttribute('width')) - button.setAttribute('data-width', element.getAttribute('width') || ''); - if (element.getAttribute('height')) - button.setAttribute('data-height', element.getAttribute('height') || ''); - - placeholder.appendChild(warning); - placeholder.appendChild(subWarning); - placeholder.appendChild(button); - - const parent = element.parentNode; - if (parent && parent.nodeName === 'A') { - const anchor = parent as HTMLElement; - button.setAttribute('data-href', anchor.getAttribute('href') || ''); - button.setAttribute('data-target', anchor.getAttribute('target') || ''); - anchor.parentNode?.replaceChild(placeholder, anchor); - } else { - element.parentNode?.replaceChild(placeholder, element); - } - } - }); + }); + } return DOMPurify.sanitize(html); } diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 4c9c785..3a6bebd 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -13,6 +13,7 @@ tooltip, syntaxFromExtension } from '$lib/utils/misc'; + import { parseMarkdown } from '$lib/utils/markdown'; import Dropdown from '$lib/components/Dropdown.svelte'; import { SvelteURLSearchParams } from 'svelte/reactivity'; import { FileUp, X, Eye, EyeClosed, Dices } from '@lucide/svelte'; @@ -32,14 +33,32 @@ let anonymousUpload = false; let isDragging = false; let dragError = ''; + let previewMode = false; + let renderedMarkdown = ''; $: currentByteSize = selectedVisibility === 2 ? estimateEncryptedSize(content) : new TextEncoder().encode(content).length; + $: if (selectedSyntax !== 'markdown') { + previewMode = false; + } + const MAX_HEIGHT = () => Math.floor((window.innerHeight - 120) * 0.6); + let previewHeight = 'auto'; + async function togglePreview(toPreview: boolean) { + if (toPreview && !previewMode) { + previewHeight = textArea?.style.height || 'auto'; + renderedMarkdown = await parseMarkdown(content, false); + } + previewMode = toPreview; + if (!toPreview) { + requestAnimationFrame(resize); + } + } + function resize() { if (!textArea) return; @@ -233,78 +252,88 @@ />
- + reader.readAsArrayBuffer(file); + }} + > + {/if} {#if isDragging || (dragError && dragError !== '')}
{/if}
-
-
- - {content.split('\n').length} line{content.split('\n').length !== 1 ? 's' : ''} - - - {formatNumber(content.length)} char{content.length !== 1 ? 's' : ''} -
-
- (data.options?.maxPasteSize ?? 0)} +
+ {#if selectedSyntax === 'markdown'} +
- {formatBytes(currentByteSize)} - - - {formatBytes(data.options?.maxPasteSize ?? 0)} + + +
+ {/if} +
+
+ + {content.split('\n').length} line{content.split('\n').length !== 1 ? 's' : ''} + + + {formatNumber(content.length)} char{content.length !== 1 ? 's' : ''} +
+
+ (data.options?.maxPasteSize ?? 0)} + > + {formatBytes(currentByteSize)} + + + {formatBytes(data.options?.maxPasteSize ?? 0)} +
diff --git a/SharpbinV3.Client/src/routes/[id]/+page.svelte b/SharpbinV3.Client/src/routes/[id]/+page.svelte index af977d3..29f0a55 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.svelte +++ b/SharpbinV3.Client/src/routes/[id]/+page.svelte @@ -877,90 +877,6 @@ .hljs { background-color: var(--color-neutral-800) !important; } - .markdown > :first-child { - margin-top: 0 !important; - padding-top: 0 !important; - } - .markdown h1 { - font-size: 2.25rem; - font-weight: 700; - margin-top: 1.5rem; - margin-bottom: 1rem; - border-bottom: 1px solid var(--color-neutral-700); - padding-bottom: 0.3rem; - } - .markdown h2 { - font-size: 1.875rem; - font-weight: 600; - margin-top: 1.25rem; - margin-bottom: 0.75rem; - border-bottom: 1px solid var(--color-neutral-700); - padding-bottom: 0.2rem; - } - .markdown h3 { - font-size: 1.5rem; - font-weight: 600; - margin-top: 1rem; - margin-bottom: 0.5rem; - } - .markdown p { - margin-bottom: 1rem; - line-height: 1.6; - } - .markdown ul { - list-style-type: disc; - margin-left: 1.5rem; - margin-bottom: 1rem; - } - .markdown ol { - list-style-type: decimal; - margin-left: 1.5rem; - margin-bottom: 1rem; - } - .markdown li { - margin-bottom: 0.25rem; - } - .markdown a { - color: #3b82f6; - text-decoration: underline; - } - .markdown .load-media-btn:hover { - background-color: #525252 !important; - } - .markdown pre { - background-color: #1a1a1a; - padding: 1rem; - border-radius: 0.375rem; - margin-bottom: 1rem; - overflow-x: auto; - } - .markdown code { - background-color: #1a1a1a; - padding: 0.2rem 0.4rem; - border-radius: 0.25rem; - font-family: monospace; - } - .markdown blockquote { - border-left: 4px solid #404040; - padding-left: 1rem; - color: #a3a3a3; - font-style: italic; - margin-bottom: 1rem; - } - .markdown table { - width: 100%; - border-collapse: collapse; - margin-bottom: 1rem; - } - .markdown th, - .markdown td { - border: 1px solid #404040; - padding: 0.5rem; - text-align: left; - } - .markdown th { - background-color: #2a2a2a; - } .codeblock-with-lines { display: block; diff --git a/SharpbinV3.Client/src/routes/layout.css b/SharpbinV3.Client/src/routes/layout.css index 7f90211..c8d27d4 100644 --- a/SharpbinV3.Client/src/routes/layout.css +++ b/SharpbinV3.Client/src/routes/layout.css @@ -34,3 +34,88 @@ textarea, select { outline: none; } + +.markdown > :first-child { + margin-top: 0 !important; + padding-top: 0 !important; +} +.markdown h1 { + font-size: 2.25rem; + font-weight: 700; + margin-top: 1.5rem; + margin-bottom: 1rem; + border-bottom: 1px solid var(--color-neutral-700); + padding-bottom: 0.3rem; +} +.markdown h2 { + font-size: 1.875rem; + font-weight: 600; + margin-top: 1.25rem; + margin-bottom: 0.75rem; + border-bottom: 1px solid var(--color-neutral-700); + padding-bottom: 0.2rem; +} +.markdown h3 { + font-size: 1.5rem; + font-weight: 600; + margin-top: 1rem; + margin-bottom: 0.5rem; +} +.markdown p { + margin-bottom: 1rem; + line-height: 1.6; +} +.markdown ul { + list-style-type: disc; + margin-left: 1.5rem; + margin-bottom: 1rem; +} +.markdown ol { + list-style-type: decimal; + margin-left: 1.5rem; + margin-bottom: 1rem; +} +.markdown li { + margin-bottom: 0.25rem; +} +.markdown a { + color: #3b82f6; + text-decoration: underline; +} +.markdown .load-media-btn:hover { + background-color: #525252 !important; +} +.markdown pre { + background-color: #1a1a1a; + padding: 1rem; + border-radius: 0.375rem; + margin-bottom: 1rem; + overflow-x: auto; +} +.markdown code { + background-color: #1a1a1a; + padding: 0.2rem 0.4rem; + border-radius: 0.25rem; + font-family: monospace; +} +.markdown blockquote { + border-left: 4px solid #404040; + padding-left: 1rem; + color: #a3a3a3; + font-style: italic; + margin-bottom: 1rem; +} +.markdown table { + width: 100%; + border-collapse: collapse; + margin-bottom: 1rem; +} +.markdown th, +.markdown td { + border: 1px solid #404040; + padding: 0.5rem; + text-align: left; +} +.markdown th { + background-color: #2a2a2a; +}