From a9356b8b1ee7caa2f59e9c5231312f8e408e269c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 2 Oct 2024 10:36:34 -0400 Subject: [PATCH] throw error if pages conflict with each other --- .../98-reference/{01-state.md => 01-$state.md} | 0 apps/svelte.dev/src/lib/server/content.ts | 13 +++++++------ .../src/routes/docs/[...path]/+layout.server.ts | 2 +- .../src/routes/docs/[...path]/+page.server.js | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) rename apps/svelte.dev/content/docs/svelte/98-reference/{01-state.md => 01-$state.md} (100%) diff --git a/apps/svelte.dev/content/docs/svelte/98-reference/01-state.md b/apps/svelte.dev/content/docs/svelte/98-reference/01-$state.md similarity index 100% rename from apps/svelte.dev/content/docs/svelte/98-reference/01-state.md rename to apps/svelte.dev/content/docs/svelte/98-reference/01-$state.md diff --git a/apps/svelte.dev/src/lib/server/content.ts b/apps/svelte.dev/src/lib/server/content.ts index 3562d9686..6a02ca613 100644 --- a/apps/svelte.dev/src/lib/server/content.ts +++ b/apps/svelte.dev/src/lib/server/content.ts @@ -60,10 +60,6 @@ function create_docs() { return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1'); } - function remove_docs(slugs: string) { - return slugs.replace(/^docs\//, ''); - } - let docs: { /** The top level entries/packages: svelte/kit/etc. Key is the topic */ topics: Record; @@ -74,7 +70,7 @@ function create_docs() { for (const topic of index.docs.children) { const pkg = topic.slug.split('/')[1]; const sections = topic.children; - const transformed_topic: Document = (docs.topics[remove_docs(topic.slug)] = { + const transformed_topic: Document = (docs.topics[topic.slug] = { ...topic, children: [] }); @@ -90,7 +86,12 @@ function create_docs() { for (const page of pages) { const slug = remove_section(page.slug); - const transformed_page: Document = (docs.pages[remove_docs(slug)] = { + + if (Object.hasOwn(docs.pages, slug)) { + throw new Error(`${docs.pages[slug].file} conflicts with ${page.file}`); + } + + const transformed_page: Document = (docs.pages[slug] = { ...page, slug, next: page.next?.slug.startsWith(`docs/${pkg}/`) diff --git a/apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts b/apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts index 065cc7249..739c6e5d2 100644 --- a/apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts +++ b/apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts @@ -6,7 +6,7 @@ export const prerender = true; export async function load({ params }) { const topic = params.path.split('/')[0]; - const document = docs.topics[topic]; + const document = docs.topics[`docs/${topic}`]; if (!document) { error(404, 'Not found'); diff --git a/apps/svelte.dev/src/routes/docs/[...path]/+page.server.js b/apps/svelte.dev/src/routes/docs/[...path]/+page.server.js index ff0def3ee..286d0d0d8 100644 --- a/apps/svelte.dev/src/routes/docs/[...path]/+page.server.js +++ b/apps/svelte.dev/src/routes/docs/[...path]/+page.server.js @@ -3,7 +3,7 @@ import { render_content } from '$lib/server/renderer'; import { error } from '@sveltejs/kit'; export async function load({ params }) { - const document = docs.pages[params.path]; + const document = docs.pages[`docs/${params.path}`]; if (!document) { error(404);