mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
⚙️ Create utility to get categories + fix prerender function with `entries`
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user