mirror of https://github.com/sveltejs/svelte
commit
99b1d6fa26
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,13 @@
|
|||||||
|
import { get_examples_data } from '../src/lib/server/examples/get-examples.js';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
|
||||||
|
const examples_data = get_examples_data(
|
||||||
|
new URL('../../../site/content/examples', import.meta.url).pathname
|
||||||
|
);
|
||||||
|
|
||||||
|
fs.mkdirSync(new URL('../src/lib/generated/', import.meta.url), { recursive: true });
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
new URL('../src/lib/generated/examples-data.js', import.meta.url),
|
||||||
|
`export default ${JSON.stringify(examples_data)}`
|
||||||
|
);
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
// @ts-check
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import { extract_frontmatter } from '../markdown';
|
||||||
|
|
||||||
|
const base = '../../site/content/faq';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {import('./types').FAQData}
|
||||||
|
*/
|
||||||
|
export function get_faq_data() {
|
||||||
|
const faqs = [];
|
||||||
|
|
||||||
|
for (const file of fs.readdirSync(base)) {
|
||||||
|
const { metadata, body } = extract_frontmatter(fs.readFileSync(`${base}/${file}`, 'utf-8'));
|
||||||
|
|
||||||
|
faqs.push({
|
||||||
|
title: metadata.question, // Initialise with empty
|
||||||
|
slug: file.split('-').slice(1).join('-').replace('.md', ''),
|
||||||
|
content: body,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return faqs;
|
||||||
|
}
|
||||||
@ -0,0 +1,87 @@
|
|||||||
|
// @ts-check
|
||||||
|
import { createShikiHighlighter } from 'shiki-twoslash';
|
||||||
|
import { transform } from '../markdown';
|
||||||
|
|
||||||
|
const languages = {
|
||||||
|
bash: 'bash',
|
||||||
|
env: 'bash',
|
||||||
|
html: 'svelte',
|
||||||
|
svelte: 'svelte',
|
||||||
|
sv: 'svelte',
|
||||||
|
js: 'javascript',
|
||||||
|
css: 'css',
|
||||||
|
diff: 'diff',
|
||||||
|
ts: 'typescript',
|
||||||
|
'': '',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import('./types').FAQData} faq_data
|
||||||
|
*/
|
||||||
|
export async function get_parsed_faq(faq_data) {
|
||||||
|
const highlighter = await createShikiHighlighter({ theme: 'css-variables' });
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
faq_data.map(async ({ content, slug, title }) => {
|
||||||
|
return {
|
||||||
|
title,
|
||||||
|
slug,
|
||||||
|
content: transform(content, {
|
||||||
|
/**
|
||||||
|
* @param {string} html
|
||||||
|
*/
|
||||||
|
heading(html) {
|
||||||
|
const title = html
|
||||||
|
.replace(/<\/?code>/g, '')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>');
|
||||||
|
|
||||||
|
return title;
|
||||||
|
},
|
||||||
|
code: (source, language) => {
|
||||||
|
let html = '';
|
||||||
|
|
||||||
|
source = source
|
||||||
|
.replace(/^([\-\+])?((?: )+)/gm, (match, prefix = '', spaces) => {
|
||||||
|
if (prefix && language !== 'diff') return match;
|
||||||
|
|
||||||
|
// for no good reason at all, marked replaces tabs with spaces
|
||||||
|
let tabs = '';
|
||||||
|
for (let i = 0; i < spaces.length; i += 4) {
|
||||||
|
tabs += ' ';
|
||||||
|
}
|
||||||
|
return prefix + tabs;
|
||||||
|
})
|
||||||
|
.replace(/\*\\\//g, '*/');
|
||||||
|
|
||||||
|
html = highlighter.codeToHtml(source, { lang: languages[language] });
|
||||||
|
|
||||||
|
html = html
|
||||||
|
.replace(
|
||||||
|
/^(\s+)<span class="token comment">([\s\S]+?)<\/span>\n/gm,
|
||||||
|
(match, intro_whitespace, content) => {
|
||||||
|
// we use some CSS trickery to make comments break onto multiple lines while preserving indentation
|
||||||
|
const lines = (intro_whitespace + content + '').split('\n');
|
||||||
|
return lines
|
||||||
|
.map((line) => {
|
||||||
|
const match = /^(\s*)(.*)/.exec(line);
|
||||||
|
const indent = (match?.[1] ?? '').replace(/\t/g, ' ').length;
|
||||||
|
|
||||||
|
return `<span class="token comment wrapped" style="--indent: ${indent}ch">${
|
||||||
|
line ?? ''
|
||||||
|
}</span>`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.replace(/\/\*…\*\//g, '…');
|
||||||
|
|
||||||
|
return html;
|
||||||
|
},
|
||||||
|
codespan: (text) => '<code>' + text + '</code>',
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
export type FAQData = {
|
||||||
|
title: string;
|
||||||
|
slug: string;
|
||||||
|
content: string;
|
||||||
|
}[];
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
import * as gist from '$lib/db/gist';
|
||||||
|
import * as session from '$lib/db/session';
|
||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
// TODO reimplement as an action
|
||||||
|
export async function PUT({ params, request }) {
|
||||||
|
const user = await session.from_cookie(request.headers.get('cookie'));
|
||||||
|
if (!user) throw error(401, 'Unauthorized');
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
await gist.update(user, params.id, body);
|
||||||
|
|
||||||
|
return new Response(undefined, { status: 204 });
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
// @ts-check
|
||||||
|
import examples_data from '$lib/generated/examples-data.js';
|
||||||
|
import { get_examples_list } from '$lib/server/examples/get-examples';
|
||||||
|
import { json } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
export const GET = () => {
|
||||||
|
return json(get_examples_list(examples_data));
|
||||||
|
};
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
import examples_data from '$lib/generated/examples-data.js';
|
||||||
|
import { get_example } from '$lib/server/examples';
|
||||||
|
import { get_examples_list } from '$lib/server/examples/get-examples';
|
||||||
|
import { error, json } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
export const GET = ({ params }) => {
|
||||||
|
const examples = new Set(
|
||||||
|
get_examples_list(examples_data)
|
||||||
|
.map((category) => category.examples)
|
||||||
|
.flat()
|
||||||
|
.map((example) => example.slug)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!examples.has(params.slug)) throw error(404, 'Example not found');
|
||||||
|
|
||||||
|
return json(get_example(examples_data, params.slug));
|
||||||
|
};
|
||||||
@ -1,12 +0,0 @@
|
|||||||
import { PUBLIC_API_BASE } from '$env/static/public';
|
|
||||||
|
|
||||||
/** @type {import('./$types').PageLoad} */
|
|
||||||
export async function load({ fetch, setHeaders }) {
|
|
||||||
const faqs = await fetch(`${PUBLIC_API_BASE}/docs/svelte/faq?content`).then((r) => r.json());
|
|
||||||
|
|
||||||
setHeaders({
|
|
||||||
'cache-control': 'public, max-age=60'
|
|
||||||
});
|
|
||||||
|
|
||||||
return { faqs };
|
|
||||||
}
|
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
import { get_parsed_faq } from '$lib/server/faq';
|
||||||
|
import { get_faq_data } from '$lib/server/faq/get-faq';
|
||||||
|
|
||||||
|
export const prerender = true;
|
||||||
|
|
||||||
|
export async function load() {
|
||||||
|
return { faqs: get_parsed_faq(get_faq_data()) };
|
||||||
|
}
|
||||||
@ -1,9 +1,31 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import adapter from '@sveltejs/adapter-auto';
|
import adapter from '@sveltejs/adapter-vercel';
|
||||||
|
import { get_examples_data, get_examples_list } from './src/lib/server/examples/get-examples.js';
|
||||||
|
import { get_tutorial_data, get_tutorial_list } from './src/lib/server/tutorial/get-tutorial.js';
|
||||||
|
|
||||||
/** @type {import('@sveltejs/kit').Config} */
|
/** @type {import('@sveltejs/kit').Config} */
|
||||||
export default {
|
export default {
|
||||||
kit: {
|
kit: {
|
||||||
adapter: adapter()
|
adapter: adapter(),
|
||||||
|
prerender: {
|
||||||
|
// TODO use route entries instead, once https://github.com/sveltejs/kit/pull/9571 is merged
|
||||||
|
entries: ['*', ...repl_json_entries(), ...tutorial_entries()]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function repl_json_entries() {
|
||||||
|
return get_examples_list(
|
||||||
|
get_examples_data(new URL('../../site/content/examples', import.meta.url).pathname)
|
||||||
|
).flatMap(({ examples }) =>
|
||||||
|
examples.map(({ slug }) => /** @type {(`/${string}`)} */ (`/repl/${slug}.json`))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function tutorial_entries() {
|
||||||
|
return get_tutorial_list(
|
||||||
|
get_tutorial_data(new URL('../../site/content/tutorial', import.meta.url).pathname)
|
||||||
|
).flatMap(({ tutorials }) =>
|
||||||
|
tutorials.map(({ slug }) => /** @type {(`/${string}`)} */ (`/tutorial/${slug}`))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue