|
1 |
| -import { index, remove_section } from '$lib/server/content'; |
| 1 | +import { remove_section } from '$lib/server/content'; |
2 | 2 | import { error, redirect } from '@sveltejs/kit';
|
3 | 3 |
|
4 | 4 | // All links to warnings/errors from the Svelte compiler/runtime go through this page in order to have stable references
|
5 | 5 | // (i.e. we can move codes between warnings/errors/different pages or even adjust codes without breaking links).
|
6 |
| -const reference = index['docs/svelte/reference'].children.filter( |
7 |
| - (child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings') |
8 |
| -); |
9 | 6 |
|
10 |
| -export const prerender = true; |
11 |
| - |
12 |
| -export function entries() { |
13 |
| - return reference.flatMap((page) => |
14 |
| - [...page.body.matchAll(/(^|\n)### (\w+)/g)].map(([, , code]) => ({ code })) |
| 7 | +// Right now we can't prerender this because we would hit a "too many routes" error on Vercel, |
| 8 | +// for which we need to implement https://github.com/sveltejs/kit/issues/9032 |
| 9 | +// const reference = index['docs/svelte/reference'].children.filter( |
| 10 | +// (child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings') |
| 11 | +// ); |
| 12 | +// |
| 13 | +// export const prerender = true; |
| 14 | +// |
| 15 | +// export function entries() { |
| 16 | +// return reference.flatMap((page) => |
| 17 | +// [...page.body.matchAll(/(^|\n)### (\w+)/g)].map(([, , code]) => ({ code })) |
| 18 | +// ); |
| 19 | +// } |
| 20 | + |
| 21 | +export async function load({ params, fetch }) { |
| 22 | + const codes: Record<string, Record<string, string[]>> = await fetch('/e/codes.json').then((r) => |
| 23 | + r.json() |
15 | 24 | );
|
16 |
| -} |
17 |
| - |
18 |
| -export function load({ params }) { |
19 |
| - // Since codes are not top level section we gotta jump through some hoops to get the right hash |
20 |
| - for (const page of reference) { |
21 |
| - const idx = page.body.indexOf(`### ${params.code}`); |
22 |
| - if (idx === -1) continue; |
23 | 25 |
|
24 |
| - const slug_idx = page.body.lastIndexOf('\n## ', idx); |
25 |
| - |
26 |
| - if (slug_idx > 0 || page.body.startsWith(`## `)) { |
27 |
| - const title = page.body.slice(slug_idx + 4, page.body.indexOf('\n', slug_idx + 4)).trim(); |
28 |
| - const slug = page.sections.find((s) => s.title === title)?.slug; |
29 |
| - |
30 |
| - if (!slug) { |
31 |
| - throw new Error( |
32 |
| - `Internal Error: Could not find previous title for code ${params.code} on ${page.slug}` |
33 |
| - ); |
| 26 | + for (const url of Object.keys(codes)) { |
| 27 | + const page = codes[url]; |
| 28 | + for (const [h2, h3] of Object.entries(page)) { |
| 29 | + if (h3.includes(params.code)) { |
| 30 | + if (h2) { |
| 31 | + redirect(307, `/${remove_section(url)}#${h2}-${params.code}`); |
| 32 | + } else { |
| 33 | + redirect(307, `/${remove_section(url)}#${params.code}`); |
| 34 | + } |
34 | 35 | }
|
35 |
| - |
36 |
| - redirect(307, `/${remove_section(page.slug)}#${slug}-${params.code}`); |
37 |
| - } else { |
38 |
| - redirect(307, `/${remove_section(page.slug)}#${params.code}`); |
39 | 36 | }
|
40 | 37 | }
|
41 | 38 |
|
|
0 commit comments