Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@
"@fontsource/noto-sans-mono": "^5.0.9",
"@lezer/highlight": "^1.1.3",
"@sveltejs/package": "^2.3.10",
"@types/dompurify": "^3.2.0",
"class-variance-authority": "^0.7.0",
"date-fns": "^2.29.3",
"date-fns-tz": "^1.3.7",
"dompurify": "^3.2.7",
"esm-env": "^1.0.0",
"hast-util-sanitize": "^5.0.1",
"hast-util-to-html": "^9.0.1",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

277 changes: 252 additions & 25 deletions src/lib/holocene/markdown-editor/preview.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script lang="ts">
import DOMPurify from 'dompurify';
import { type ClassNameValue, twMerge } from 'tailwind-merge';

import { page } from '$app/state';

import { useDarkMode } from '$lib/utilities/dark-mode';
import { renderMarkdown } from '$lib/utilities/render-markdown';

interface Props {
content: string;
Expand All @@ -19,19 +21,6 @@

let { content, class: className = '', overrideTheme = '' }: Props = $props();

let iframe: HTMLIFrameElement | null = $state(null);

const resizeIframe = () => {
if (!iframe) return;
const minHeight = 100;
const height = Math.max(
iframe.contentWindow.document.body.scrollHeight + 2,
minHeight,
);
iframe.height = '';
iframe.height = height + 'px';
};

const { workflow: workflowId, run: runId, namespace } = page.params;

const replaceTemplate = (content: string) => {
Expand All @@ -48,18 +37,256 @@
};

const templatedContent = $derived(replaceTemplate(content));
const previewTheme = $derived($useDarkMode ? 'dark' : 'light');
const previewPath = $derived(
`/render?content=${encodeURIComponent(templatedContent)}&theme=${previewTheme}&overrideTheme=${overrideTheme}`,
);
const isDark = $derived($useDarkMode);
const themeClass = $derived.by(() => {
if (overrideTheme) {
return isDark ? `dark-${overrideTheme}` : `light-${overrideTheme}`;
}
return isDark ? 'dark' : 'light';
});

let renderedHtml = $state('');

$effect(() => {
renderMarkdown(templatedContent).then((html) => {
const purified = DOMPurify.sanitize(html, {
ALLOWED_TAGS: [
'p',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'ul',
'ol',
'li',
'code',
'pre',
'a',
'strong',
'em',
'blockquote',
'table',
'thead',
'tbody',
'tr',
'th',
'td',
'br',
'hr',
'del',
'ins',
],
ALLOWED_ATTR: ['href', 'target', 'rel', 'class'],
ALLOW_DATA_ATTR: false,
ALLOW_ARIA_ATTR: false,
ALLOW_UNKNOWN_PROTOCOLS: false,
});
renderedHtml = purified;
});
});
</script>

<section class={twMerge('h-full w-full', className)}>
<iframe
bind:this={iframe}
onload={resizeIframe}
title="output"
class="w-full"
src={previewPath}
></iframe>
<section
class={twMerge('markdown-preview h-full w-full p-4', className)}
data-theme={themeClass}
>
{@html renderedHtml}
</section>

<style>
.markdown-preview {
font-size: 14px;
font-weight: 400;
line-height: 20px;
white-space: pre-line;
font-family: sans-serif;
}

.markdown-preview :global(h1) {
font-size: 2em;
}

.markdown-preview :global(h2) {
font-size: 1.5em;
}

.markdown-preview :global(h3) {
font-size: 1.17em;
}

.markdown-preview :global(h4) {
font-size: 1em;
}

.markdown-preview :global(h5) {
font-size: 0.83em;
}

.markdown-preview :global(h6) {
font-size: 0.67em;
}

.markdown-preview :global(ul),
.markdown-preview :global(ol) {
white-space: normal;
}

.markdown-preview :global(li) {
list-style-position: inside;
}

.markdown-preview :global(li *) {
display: inline;
}

.markdown-preview :global(a) {
gap: 0.5rem;
align-items: center;
border-radius: 0.25rem;
max-width: fit-content;
text-decoration: underline;
text-underline-offset: 2px;
cursor: pointer;
}

.markdown-preview :global(blockquote) {
padding-top: 0;
padding-bottom: 0;
padding-left: 0.5rem;
border-left: 4px solid;
border-left-color: #92a4c3;
background: #e8efff;
color: #121416;
}

.markdown-preview :global(blockquote p) {
font-size: 1.25rem;
line-height: 1.75rem;
}

.markdown-preview :global(code) {
font-family: monospace;
padding: 0.5rem;
border-radius: 0.25rem;
background: #e8efff;
color: #121416;
}

.markdown-preview :global(pre) {
font-family: monospace;
padding: 0.5rem;
border-radius: 0.25rem;
background: #e8efff;
color: #121416;
}

.markdown-preview :global(pre code) {
padding: 0;
}

.markdown-preview[data-theme='light'] {
background-color: #fff;
color: #121416;
}

.markdown-preview[data-theme='light'] :global(a) {
color: #444ce7;
}

.markdown-preview[data-theme='dark'] {
background-color: #141414;
color: #f8fafc;
}

.markdown-preview[data-theme='dark'] :global(a) {
color: #8098f9;
}

.markdown-preview[data-theme='light-background'] {
background-color: #f8fafc;
color: #121416;
}

.markdown-preview[data-theme='light-background'] :global(a) {
color: #444ce7;
}

.markdown-preview[data-theme='light-background'] :global(code) {
background: #e8efff;
color: #121416;
}

.markdown-preview[data-theme='light-background'] :global(pre) {
padding: 0.5rem;
border-radius: 0.25rem;
border-color: #aebed9;
background: #e8efff;
color: #141414;
}

.markdown-preview[data-theme='dark-background'] {
background-color: #141414;
color: #f8fafc;
}

.markdown-preview[data-theme='dark-background'] :global(a) {
color: #8098f9;
}

.markdown-preview[data-theme='dark-background'] :global(code) {
background: #292d3e;
color: #f8fafc;
}

.markdown-preview[data-theme='dark-background'] :global(pre) {
padding: 0.5rem;
border-radius: 0.25rem;
border-color: #1e293b;
background: #292d3e;
color: #f8fafc;
}

.markdown-preview[data-theme='light-primary'] {
background-color: #fff;
color: #121416;
}

.markdown-preview[data-theme='light-primary'] :global(a) {
color: #444ce7;
}

.markdown-preview[data-theme='light-primary'] :global(code) {
background: #e8efff;
color: #121416;
}

.markdown-preview[data-theme='light-primary'] :global(pre) {
padding: 0.5rem;
border-radius: 0.25rem;
background: #e8efff;
color: #121416;
}

.markdown-preview[data-theme='dark-primary'] {
background-color: #000;
color: #f8fafc;
}

.markdown-preview[data-theme='dark-primary'] :global(a) {
color: #8098f9;
}

.markdown-preview[data-theme='dark-primary'] :global(code) {
background: #292d3e;
color: #f8fafc;
}

.markdown-preview[data-theme='dark-primary'] :global(pre) {
padding: 0.5rem;
border-radius: 0.25rem;
background: #292d3e;
color: #f8fafc;
}
</style>
Loading
Loading