-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathArticle.svelte
More file actions
95 lines (84 loc) · 2.97 KB
/
Article.svelte
File metadata and controls
95 lines (84 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<script context="module" lang="ts">
import { writable, type Writable } from 'svelte/store';
export type LayoutContext = Writable<
Record<
string,
{
title: string;
step?: number;
visible: boolean;
level: number;
}
>
>;
</script>
<script lang="ts">
import { MainFooter } from '$lib/components';
import SeoOgImage from '$lib/components/SeoOgImage.svelte';
import { DocsArticle } from '$lib/layouts';
import PromptBanner from '$lib/components/PromptBanner.svelte';
import type { TocItem } from '$lib/layouts/DocsArticle.svelte';
import { DOCS_TITLE_SUFFIX, OVERVIEW_TITLE_SUFFIX } from '$routes/titles';
import { getContext, setContext } from 'svelte';
import { page } from '$app/state';
export let title: string;
export let description: string;
export let back: string | undefined = undefined;
export let difficulty: string | undefined = undefined;
export let readtime: string | undefined = undefined;
export let date: string | undefined = undefined;
export let prompt: string | undefined = undefined;
setContext<LayoutContext>('headings', writable({}));
const headings = getContext<LayoutContext>('headings');
let selected: string | undefined = undefined;
headings.subscribe((n) => {
const noVisible = Object.values(n).every((n) => !n.visible);
if (selected && noVisible) {
return;
}
for (const key in n) {
if (n[key].visible) {
selected = key;
break;
}
}
});
$: entries = Object.entries($headings);
$: toc = entries.reduce<Array<TocItem>>((carry, [id, heading]) => {
carry.push({
title: heading.title,
href: `#${id}`,
step: heading.step,
selected: selected === id,
level: heading.level
});
return carry;
}, []);
const isProductsPage = /^\/docs\/products\/[^/]+$/.test(page.route.id!.toString());
let seoTitle = title + DOCS_TITLE_SUFFIX;
if (isProductsPage) seoTitle = title + OVERVIEW_TITLE_SUFFIX;
</script>
<svelte:head>
<!-- Titles -->
<title>{seoTitle}</title>
<meta property="og:title" content={seoTitle} />
<meta name="twitter:title" content={seoTitle} />
<!-- Description -->
<meta name="description" content={description} />
<meta property="og:description" content={description} />
<meta name="twitter:description" content={description} />
<SeoOgImage {title} {description} />
</svelte:head>
<DocsArticle {title} {back} {toc} {date} promptPath={prompt}>
<svelte:fragment slot="metadata">
{#if difficulty}
<li>{difficulty}</li>
{/if}
{#if readtime}
<li>{readtime} min</li>
{/if}
</svelte:fragment>
<PromptBanner promptPath={prompt} />
<slot />
</DocsArticle>
<MainFooter variant="docs" />