Skip to content

Commit

Permalink
throw error if pages conflict with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Oct 2, 2024
1 parent ad8bae9 commit a9356b8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions apps/svelte.dev/src/lib/server/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Document>;
Expand All @@ -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: []
});
Expand All @@ -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}/`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion apps/svelte.dev/src/routes/docs/[...path]/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a9356b8

Please sign in to comment.