2022-08-30 17:41:06 +00:00
import type { Options as AcornOpts } from 'acorn' ;
2022-08-30 17:38:35 +00:00
import { parse } from 'acorn' ;
2022-08-30 17:41:06 +00:00
import type { AstroConfig , SSRError } from 'astro' ;
import matter from 'gray-matter' ;
import type { MdxjsEsm } from 'mdast-util-mdx' ;
2022-08-30 17:38:35 +00:00
2022-07-20 14:56:32 +00:00
function appendForwardSlash ( path : string ) {
return path . endsWith ( '/' ) ? path : path + '/' ;
}
2022-07-28 14:58:44 +00:00
interface FileInfo {
fileId : string ;
fileUrl : string ;
}
2022-07-20 14:56:32 +00:00
/** @see 'vite-plugin-utils' for source */
2022-07-28 14:58:44 +00:00
export function getFileInfo ( id : string , config : AstroConfig ) : FileInfo {
2022-07-20 14:56:32 +00:00
const sitePathname = appendForwardSlash (
config . site ? new URL ( config . base , config . site ) . pathname : config.base
) ;
2022-07-28 14:58:44 +00:00
// Try to grab the file's actual URL
let url : URL | undefined = undefined ;
try {
url = new URL ( ` file:// ${ id } ` ) ;
} catch { }
2022-07-20 14:56:32 +00:00
const fileId = id . split ( '?' ) [ 0 ] ;
2022-07-28 14:58:44 +00:00
let fileUrl : string ;
const isPage = fileId . includes ( '/pages/' ) ;
2022-07-28 15:00:32 +00:00
if ( isPage ) {
2022-07-28 14:58:44 +00:00
fileUrl = fileId . replace ( /^.*?\/pages\// , sitePathname ) . replace ( /(\/index)?\.mdx$/ , '' ) ;
2022-07-28 15:00:32 +00:00
} else if ( url && url . pathname . startsWith ( config . root . pathname ) ) {
2022-07-28 14:58:44 +00:00
fileUrl = url . pathname . slice ( config . root . pathname . length ) ;
} else {
fileUrl = fileId ;
}
2022-07-20 14:56:32 +00:00
if ( fileUrl && config . trailingSlash === 'always' ) {
fileUrl = appendForwardSlash ( fileUrl ) ;
}
return { fileId , fileUrl } ;
}
2022-07-29 15:22:57 +00:00
/ * *
* Match YAML exception handling from Astro core errors
* @see 'astro/src/core/errors.ts'
2022-07-29 15:24:57 +00:00
* /
2022-08-05 23:55:38 +00:00
export function parseFrontmatter ( code : string , id : string ) {
2022-07-29 15:22:57 +00:00
try {
2022-08-05 23:55:38 +00:00
return matter ( code ) ;
2022-07-29 15:22:57 +00:00
} catch ( e : any ) {
if ( e . name === 'YAMLException' ) {
const err : SSRError = e ;
err . id = id ;
err . loc = { file : e.id , line : e.mark.line + 1 , column : e.mark.column } ;
err . message = e . reason ;
throw err ;
} else {
throw e ;
}
}
}
2022-08-01 21:23:56 +00:00
export function jsToTreeNode (
jsString : string ,
acornOpts : AcornOpts = {
ecmaVersion : 'latest' ,
sourceType : 'module' ,
}
) : MdxjsEsm {
return {
type : 'mdxjsEsm' ,
value : '' ,
data : {
estree : {
body : [ ] ,
. . . parse ( jsString , acornOpts ) ,
type : 'Program' ,
sourceType : 'module' ,
} ,
} ,
} ;
}
2022-08-30 17:38:35 +00:00
// TODO: remove for 1.0
export function handleExtendsNotSupported ( pluginConfig : any ) {
if (
typeof pluginConfig === 'object' &&
pluginConfig !== null &&
( pluginConfig as any ) . hasOwnProperty ( 'extends' )
) {
throw new Error (
` [MDX] The "extends" plugin option is no longer supported! Astro now extends your project's \` markdown \` plugin configuration by default. To customize this behavior, see the \` extendPlugins \` option instead: https://docs.astro.build/en/guides/integrations-guide/mdx/#extendplugins `
) ;
}
}