diff --git a/src/data/index.ts b/src/data/index.ts index 54092b2..3710562 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -3,4 +3,21 @@ import { svgs } from './svgs'; export const svgsData = svgs.map((svg: iSVG, index: number) => { return { id: index, ...svg }; -}); \ No newline at end of file +}); + +export const getCategories = () => { + const categories = svgs + .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) + .filter((category, index, array) => array.indexOf(category) === index); + return categories; +}; + +export const getCategoriesForDirectory = () => { + const categories = svgs + .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) + .filter((category, index, array) => array.indexOf(category) === index) + .map((category) => ({ + slug: category.toLowerCase() + })); + return categories; +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 8ee90e2..8b38947 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -11,9 +11,7 @@ // Get categories: import { svgs } from '@/data/svgs'; - const categories = svgs - .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) - .filter((category, index, array) => array.indexOf(category) === index); + const categories = getCategories(); // Get category counts: let categoryCounts: Record = {}; @@ -30,6 +28,7 @@ // Layout: import Navbar from '@/components/navbar.svelte'; + import { getCategories } from '@/data'; @@ -38,22 +37,22 @@ -
+
diff --git a/src/routes/directory/[slug]/+page.ts b/src/routes/directory/[slug]/+page.ts index eb91b6b..595a5ac 100644 --- a/src/routes/directory/[slug]/+page.ts +++ b/src/routes/directory/[slug]/+page.ts @@ -1,35 +1,34 @@ -import { error } from '@sveltejs/kit'; -import type { PageLoad } from './$types'; - -import { svgs } from '@/data/svgs'; +import type { PageLoad, EntryGenerator } from './$types'; import type { iSVG } from '@/types/svg'; +import { error } from '@sveltejs/kit'; +import { svgs } from '@/data/svgs'; +import { getCategoriesForDirectory } from '@/data'; + +export const entries: EntryGenerator = () => { + const categories = getCategoriesForDirectory(); + return categories; +}; + export const prerender = true; export const load = (async ({ params }) => { const { slug } = params; - // Check if slug is valid: - if (!slug) { - return error(404, 'Not found'); - } - - // Filter out the svg with the matching slug: const svgsByCategory = svgs.filter((svg: iSVG) => { if (Array.isArray(svg.category)) { - return svg.category.some((categoryItem) => categoryItem.toLowerCase() === slug); + return svg.category.some((categoryItem) => categoryItem.toLowerCase() === slug.toLowerCase()); } else { - return svg.category.toLowerCase() === slug; + return svg.category.toLowerCase() === slug.toLowerCase(); } }); - // If SVGs array is empty, category can't exist if (svgsByCategory.length === 0) { - return error(404, 'Not found'); + throw error(404, 'Category not found'); } return { - category: slug as string, + category: slug, svgs: svgsByCategory }; }) satisfies PageLoad;