diff --git a/src/lib/utils/getCategories.ts b/src/lib/utils/getCategories.ts new file mode 100644 index 0000000..0a5fc41 --- /dev/null +++ b/src/lib/utils/getCategories.ts @@ -0,0 +1,85 @@ +// import type { Category } from '$lib/types/service'; +// import fs from 'fs'; +// import path from 'path'; + +// export const getCategories = async (): Promise => { +// const contentDir = await getContentDir(); +// const categories: Category[] = []; + +// for (const categoryDir of contentDir.dirs) { +// const categoryData = await import(`../content/${categoryDir.name}/category.json`); +// const services = await Promise.all( +// categoryDir.files +// .filter((file) => file.name.endsWith('.md')) +// .map(async (file) => { +// const { metadata, content } = await getMetadata(file.content); +// return { +// ...metadata, +// content, +// }; +// }) +// ); + +// categories.push({ +// ...categoryData.default, +// services, +// }); +// } + +// return categories; +// }; + +// // ----------------- +// const contentPath: string = path.join(process.cwd(), 'src', 'content'); + +// const getCategories = async ( contentPath: string ): Promise => { +// try { +// // read directories - entries - in contentPath +// const entries = await fs.promises.readdir( contentPath ); + +// const categoryDirs = await Promise.all( +// entries.map(async (entry) => { +// // /src/content/example-service +// const entryPath = path.join(contentPath, entry); +// // file statistict +// const stats = fs.promises.stat(entryPath); + +// // Ensure stats is an object before accessing properties +// if (typeof stats === 'object' && stats !== null && 'isDirectory' in stats) { +// return +// } + + +// return +// }) +// return +// } + + +import type { Category, Service } from '$lib/types/service'; +import fs from 'fs'; +import path from 'path'; + +export const getCategories = async (): Promise => { + const contentDir = path.join(process.cwd(), 'src', 'content'); + const categoryDirs = fs.readdirSync(contentDir, { withFileTypes: true }) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name); + + const categories: Category[] = await Promise.all( + categoryDirs.map(async categoryId => { + const categoryJsonPath = path.join(contentDir, categoryId, `${categoryId}.json`); + const categoryData = JSON.parse(fs.readFileSync(categoryJsonPath, 'utf-8')); + + return { + ...categoryData, + services: categoryData.services.map((service: Service) => ({ + ...service, + id: `${categoryId}/${service.id}`, + })), + }; + }) + ); + + return categories; +}; diff --git a/src/routes/sluzby/[categoryId]/[serviceId]/+page.server.ts b/src/routes/sluzby/[categoryId]/[serviceId]/+page.server.ts new file mode 100644 index 0000000..8084aaf --- /dev/null +++ b/src/routes/sluzby/[categoryId]/[serviceId]/+page.server.ts @@ -0,0 +1,62 @@ +// import type { PageServerLoad } from './$types'; +// import path from 'path'; +// import fs from 'fs'; +// import { error } from '@sveltejs/kit'; +// import { fetchServiceData } from '$lib/utils/fetchServiceData'; + +// export const load: PageServerLoad = async ({ params }) => { +// const { categoryId, serviceId } = params; + +// try { +// const serviceData = await fetchServiceData(categoryId, serviceId); + +// return { +// service: serviceData +// }; +// } catch (err) { +// throw error(404, `Service ${serviceId} not found`); +// } +// }; + +// src/routes/[categoryId]/[serviceId]/+page.server.ts +import { error } from '@sveltejs/kit'; +import { getCategories } from '$lib/utils/getCategories'; + +export const load = async ({ params }) => { + const { categoryId, serviceId } = params; + const categories = await getCategories(); + const category = categories.find(cat => cat.id === categoryId); + + if (!category) { + throw error(404, 'Category not found'); + } + + const service = category.services.find(serv => serv.id === `${categoryId}/${serviceId}`); + + if (!service) { + throw error(404, 'Service not found'); + } + // mdsvex + const {default: content, metadata: frontmatter} = await import(`$content/${categoryId}/${serviceId}.md`) + + const seoData = { + title: service.title, // Assuming title exists in service data + description: service.description, + id: service.id, + image: service.image, + price: service.price, + duration: service.duration, + // ... (populate other optional fields) + frontmatter: frontmatter, // Assuming metadata contains date and tags +}; + + return { + category, + service: { + ...service, + content, + frontmatter + }, + seoData + }; +};