diff options
Diffstat (limited to 'packages/integrations/mdx/src/utils.ts')
-rw-r--r-- | packages/integrations/mdx/src/utils.ts | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/packages/integrations/mdx/src/utils.ts b/packages/integrations/mdx/src/utils.ts index 6eb7a3570..97bc72d74 100644 --- a/packages/integrations/mdx/src/utils.ts +++ b/packages/integrations/mdx/src/utils.ts @@ -1,4 +1,5 @@ -import type { AstroConfig } from 'astro'; +import type { AstroConfig, SSRError } from 'astro'; +import matter from 'gray-matter'; function appendForwardSlash(path: string) { return path.endsWith('/') ? path : path + '/'; @@ -37,3 +38,23 @@ export function getFileInfo(id: string, config: AstroConfig): FileInfo { } return { fileId, fileUrl }; } + +/** + * Match YAML exception handling from Astro core errors + * @see 'astro/src/core/errors.ts' + */ +export function getFrontmatter(code: string, id: string) { + try { + return matter(code).data; + } 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; + } + } +} |