/** * Mathlified Custom Page Server version %version% * generated on %date% */ //@ts-expect-error (this line will be removed during injection) import type { PageServerLoad } from './$types'; import { error } from '@sveltejs/kit'; //@ts-expect-error (this line will be removed during injection) import { directory } from '../directory'; //@ts-expect-error (this line will be removed during injection) import { preprocess } from '$lib/server/%collection%'; import { existsSync, readFileSync } from 'node:fs'; import path from 'node:path'; import { normalizePath } from 'vite'; export const prerender = true; //@ts-expect-error (this line will be removed during injection) export const load: PageServerLoad = async ({ params, depends }) => { const slugPath = path.join('%collection%', params.path); let filePath = path.join('src/content', directory[slugPath]); let content: string; let title: string | undefined; if (existsSync(`${filePath}.md`)) { // md file depends('md'); filePath = path.join('./', filePath + '.md'); const file = readFileSync(filePath, 'utf-8'); if (preprocess['md']) { return { data: { ...preprocess.md(file, { paramsPath: params.path, filePath }), isMd: true }, filePath }; } // default md behavior const { metadata, body } = extractFrontmatter(readFileSync(filePath, 'utf-8')); if (metadata.title) { title = metadata.title; content = `# ${metadata.title}\n\n${body}`; } else { title = params.path.split(path.sep).pop(); content = body; } title = title = title ? title[0].toLocaleUpperCase() + title.slice(1) : undefined; // 1,2a) prettier workaround: _{} gets converted to \_{}, so we have to change it back in math // 1,2b) go from tex $x$ to $`x` djot syntax // 3) put punctuation in math inline to prevent awkward line breaks // 4) table alignment: prettier-markdown to djot syntax // 5) change $ content = content .replace( /(? `$$\`${match.replaceAll('\\_', '_')}\`` ) .replace( /(? `$\`${match.replaceAll('\\_', '_')}\`` ) .replace(/(? `$$\`${match.replaceAll('\\_', '_')}\``) .replace(/(? `$\`${match.replaceAll('\\_', '_')}\``) .replace(/(? = {}; frontmatter.split('\n').forEach((pair) => { const i = pair.indexOf(':'); metadata[pair.slice(0, i).trim()] = removeQuotes(pair.slice(i + 1).trim()); }); return { metadata, body }; } function removeQuotes(str: string) { return str.replace(/(^["']|["']$)/g, ''); }