diff options
author | 2022-07-29 10:22:57 -0500 | |
---|---|---|
committer | 2022-07-29 11:22:57 -0400 | |
commit | 1743fe140eb58d60e26cbd11a066bb60de046e0c (patch) | |
tree | f3e4f24e2ad90c80df47add8a29406cf93217d1a /packages/integrations/mdx/src/utils.ts | |
parent | 45bec97d28d97edd5c234caf3ec97e1977bea0f7 (diff) | |
download | astro-1743fe140eb58d60e26cbd11a066bb60de046e0c.tar.gz astro-1743fe140eb58d60e26cbd11a066bb60de046e0c.tar.zst astro-1743fe140eb58d60e26cbd11a066bb60de046e0c.zip |
feat: support `layout` in MDX frontmatter (#4088)
* deps: add gray-matter
* feat: support layout frontmatter property
* test: frontmatter, content prop
* docs: update layout recommendation
* deps: fix lockfile
* chore: changeset
* fix: inherit rollup plugin transform
* fix: avoid parsing frontmatter on custom parsers
* fix: match YAML err handling from md
* docs: absolute url to docs
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* chore: formatting
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
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; + } + } +} |