diff --git a/apps/svelte.dev/src/routes/+layout.server.js b/apps/svelte.dev/src/routes/+layout.server.js deleted file mode 100644 index b3f70a0c59..0000000000 --- a/apps/svelte.dev/src/routes/+layout.server.js +++ /dev/null @@ -1,35 +0,0 @@ -import { fetchBanner } from '@sveltejs/site-kit/components'; - -export const load = async ({ url, fetch }) => { - const [nav_links, banner] = await Promise.all([ - fetch('/nav.json').then((r) => r.json()), - fetchBanner('svelte.dev', fetch) - ]); - - return { - nav_title: get_nav_title(url), - nav_links, - banner - }; -}; - -/** @param {URL} url */ -function get_nav_title(url) { - const list = new Map([ - [/^docs/, 'Docs'], - [/^playground/, 'Playground'], - [/^blog/, 'Blog'], - [/^faq/, 'FAQ'], - [/^tutorial/, 'Tutorial'], - [/^search/, 'Search'], - [/^examples/, 'Examples'] - ]); - - for (const [regex, title] of list) { - if (regex.test(url.pathname.replace(/^\/(.+)/, '$1'))) { - return title; - } - } - - return ''; -} diff --git a/apps/svelte.dev/src/routes/+layout.server.ts b/apps/svelte.dev/src/routes/+layout.server.ts new file mode 100644 index 0000000000..7c8f9cf344 --- /dev/null +++ b/apps/svelte.dev/src/routes/+layout.server.ts @@ -0,0 +1,64 @@ +import { docs, index } from '$lib/server/content'; +import { fetchBanner } from '@sveltejs/site-kit/components'; +import type { NavigationLink } from '@sveltejs/site-kit'; + +const nav_links: NavigationLink[] = [ + { + title: 'Docs', + slug: 'docs', + sections: Object.values(docs.topics) + .map((topic) => ({ + title: topic.metadata.title, + path: '/' + topic.slug, // this will make the UI show a flyout menu for the docs nav entry + sections: topic.children.map((section) => ({ + title: section.metadata.title, + sections: section.children.map((page) => ({ + title: page.metadata.title, + path: '/' + page.slug + })) + })) + })) + .sort((a, b) => a.title.localeCompare(b.title)) // Svelte first + }, + { + title: 'Tutorial', + slug: 'tutorial', + sections: index.tutorial.children.map((topic) => ({ + title: topic.metadata.title, + sections: topic.children.map((section) => ({ + title: section.metadata.title, + sections: section.children.map((page) => ({ + title: page.metadata.title, + path: '/tutorial/' + page.slug.split('/').pop() + })) + })) + })) + }, + { + title: 'Playground', + slug: 'playground' + }, + { + title: 'Blog', + slug: 'blog' + } +]; + +const sections: Record = { + docs: 'Docs', + playground: 'Playground', + blog: 'Blog', + tutorial: 'Tutorial', + search: 'Search' +}; + +export const load = async ({ url, fetch }) => { + const banner = await fetchBanner('svelte.dev', fetch); + const nav_title = sections[url.pathname.split('/')[1]!] ?? ''; + + return { + nav_title, + nav_links, + banner + }; +}; diff --git a/apps/svelte.dev/src/routes/nav.json/+server.ts b/apps/svelte.dev/src/routes/nav.json/+server.ts deleted file mode 100644 index 3fc74d5645..0000000000 --- a/apps/svelte.dev/src/routes/nav.json/+server.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { json } from '@sveltejs/kit'; -import { docs as _docs, index } from '$lib/server/content'; -import type { NavigationLink } from '@sveltejs/site-kit'; - -export const prerender = true; - -export const GET = async () => { - return json(await get_nav_list()); -}; - -async function get_nav_list(): Promise { - const docs = Object.values(_docs.topics) - .map((topic) => ({ - title: topic.metadata.title, - path: '/' + topic.slug, // this will make the UI show a flyout menu for the docs nav entry - sections: topic.children.map((section) => ({ - title: section.metadata.title, - sections: section.children.map((page) => ({ - title: page.metadata.title, - path: '/' + page.slug - })) - })) - })) - .sort((a, b) => a.title.localeCompare(b.title)); // Svelte first - - const tutorial = index.tutorial.children.map((topic) => ({ - title: topic.metadata.title, - sections: topic.children.map((section) => ({ - title: section.metadata.title, - sections: section.children.map((page) => ({ - title: page.metadata.title, - path: '/tutorial/' + page.slug.split('/').pop() - })) - })) - })); - - return [ - { - title: 'Docs', - prefix: 'docs', - pathname: '/docs', - sections: docs - }, - { - title: 'Tutorial', - prefix: 'tutorial', - pathname: '/tutorial', - sections: tutorial - }, - { - title: 'Playground', - prefix: 'playground', - pathname: '/playground' - }, - { - title: 'Blog', - prefix: 'blog', - pathname: '/blog' - } - ]; -} diff --git a/packages/site-kit/src/lib/components/LinksDropdown.svelte b/packages/site-kit/src/lib/components/LinksDropdown.svelte index 0fe4290a52..755967fdcd 100644 --- a/packages/site-kit/src/lib/components/LinksDropdown.svelte +++ b/packages/site-kit/src/lib/components/LinksDropdown.svelte @@ -8,8 +8,8 @@ {link.title} diff --git a/packages/site-kit/src/lib/nav/Menu.svelte b/packages/site-kit/src/lib/nav/Menu.svelte index 99ec67c5a4..16bea444d1 100644 --- a/packages/site-kit/src/lib/nav/Menu.svelte +++ b/packages/site-kit/src/lib/nav/Menu.svelte @@ -14,7 +14,7 @@ open_store.set(true); const segment = get(page).url.pathname.split('/')[1]; - current_menu_view.set(get(links_store).find((link) => link.prefix === segment)); + current_menu_view.set(get(links_store).find((link) => link.slug === segment)); show_context_menu.set(!!get(current_menu_view)?.sections && !!get(current_menu_view)); } @@ -188,7 +188,7 @@
{#each links as link}