import { PageStrategy } from '..' ; import { extractStaticData , File } from '../../utils/virtual-module' ; import type { FileHandler , FindPages } from '../types.doc' ; export class DefaultPageStrategy extends PageStrategy { constructor ( opts = { } ) { const { extraFindPages , fileHandler = defaultFileHandler } = opts ; super ( ( pagesDir , helpersFromParent ) => { const helpers = this . createHelpers ( fileHandler ) ; helpers . watchFiles ( pagesDir , '**/*$.{md,mdx,js,jsx,ts,tsx}' ) ; if ( typeof extraFindPages === 'function' ) { extraFindPages ( pagesDir , helpers ) ; } } ) ; } } /**
 * The defaultFileHandler return the result to caller,
 * instead of directly setting the pageData object.
 * so that it is more useful to users.
 */ export const defaultFileHandler = async ( file , api ) => { const pagePublicPath = getPagePublicPath ( file . relative ) ; return { pageId : pagePublicPath , dataPath : file . path , staticData : await extractStaticData ( file ) , } ; } ; /**
 * turn `sub-path/page2/index.tsx` into `/sub-path/page2`
 */ export function getPagePublicPath ( relativePageFilePath ) { let pagePublicPath = relativePageFilePath . replace ( /\$\.(md|mdx|js|jsx|ts|tsx)$/ , '' ) ; pagePublicPath = pagePublicPath . replace ( /index$/ , '' ) ; pagePublicPath = pagePublicPath . replace ( /\/$/ , '' ) ; pagePublicPath = `/${ pagePublicPath }` ; pagePublicPath = pagePublicPath . replace ( /\[(.*?)\]/g , ( _ , paramName ) => `:${ paramName }` ) ; return pagePublicPath ; }