Localize the examples dependents

pull/8432/head
Puru Vijay 4 years ago
parent 9793b41817
commit 95627d0213

@ -1,9 +1,8 @@
<script> <script>
import Repl from '@sveltejs/repl';
import { onMount } from 'svelte';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { process_example } from '$lib/utils/examples'; import { process_example } from '$lib/utils/examples';
import { PUBLIC_API_BASE } from '$env/static/public'; import Repl from '@sveltejs/repl';
import { onMount } from 'svelte';
export let version = '3'; export let version = '3';
export let gist = null; export let gist = null;
@ -58,7 +57,7 @@
repl.set({ components }); repl.set({ components });
}); });
} else if (example) { } else if (example) {
fetch(`${PUBLIC_API_BASE}/docs/svelte/examples/${example}`).then(async (response) => { fetch(`/examples/${example}.json`).then(async (response) => {
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
const components = process_example(data.files); const components = process_example(data.files);

@ -42,7 +42,7 @@ export function get_examples_data() {
.readdirSync(example_base_dir) .readdirSync(example_base_dir)
.filter((file) => !file.endsWith('meta.json'))) { .filter((file) => !file.endsWith('meta.json'))) {
files.push({ files.push({
filename: file, name: file,
type: file.split('.').at(-1), type: file.split('.').at(-1),
content: fs.readFileSync(`${example_base_dir}/${file}`, 'utf-8'), content: fs.readFileSync(`${example_base_dir}/${file}`, 'utf-8'),
}); });

@ -6,8 +6,8 @@ export type ExamplesData = {
slug: string; slug: string;
files: { files: {
content: string; content: string;
type: 'svelte' | 'js'; type: string;
filename: string; name: string;
}[]; }[];
}[]; }[];
}[]; }[];

@ -1,15 +1,23 @@
import { error, json } from '@sveltejs/kit';
import { dev } from '$app/environment'; import { dev } from '$app/environment';
import * as session from '$lib/db/session';
import { client } from '$lib/db/client'; import { client } from '$lib/db/client';
import * as gist from '$lib/db/gist'; import * as gist from '$lib/db/gist';
import { PUBLIC_API_BASE } from '$env/static/public'; import * as session from '$lib/db/session';
import { get_example } from '$lib/server/examples';
import { get_examples_data, get_examples_list } from '$lib/server/examples/get-examples';
import { error, json } from '@sveltejs/kit';
const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/; const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/;
/** @type {Set<string>} */ const examples_data = get_examples_data();
let examples;
const examples = new Set(
get_examples_list(examples_data)
.map((category) => category.examples)
.flat()
.map((example) => example.slug)
);
/** @param {import('$lib/server/examples/types').ExamplesData[number]['examples'][number]['files'][number][]} files */
function munge(files) { function munge(files) {
return files return files
.map((file) => { .map((file) => {
@ -18,6 +26,7 @@ function munge(files) {
let type = file.name.slice(dot + 1); let type = file.name.slice(dot + 1);
if (type === 'html') type = 'svelte'; if (type === 'html') type = 'svelte';
// @ts-expect-error what is file.source? by @PuruVJ
return { name, type, source: file.source ?? file.content ?? '' }; return { name, type, source: file.source ?? file.content ?? '' };
}) })
.sort((a, b) => { .sort((a, b) => {
@ -31,33 +40,14 @@ function munge(files) {
} }
export async function GET({ params }) { export async function GET({ params }) {
if (!examples) {
const res = await fetch(`${PUBLIC_API_BASE}/docs/svelte/examples`);
examples = new Set(
(await res.json())
.map((category) => category.examples)
.flat()
.map((example) => example.slug)
);
}
if (examples.has(params.id)) { if (examples.has(params.id)) {
const res = await fetch(`${PUBLIC_API_BASE}/docs/svelte/examples/${params.id}`); const example = get_example(examples_data, params.id);
if (!res.ok) {
return new Response(await res.json(), {
status: res.status,
headers: { 'Content-Type': 'application/json' },
});
}
const example = await res.json();
return json({ return json({
id: params.id, id: params.id,
name: example.name, name: example.title,
owner: null, owner: null,
relaxed: example.relaxed, // TODO is this right? relaxed: false, // TODO is this right? EDIT: It was example.relaxed before, which no example return to my knowledge. By @PuruVJ
components: munge(example.files), components: munge(example.files),
}); });
} }
@ -83,6 +73,7 @@ export async function GET({ params }) {
name: app.name, name: app.name,
owner: app.userid, owner: app.userid,
relaxed: false, relaxed: false,
// @ts-expect-error app.files has a `source` property
components: munge(app.files), components: munge(app.files),
}); });
} }

@ -2,7 +2,6 @@
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import ReplWidget from '$lib/components/ReplWidget.svelte'; import ReplWidget from '$lib/components/ReplWidget.svelte';
/** @type {import('./$types').PageData} */
export let data; export let data;
</script> </script>

@ -0,0 +1,7 @@
import { get_example } from '$lib/server/examples';
import { get_examples_data } from '$lib/server/examples/get-examples';
import { json } from '@sveltejs/kit';
export const GET = async ({ params }) => {
return json(get_example(get_examples_data(), params.slug));
};

@ -16,7 +16,7 @@
let repl; let repl;
const clone = (file) => ({ const clone = (file) => ({
name: file.filename.replace(/.\w+$/, ''), name: file.name.replace(/.\w+$/, ''),
type: file.type, type: file.type,
source: file.content, source: file.content,
}); });

Loading…
Cancel
Save