summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2022-08-12 17:17:26 -0500
committerGravatar GitHub <noreply@github.com> 2022-08-12 18:17:26 -0400
commitf7afdb889fe4e97177958c8ec92f80c5f6e5cb51 (patch)
treebcc42e78a52a91a9a5efe14e93e3c517acb2b441 /packages/integrations/mdx/src
parent437bf73f264b2811b7ee8f11c2b54d3414144a0f (diff)
downloadastro-f7afdb889fe4e97177958c8ec92f80c5f6e5cb51.tar.gz
astro-f7afdb889fe4e97177958c8ec92f80c5f6e5cb51.tar.zst
astro-f7afdb889fe4e97177958c8ec92f80c5f6e5cb51.zip
[MDX] Fix remaining inconsistencies with Markdown (#4268)
* feat: add "file" and "url" to layout props * feat: add rawContent and compiledContent errs * fix: add "file" and "url" to frontmatter * fix: add separate MDX instance type * types: add MarkdownLayoutProps and MDXLayoutProps * refactor: simplify MDXLayoutProps * test: pass file and url to layout * test: glob components with .default and Content * feat: add <Content /> to MDX * feat: declare MDX type module * fix: [MD] move file and url to layout props only * chore: changeset * chore: bump MDX to "minor" with more details * refactor: remove "file" + "url" top-level props (save for minor) * revert: MDInstance type def updates (save for minor) * fix: MDXInstance "default" + "content" types * fix: bad test layout * chore: remove getHeaders fro *.mdx
Diffstat (limited to 'packages/integrations/mdx/src')
-rw-r--r--packages/integrations/mdx/src/astro-data-utils.ts2
-rw-r--r--packages/integrations/mdx/src/index.ts19
2 files changed, 21 insertions, 0 deletions
diff --git a/packages/integrations/mdx/src/astro-data-utils.ts b/packages/integrations/mdx/src/astro-data-utils.ts
index 0bc375c27..f3d04e431 100644
--- a/packages/integrations/mdx/src/astro-data-utils.ts
+++ b/packages/integrations/mdx/src/astro-data-utils.ts
@@ -28,6 +28,8 @@ export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any
export default function ({ children }) {
const { layout, ...content } = frontmatter;
+ content.file = file;
+ content.url = url;
content.astro = {};
Object.defineProperty(content.astro, 'headings', {
get() {
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts
index f92a10a53..17fe0cd74 100644
--- a/packages/integrations/mdx/src/index.ts
+++ b/packages/integrations/mdx/src/index.ts
@@ -26,6 +26,12 @@ const DEFAULT_REMARK_PLUGINS: MdxRollupPluginOptions['remarkPlugins'] = [
];
const DEFAULT_REHYPE_PLUGINS: MdxRollupPluginOptions['rehypePlugins'] = [];
+const RAW_CONTENT_ERROR =
+ 'MDX does not support rawContent()! If you need to read the Markdown contents to calculate values (ex. reading time), we suggest injecting frontmatter via remark plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins';
+
+const COMPILED_CONTENT_ERROR =
+ 'MDX does not support compiledContent()! If you need to read the HTML contents to calculate values (ex. reading time), we suggest injecting frontmatter via rehype plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins';
+
function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] {
if (Array.isArray(config)) return config;
@@ -127,6 +133,19 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
if (!moduleExports.includes('file')) {
code += `\nexport const file = ${JSON.stringify(fileId)};`;
}
+ if (!moduleExports.includes('rawContent')) {
+ code += `\nexport function rawContent() { throw new Error(${JSON.stringify(
+ RAW_CONTENT_ERROR
+ )}) };`;
+ }
+ if (!moduleExports.includes('compiledContent')) {
+ code += `\nexport function compiledContent() { throw new Error(${JSON.stringify(
+ COMPILED_CONTENT_ERROR
+ )}) };`;
+ }
+ if (!moduleExports.includes('Content')) {
+ code += `\nexport const Content = MDXContent;`;
+ }
if (command === 'dev') {
// TODO: decline HMR updates until we have a stable approach