maybe docs works?
This commit is contained in:
parent
3ffd9ed38c
commit
1922ee58ec
1 changed files with 23 additions and 26 deletions
|
|
@ -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');
|
||||
}
|
||||
// Find the matching document
|
||||
const content = docs[importPath];
|
||||
|
||||
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 }));
|
||||
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
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue