feat: Support for filename as title (plugin v1.2)

This commit is contained in:
Maxime Cannoodt 2022-11-14 21:07:15 +01:00
parent 8d37c90106
commit de32a04f9c
4 changed files with 50 additions and 18 deletions

View File

@ -19,6 +19,8 @@
import Footnote from '$lib/marked/renderers/Footnote.svelte'; import Footnote from '$lib/marked/renderers/Footnote.svelte';
export let plaintext: string; export let plaintext: string;
export let fileTitle: string | undefined;
let ref: HTMLDivElement; let ref: HTMLDivElement;
let footnotes: HTMLDivElement[]; let footnotes: HTMLDivElement[];
let footnoteContainer: HTMLDivElement; let footnoteContainer: HTMLDivElement;
@ -29,10 +31,14 @@
const options = { ...marked.defaults, breaks: true }; const options = { ...marked.defaults, breaks: true };
function onParsed() { function onParsed() {
setTitle(); !fileTitle && setTitle();
parseFootnotes(); parseFootnotes();
} }
$: if (fileTitle) {
document.title = fileTitle.trim();
}
/** /**
* Searches for the first major header in the document to use as page title. * Searches for the first major header in the document to use as page title.
*/ */
@ -68,6 +74,9 @@
prose-strong:font-bold prose-a:font-normal prose-blockquote:font-normal prose-blockquote:not-italic prose-strong:font-bold prose-a:font-normal prose-blockquote:font-normal prose-blockquote:not-italic
prose-blockquote:first:before:content-[''] prose-hr:transition-colors prose-code:before:content-[''] prose-code:after:content-['']" prose-blockquote:first:before:content-[''] prose-hr:transition-colors prose-code:before:content-[''] prose-code:after:content-['']"
> >
{#if fileTitle}
<h1>{fileTitle}</h1>
{/if}
<SvelteMarkdown <SvelteMarkdown
on:parsed={onParsed} on:parsed={onParsed}
renderers={{ renderers={{

View File

@ -12,6 +12,15 @@
Notes are stored for a limited amount of time. The note at this link was either set to expire, Notes are stored for a limited amount of time. The note at this link was either set to expire,
or deleted due to inactivity. Sorry! or deleted due to inactivity. Sorry!
</p> </p>
{:else}
<h1>Something went wrong 🤔</h1>
<p class="prose-xl">
{#if import.meta.env.DEV}
<pre class="prose-xl">{JSON.stringify($page.error, null, 2)}</pre>
{:else}
<p class="prose-xl">An error occurred while loading this page. Please try again later.</p>
{/if}
</p>
{/if} {/if}
<div class="not-prose w-full flex justify-center mt-16"> <div class="not-prose w-full flex justify-center mt-16">

View File

@ -25,9 +25,9 @@ export const load: PageServerLoad = async ({ request, params, setHeaders, getCli
}); });
return { note }; return { note };
} catch { } catch {
error(500, response.statusText); throw error(500, response.statusText);
} }
} else { } else {
error(response.status, response.statusText); throw error(response.status, response.statusText);
} }
}; };

View File

@ -17,20 +17,7 @@
let timeString: string; let timeString: string;
let decryptFailed = false; let decryptFailed = false;
let showRaw = false; let showRaw = false;
let fileTitle: string | undefined;
onMount(() => {
if (browser && note) {
const key = location.hash.slice(1);
decrypt({ ...note, key }, note.crypto_version)
.then((value) => (plaintext = value))
.catch(() => (decryptFailed = true));
}
});
$: if (note?.insert_time) {
const diff_ms = new Date().valueOf() - new Date(note.insert_time).valueOf();
timeString = msToString(diff_ms);
}
function toggleRaw() { function toggleRaw() {
showRaw = !showRaw; showRaw = !showRaw;
@ -52,6 +39,33 @@
const months = days / 30.42; const months = days / 30.42;
return `${Math.floor(months)} month${months >= 2 ? 's' : ''}`; return `${Math.floor(months)} month${months >= 2 ? 's' : ''}`;
} }
function parsePayload(payload: string): { body: string; title?: string } {
try {
const parsed = JSON.parse(payload);
return { body: parsed?.body, title: parsed?.title };
} catch (e) {
return { body: payload, title: undefined };
}
}
onMount(() => {
if (browser && note) {
const key = location.hash.slice(1);
decrypt({ ...note, key }, note.crypto_version)
.then((value) => {
const { body, title } = parsePayload(value);
plaintext = body;
fileTitle = title;
})
.catch(() => (decryptFailed = true));
}
});
$: if (note?.insert_time) {
const diff_ms = new Date().valueOf() - new Date(note.insert_time).valueOf();
timeString = msToString(diff_ms);
}
</script> </script>
<svelte:head> <svelte:head>
@ -88,7 +102,7 @@
{#if showRaw} {#if showRaw}
<RawRenderer>{plaintext}</RawRenderer> <RawRenderer>{plaintext}</RawRenderer>
{:else} {:else}
<MarkdownRenderer {plaintext} /> <MarkdownRenderer {plaintext} {fileTitle} />
{/if} {/if}
</div> </div>
{/if} {/if}