From 1922ee58ecb288b012c3b1a7f590d8c767fc18e6 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 24 Sep 2025 05:09:46 +0200 Subject: [PATCH] maybe docs works? --- src/routes/api/docs/[slug]/+server.ts | 49 +++++++++++++-------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/routes/api/docs/[slug]/+server.ts b/src/routes/api/docs/[slug]/+server.ts index f53d0cb..bc2ffa9 100644 --- a/src/routes/api/docs/[slug]/+server.ts +++ b/src/routes/api/docs/[slug]/+server.ts @@ -1,14 +1,10 @@ import { error } from '@sveltejs/kit'; -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; -// Get the directory path -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -// Path to markdown files -const DOCS_PATH = path.resolve(__dirname, '../../../../lib/taDocs'); +// Import all markdown files at build time +const docs = import.meta.glob('$lib/taDocs/*.md', { + as: 'raw', + eager: true +}); export async function GET({ params }) { const { slug } = params; @@ -18,23 +14,24 @@ export async function GET({ params }) { throw error(400, 'Invalid documentation file requested'); } - const filePath = path.join(DOCS_PATH, slug); + // Create the import path that matches our glob pattern + const importPath = `/src/lib/taDocs/${slug}`; - try { - if (!fs.existsSync(filePath)) { - throw error(404, 'Documentation file not found'); - } - - const content = fs.readFileSync(filePath, 'utf-8'); - - return new Response(content, { - headers: { - 'Content-Type': 'text/markdown', - 'Cache-Control': 'max-age=600' // Cache for 10 minutes - } - }); - } catch (err) { - console.error(`Error reading documentation file: ${filePath}`, err); - throw error(500, JSON.stringify({ error: 'Error loading documentation', err: err })); + // Find the matching document + const content = docs[importPath]; + + if (!content) { + console.error(`Documentation file not found: ${slug}`); + // List available files for debugging + console.error('Available files:', Object.keys(docs)); + throw error(404, 'Documentation file not found'); } + + return new Response(content, { + headers: { + 'Content-Type': 'text/markdown; charset=utf-8', + 'Cache-Control': 'max-age=600', // Cache for 10 minutes + 'Access-Control-Allow-Origin': '*' // Add CORS if needed + } + }); } \ No newline at end of file