From d73bac59a89ee79c2fd4746a172bdb41d0045cc1 Mon Sep 17 00:00:00 2001 From: matthieu42morin Date: Sun, 31 Mar 2024 05:39:48 +0200 Subject: [PATCH] data pages for listing everything json, each category and each service under a category, bordel de merde for the time being --- src/routes/sluzby/+page.server.ts | 20 +++ src/routes/sluzby/[category]/+page.ts | 31 ++++ .../sluzby/[category]/[service]/+page.ts | 134 ++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 src/routes/sluzby/+page.server.ts create mode 100644 src/routes/sluzby/[category]/+page.ts create mode 100644 src/routes/sluzby/[category]/[service]/+page.ts diff --git a/src/routes/sluzby/+page.server.ts b/src/routes/sluzby/+page.server.ts new file mode 100644 index 0000000..10b0978 --- /dev/null +++ b/src/routes/sluzby/+page.server.ts @@ -0,0 +1,20 @@ +import { PageServerLoad } from './$types'; +import { readJsonFile } from '$lib/utils/formatParse'; + +export const load: PageServerLoad = async () => { + const categoriesDir = path.join(process.cwd(), 'src', 'content'); + const categoryDirs = fs.readdirSync(categoriesDir).filter(dir => fs.lstatSync(path.join(categoriesDir, dir)).isDirectory()); + + const services = categoryDirs.map(categoryDir => { + const category = readJsonFile(`${categoryDirs}/${categoryDir}.json`); + return { category: categoryDir, items: category }; + }); + + return { props: { services } }; +} + +// export const load: = async () => { +// return { +// posts: listPosts() +// }; +// }; diff --git a/src/routes/sluzby/[category]/+page.ts b/src/routes/sluzby/[category]/+page.ts new file mode 100644 index 0000000..2e65dfc --- /dev/null +++ b/src/routes/sluzby/[category]/+page.ts @@ -0,0 +1,31 @@ +import type { ExtendedCategory } from '$lib/types'; + +import { promises as fs } from 'fs'; +import path from 'path'; +import { parseMarkdown } from '$lib/markdownParser'; +import type { ExtendedCategory } from '$lib/types'; + +export async function get({ params }) { + const { category, service } = params; + + // Read the JSON file for the service + const jsonPath = path.join('src', 'content', category, service, `${service}.json`); + const jsonContent = await fs.readFile(jsonPath, 'utf-8'); + const serviceData = JSON.parse(jsonContent); + + // Read the markdown file for the service + const markdownPath = path.join('src', 'content', category, service, `${service}.md`); + const markdownContent = await fs.readFile(markdownPath, 'utf-8'); + const { frontmatter, headings } = parseMarkdown(markdownContent); + + // Combine the service data with the frontmatter and headings to create the extended service + const ExtendedCategory: ExtendedCategory = { + ...serviceData, + ...frontmatter, + headings, + }; + + return { + body: ExtendedCategory, + }; +}; diff --git a/src/routes/sluzby/[category]/[service]/+page.ts b/src/routes/sluzby/[category]/[service]/+page.ts new file mode 100644 index 0000000..711c940 --- /dev/null +++ b/src/routes/sluzby/[category]/[service]/+page.ts @@ -0,0 +1,134 @@ + +// import type { PageLoad } from './$types'; +// import servicesData from '$lib/services.json'; // Assuming you have a JSON file with all the data + +// export const load: PageLoad = async ({ params }) => { +// const { category, service } = params; +// const serviceItem = servicesData.find((s) => s.category === category)?.items.find((item) => item.id === service); + +// if (!serviceItem) { +// throw new Error('Service not found'); +// } + +// return { +// props: { +// openGraphData: { +// title: serviceItem.title, +// description: serviceItem.description, +// image: serviceItem.image, +// // ...other OpenGraph data... +// }, +// }, +// }; +// }; +// import { promises as fs } from 'fs'; +// import path from 'path'; +// import type { PageServerLoad } from './$types'; +// import { parseMarkdown } from '$lib/markdownParser'; // You'll need to create this +// import servicesData from '$lib/services.json'; +// import type { ExtendedServiceItem, MarkdownFrontmatter } from '$lib/types'; + +// export const load: PageServerLoad = async ({ params }) => { +// const { category, service } = params; + +// // Read the markdown file for the service with grey matter +// const markdownPath = path.join('src', 'content', 'posts', category, service, 'content.md'); +// const markdownContent = await fs.readFile(markdownPath, 'utf-8'); +// const { frontmatter } = parseMarkdown(markdownContent); + +// // Find the service item in the JSON data +// const serviceItem = servicesData.find((s) => s.category === category)?.items.find((item) => item.id === service); + +// if (!serviceItem) { +// throw new Error('Service not found'); +// } + +// // Combine the service item with the frontmatter to create the extended service item +// const extendedServiceItem: ExtendedServiceItem = { +// ...serviceItem, +// ...frontmatter, +// }; + +// return { +// props: { +// openGraphData: extendedServiceItem, +// }, +// }; +// }; + + + +import { promises as fs } from 'fs'; +import path from 'path'; +import * as conf from '$lib/config' +import { readJsonFile, parseMarkdownFile } from '$lib/utils'; +import { parseMarkdown } from '$lib/markdownParser'; +import type { ExtendedService } from '$lib/types'; + +export const load:PageLoad = async ({ params }) => { + const { category, service } = params; + + // Read the JSON file for the service + const categoryJSON = readJsonFile(`${params.category}/${params.category}.json`); + const serviceData = categoryJSON.find(service => service.id === params.service); + + if (!service) { + throw new Error(`Service not found: ${params.service}`); + } + + // Read the markdown file for the service + const markdownData = parseMarkdownFile(`${params.category}/${params.service}/${params.service}.md`); + const markdownPath = path.join('src', 'content', category, service, `${service}.md`); + const markdownContent = await fs.readFile(markdownPath, 'utf-8'); + const frontmatter = parseMarkdown(markdownContent); + // headings here instead of remark plugin custom? + // Combine the service data with the frontmatter and headings to create the extended service + const extendedService: ExtendedService = { + ...serviceData, + ...frontmatter, + + }; + + return { + props: { + extendedService, + og: { + title: extendedService.title, + type: 'article', + image: extendedService.image, + url: `https://yourwebsite.com/services/${params.category}/${params.service}`, + description: extendedService.description, + published_time: extendedService.frontmatter.date, + tags: extendedService.frontmatter.tags + }} + }; +}; + + + +export async function load({ params }) { + const category = readJsonFile(`${params.category}/pmu.json`); + const service = category.services.find(service => service.id === params.service); + + if (!service) { + throw new Error(`Service not found: ${params.service}`); + } + + const markdownData = parseMarkdownFile(`${params.category}/${params.service}/${params.service}.md`); + const post = { ...service, ...markdownData }; + + return { + props: { + post, + og: { + title: post.title, + type: 'article', + image: post.image, + url: `${conf.url}/${params.category}/${params.service}`, + description: post.description, + published_time: post.frontmatter.date, + tags: post.frontmatter.tags + } + } + }; +}