Skip to content

Commit bd4cd06

Browse files
authored
feat: shorter docs URLs (#73)
Removes the section part from the slugs Fixes #62
1 parent 9466024 commit bd4cd06

File tree

5 files changed

+75
-31
lines changed

5 files changed

+75
-31
lines changed

apps/svelte.dev/src/lib/server/content.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { read } from '$app/server';
2+
import type { Document } from '@sveltejs/site-kit';
23
import { create_index } from '@sveltejs/site-kit/server/content';
34

45
const documents = import.meta.glob<string>('../../../content/**/*.md', {
@@ -51,3 +52,64 @@ export const blog_posts = index.blog.children
5152
};
5253
})
5354
.sort((a, b) => (a.date < b.date ? 1 : -1));
55+
56+
/**
57+
* Create docs index, which is basically the same structure as the original index
58+
* but with adjusted slugs (the section part is omitted for cleaner URLs), separated
59+
* topics/pages and next/prev adjusted so that they don't point to different topics.
60+
*/
61+
function create_docs() {
62+
function remove_section(slug: string) {
63+
return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1');
64+
}
65+
66+
function remove_docs(slugs: string) {
67+
return slugs.replace(/^docs\//, '');
68+
}
69+
70+
let docs: {
71+
/** The top level entries/packages: svelte/kit/etc. Key is the topic */
72+
topics: Record<string, Document>;
73+
/** The docs pages themselves. Key is the topic + page */
74+
pages: Record<string, Document>;
75+
} = { topics: {}, pages: {} };
76+
77+
for (const topic of index.docs.children) {
78+
const pkg = topic.slug.split('/')[1];
79+
const sections = topic.children;
80+
const transformed_topic: Document = (docs.topics[remove_docs(topic.slug)] = {
81+
...topic,
82+
children: []
83+
});
84+
85+
for (const section of sections) {
86+
const pages = section.children;
87+
const transformed_section: Document = {
88+
...section,
89+
children: []
90+
};
91+
92+
transformed_topic.children.push(transformed_section);
93+
94+
for (const page of pages) {
95+
const slug = remove_section(page.slug);
96+
const transformed_page: Document = (docs.pages[remove_docs(slug)] = {
97+
...page,
98+
slug,
99+
next: page.next?.slug.startsWith(`docs/${pkg}/`)
100+
? { slug: remove_section(page.next.slug), title: page.next.title }
101+
: null,
102+
prev: page.prev?.slug.startsWith(`docs/${pkg}/`)
103+
? { slug: remove_section(page.prev.slug), title: page.prev.title }
104+
: null
105+
});
106+
107+
transformed_section.children.push(transformed_page);
108+
}
109+
}
110+
}
111+
112+
return docs;
113+
}
114+
115+
export const docs = create_docs();

apps/svelte.dev/src/routes/content.json/+server.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { index } from '$lib/server/content';
1+
import { index, docs as _docs } from '$lib/server/content';
22
import { json } from '@sveltejs/kit';
33
import { markedTransform, normalizeSlugify, removeMarkdown } from '@sveltejs/site-kit/markdown';
44
import type { Block } from '@sveltejs/site-kit/search';
@@ -18,11 +18,7 @@ function get_href(parts: string[]) {
1818
async function content() {
1919
const blocks: Block[] = [];
2020
const breadcrumbs: string[] = [];
21-
// We want the actual contents: docs -> docs/svelte etc -> docs/svelte/overview etc -> docs/svelte/overview/introduction etc
22-
let docs = index.docs.children.flatMap((topic) =>
23-
topic.children.flatMap((section) => section.children)
24-
);
25-
docs = docs.concat(
21+
const docs = Object.values(_docs.pages).concat(
2622
index.tutorial.children.flatMap((topic) =>
2723
topic.children.flatMap((section) =>
2824
section.children.map((entry) => ({

apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { index } from '$lib/server/content';
1+
import { docs } from '$lib/server/content';
22
import { error } from '@sveltejs/kit';
33

44
export const prerender = true;
55

66
export async function load({ params }) {
7-
const page = index[`docs/${params.path.split('/')[0]}`];
7+
const page = docs.topics[params.path.split('/')[0]];
88

99
if (!page) {
1010
error(404, 'Not found');
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
1-
import { index } from '$lib/server/content';
1+
import { docs } from '$lib/server/content';
22
import { render_content } from '$lib/server/renderer';
33
import { error, redirect } from '@sveltejs/kit';
44

55
export async function load({ params }) {
6-
const document = index[`docs/${params.path}`];
6+
const document = docs.pages[params.path];
77

88
if (!document) {
9-
error(404);
10-
}
11-
12-
if (!document.body) {
13-
let child = document;
14-
15-
while (child.children[0]) {
16-
child = child.children[0];
9+
const topic = docs.topics[params.path];
10+
if (topic) {
11+
redirect(307, `/${topic.children[0].children[0].slug}`);
1712
}
18-
19-
if (child === document) {
20-
error(404);
21-
}
22-
23-
redirect(307, `/${child.slug}`);
13+
error(404);
2414
}
2515

26-
const pkg = params.path.split('/')[0];
27-
2816
return {
2917
document: {
3018
...document,
31-
body: await render_content(document.file, document.body),
32-
prev: document.prev?.slug.startsWith(`docs/${pkg}/`) ? document.prev : null,
33-
next: document.next?.slug.startsWith(`docs/${pkg}/`) ? document.next : null
19+
body: await render_content(document.file, document.body)
3420
}
3521
};
3622
}

apps/svelte.dev/src/routes/nav.json/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { json } from '@sveltejs/kit';
2-
import { blog_posts, index } from '$lib/server/content';
2+
import { blog_posts, docs as _docs, index } from '$lib/server/content';
33
import type { NavigationLink } from '@sveltejs/site-kit';
44

55
export const prerender = true;
@@ -9,7 +9,7 @@ export const GET = async () => {
99
};
1010

1111
async function get_nav_list(): Promise<NavigationLink[]> {
12-
const docs = index.docs.children.map((topic) => ({
12+
const docs = Object.values(_docs.topics).map((topic) => ({
1313
title: topic.metadata.title,
1414
sections: topic.children.map((section) => ({
1515
title: section.metadata.title,

0 commit comments

Comments
 (0)