11import { error } from '@sveltejs/kit' ;
2- import fs from 'fs' ;
3- import path from 'path' ;
42import type { PageServerLoad } from './$types' ;
53
4+ // Use a simpler approach with virtual modules
5+ const markdownModules = import . meta. glob ( '/static/markdown/**/*.md' , { as : 'raw' , eager : true } ) ;
6+
67export const load : PageServerLoad = async ( { params, url } ) => {
78 try {
89 // Ensure the slug is properly defined
@@ -19,65 +20,63 @@ export const load: PageServerLoad = async ({ params, url }) => {
1920 targetSlug = 'introduction' ;
2021 }
2122
22- // Try multiple file paths to find the correct markdown file
23- let filePath = '' ;
24- let content = '' ;
25- let fileFound = false ;
23+ // Debug: Log all available markdown files
24+ console . log ( 'Available markdown modules:' , Object . keys ( markdownModules ) ) ;
2625
27- // Possible file paths to try in order
28- const possiblePaths = [
29- // Direct match (e.g., metrics/quick-start.md for /metrics/quick-start)
30- path . join ( process . cwd ( ) , 'static' , 'markdown' , `${ targetSlug } .md` ) ,
31-
32- // Nested path with index.md (e.g., metrics/index.md for /metrics)
33- path . join ( process . cwd ( ) , 'static' , 'markdown' , targetSlug , 'index.md' ) ,
34-
35- // Hyphenated path (e.g., metrics-quick-start.md for /metrics/quick-start)
36- path . join ( process . cwd ( ) , 'static' , 'markdown' , `${ targetSlug . replace ( / \/ / g, '-' ) } .md` ) ,
26+ // Function to find a markdown file by slug
27+ function findMarkdownContent ( slug : string ) : string | null {
28+ // Try different path patterns
29+ const patterns = [
30+ // Direct match
31+ `/static/markdown/${ slug } .md` ,
32+ // Nested index
33+ `/static/markdown/${ slug } /index.md` ,
34+ // Hyphenated
35+ `/static/markdown/${ slug . replace ( / \/ / g, '-' ) } .md`
36+ ] ;
3737
38- // For root path, try index.md
39- path . join ( process . cwd ( ) , 'static' , 'markdown' , 'index.md' ) ,
40-
41- // Alternative extension
42- path . join ( process . cwd ( ) , 'static' , 'markdown' , `${ targetSlug } .markdown` ) ,
43- ] ;
44-
45- for ( const tryPath of possiblePaths ) {
46- if ( fs . existsSync ( tryPath ) ) {
47- filePath = tryPath ;
48- fileFound = true ;
49- break ;
38+ // For the root path
39+ if ( ! slug || slug === '' ) {
40+ patterns . push ( '/static/markdown/index.md' ) ;
5041 }
51- }
52-
53- // If no file was found, try one more approach for nested paths
54- if ( ! fileFound && targetSlug . includes ( '/' ) ) {
55- // For paths like 'metrics/quick-start', try both 'metrics/quick-start.md' and 'metrics-quick-start.md'
56- const segments = targetSlug . split ( '/' ) ;
57- const lastSegment = segments . pop ( ) || '' ;
58- const parentPath = segments . join ( '/' ) ;
5942
60- const nestedPath = path . join ( process . cwd ( ) , 'static' , 'markdown' , parentPath , `${ lastSegment } .md` ) ;
43+ // Try each pattern
44+ for ( const pattern of patterns ) {
45+ if ( markdownModules [ pattern ] ) {
46+ console . log ( 'Found file at path:' , pattern ) ;
47+ return markdownModules [ pattern ] ;
48+ }
49+ }
6150
62- if ( fs . existsSync ( nestedPath ) ) {
63- filePath = nestedPath ;
64- fileFound = true ;
51+ // If slug contains slashes, try a nested approach
52+ if ( slug . includes ( '/' ) ) {
53+ const segments = slug . split ( '/' ) ;
54+ const lastSegment = segments . pop ( ) || '' ;
55+ const parentPath = segments . join ( '/' ) ;
56+
57+ const nestedPattern = `/static/markdown/${ parentPath } /${ lastSegment } .md` ;
58+ if ( markdownModules [ nestedPattern ] ) {
59+ console . log ( 'Found file at nested path:' , nestedPattern ) ;
60+ return markdownModules [ nestedPattern ] ;
61+ }
6562 }
63+
64+ return null ;
6665 }
6766
68- // If still no file found, throw a 404 error
69- if ( ! fileFound ) {
67+ // Find the markdown file
68+ const content = findMarkdownContent ( targetSlug ) ;
69+
70+ // If no file found, throw a 404 error
71+ if ( ! content ) {
72+ console . error ( `Page not found: ${ targetSlug } ` ) ;
7073 throw error ( 404 , `Page not found: ${ targetSlug } ` ) ;
7174 }
7275
73- // Read the file content
74- content = fs . readFileSync ( filePath , 'utf-8' ) ;
75-
7676 // Return the slug and content
77- const result = { slug : targetSlug , content } ;
78-
79- return result ;
77+ return { slug : targetSlug , content } ;
8078 } catch ( e : any ) {
79+ console . error ( 'Error in load function:' , e ) ;
8180 if ( e . status === 404 ) {
8281 throw e ; // Re-throw 404 errors
8382 }
0 commit comments