Skip to content

Commit 9793b41

Browse files
authored
feat(site-2): Local examples (#8431)
* feat(docs): Local tutorial * Refactor some stuff * Better error handling * Fix search imports * Prerender tutorial * try prerendering hack * fix super stupid display hidden bug * Shorten the rendered URL * Shorten URL code even more * Prerender in svelte.config.js * Refactor * Fix ScreenToggle dark mode styles * Initial POC * Rvert to old hack * Fix svelte-document.jpg
1 parent 35e7a85 commit 9793b41

File tree

12 files changed

+147
-55
lines changed

12 files changed

+147
-55
lines changed

sites/svelte.dev/src/lib/components/ScreenToggle.svelte

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
display: flex;
2121
justify-content: center;
2222
align-items: center;
23-
border-top: 1px solid var(--second);
24-
background-color: white;
23+
border-top: 1px solid var(--sk-theme-2);
24+
background-color: var(--sk-back-4);
2525
}
2626
2727
button {
2828
margin: 0 0.15em;
2929
width: 4em;
3030
height: 1em;
3131
padding: 0.3em 0.4em;
32-
border-radius: var(--border-r);
32+
border-radius: var(--sk-border-radius);
3333
line-height: 1em;
3434
box-sizing: content-box;
35-
color: #888;
36-
border: 1px solid var(--back-light);
35+
color: var(--sk-text-3);
36+
border: 1px solid var(--sk-back-3);
3737
}
3838
3939
.selected {
40-
background-color: var(--prime);
40+
background-color: var(--sk-theme-1);
4141
color: white;
4242
}
4343
</style>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// @ts-check
2+
import fs from 'node:fs';
3+
4+
const base = '../../site/content/examples/';
5+
6+
/**
7+
* @returns {import('./types').ExamplesData}
8+
*/
9+
export function get_examples_data() {
10+
const examples = [];
11+
12+
for (const subdir of fs.readdirSync(base)) {
13+
// Exclude embeds
14+
if (subdir.endsWith('99-embeds')) continue;
15+
16+
const section = {
17+
title: '', // Initialise with empty
18+
slug: subdir.split('-').slice(1).join('-'),
19+
examples: [],
20+
};
21+
22+
if (!(fs.statSync(`${base}/${subdir}`).isDirectory() || subdir.endsWith('meta.json'))) continue;
23+
24+
if (!subdir.endsWith('meta.json'))
25+
section.title = JSON.parse(fs.readFileSync(`${base}/${subdir}/meta.json`, 'utf-8')).title;
26+
27+
for (const section_dir of fs.readdirSync(`${base}/${subdir}`)) {
28+
const match = /\d{2}-(.+)/.exec(section_dir);
29+
if (!match) continue;
30+
31+
const slug = match[1];
32+
33+
const example_base_dir = `${base}/${subdir}/${section_dir}`;
34+
35+
// Get title for
36+
const example_title = JSON.parse(
37+
fs.readFileSync(`${example_base_dir}/meta.json`, 'utf-8')
38+
).title;
39+
40+
const files = [];
41+
for (const file of fs
42+
.readdirSync(example_base_dir)
43+
.filter((file) => !file.endsWith('meta.json'))) {
44+
files.push({
45+
filename: file,
46+
type: file.split('.').at(-1),
47+
content: fs.readFileSync(`${example_base_dir}/${file}`, 'utf-8'),
48+
});
49+
}
50+
51+
section.examples.push({ title: example_title, slug, files });
52+
}
53+
54+
examples.push(section);
55+
}
56+
57+
return examples;
58+
}
59+
60+
/**
61+
* @param {import('./types').ExamplesData} examples_data
62+
* @returns {import('./types').ExamplesList}
63+
*/
64+
export function get_examples_list(examples_data) {
65+
return examples_data.map((section) => ({
66+
title: section.title,
67+
examples: section.examples.map((example) => ({
68+
title: example.title,
69+
slug: example.slug,
70+
})),
71+
}));
72+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {import('./types').ExamplesData} examples_data
3+
* @param {string} slug
4+
*/
5+
export function get_example(examples_data, slug) {
6+
const example = examples_data
7+
.find((section) => section.examples.find((example) => example.slug === slug))
8+
?.examples.find((example) => example.slug === slug);
9+
10+
return example;
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export type ExamplesData = {
2+
title: string;
3+
slug: string;
4+
examples: {
5+
title: string;
6+
slug: string;
7+
files: {
8+
content: string;
9+
type: 'svelte' | 'js';
10+
filename: string;
11+
}[];
12+
}[];
13+
}[];
14+
15+
export interface Example {
16+
title: string;
17+
slug: string;
18+
}
19+
20+
export interface ExampleSection {
21+
title: string;
22+
examples: Example[];
23+
}
24+
25+
export type ExamplesList = ExampleSection[];

sites/svelte.dev/src/routes/examples/+layout.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

sites/svelte.dev/src/routes/examples/+layout.svelte

Lines changed: 0 additions & 10 deletions
This file was deleted.

sites/svelte.dev/src/routes/examples/+page.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { redirect } from '@sveltejs/kit';
22

3+
export const prerender = true;
4+
35
/** @type {import('./$types').PageLoad} */
46
export function load() {
57
throw redirect(301, 'examples/hello-world');

sites/svelte.dev/src/routes/examples/[slug]/+page.js

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { get_example } from '$lib/server/examples';
2+
import { get_examples_data, get_examples_list } from '$lib/server/examples/get-examples';
3+
4+
export const prerender = true;
5+
6+
export async function load({ params }) {
7+
const examples_data = get_examples_data();
8+
9+
const examples_list = get_examples_list(examples_data);
10+
const example = get_example(examples_data, params.slug);
11+
12+
return {
13+
examples_list,
14+
example,
15+
slug: params.slug,
16+
};
17+
}

sites/svelte.dev/src/routes/examples/[slug]/+page.svelte

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<!-- FIXME sometimes it adds a trailing slash when landing -->
22
<script>
3-
import { getContext } from 'svelte';
3+
// @ts-check
44
import { navigating } from '$app/stores';
5-
import Repl from '@sveltejs/repl';
65
import ScreenToggle from '$lib/components/ScreenToggle.svelte';
7-
import {
8-
mapbox_setup, // see site/content/examples/15-context/00-context-api
9-
svelteUrl,
10-
} from '../../../config';
11-
import TableOfContents from './_TableOfContents.svelte';
6+
import Repl from '@sveltejs/repl';
7+
import { mapbox_setup, svelteUrl } from '../../../config';
8+
import TableOfContents from './TableOfContents.svelte';
129
13-
/** @type {import('./$types').PageData} */
1410
export let data;
1511
1612
/** @type {number} */
@@ -19,10 +15,8 @@
1915
/** @type {import('@sveltejs/repl').default} */
2016
let repl;
2117
22-
const { sections } = getContext('examples');
23-
2418
const clone = (file) => ({
25-
name: file.name.replace(/.\w+$/, ''),
19+
name: file.filename.replace(/.\w+$/, ''),
2620
type: file.type,
2721
source: file.content,
2822
});
@@ -34,7 +28,7 @@
3428
</script>
3529

3630
<svelte:head>
37-
<title>{data.example.name} {data.example.name ? '' : ''} Svelte Examples</title>
31+
<title>{data.example.title} {data.example.title ? '' : ''} Svelte Examples</title>
3832

3933
<meta name="twitter:title" content="Svelte examples" />
4034
<meta name="twitter:description" content="Cybernetically enhanced web apps" />
@@ -44,7 +38,11 @@
4438
<h1 class="visually-hidden">Examples</h1>
4539
<div class="examples-container" bind:clientWidth={width}>
4640
<div class="viewport offset-{offset}">
47-
<TableOfContents {sections} active_section={data.example.slug} isLoading={!!$navigating} />
41+
<TableOfContents
42+
sections={data.examples_list}
43+
active_section={data.example.slug}
44+
isLoading={!!$navigating}
45+
/>
4846
<div class="repl-container" class:loading={$navigating}>
4947
<Repl
5048
bind:this={repl}

sites/svelte.dev/src/routes/examples/[slug]/_TableOfContents.svelte renamed to sites/svelte.dev/src/routes/examples/[slug]/TableOfContents.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<ul class="examples-toc">
1616
{#each sections as section}
1717
<li>
18-
<span class="section-title">{section.name}</span>
18+
<span class="section-title">{section.title}</span>
1919

2020
{#each section.examples as example}
2121
<div class="row" class:active={example.slug === active_section} class:loading={isLoading}>
@@ -31,7 +31,7 @@
3131
src="/examples/thumbnails/{example.slug}.jpg"
3232
/>
3333

34-
<span>{example.name}</span>
34+
<span>{example.title}</span>
3535
</a>
3636
{#if example.slug === active_section}
3737
<a bind:this={active_el} href="/repl/{example.slug}" class="repl-link">REPL</a>
@@ -109,7 +109,7 @@
109109
}
110110
111111
.thumbnail {
112-
background-color: var(--sk-back-1);
112+
background-color: #fff;
113113
object-fit: contain;
114114
width: 5rem;
115115
height: 5rem;
Loading

0 commit comments

Comments
 (0)