Skip to content

Commit

Permalink
chore: minor changes on types
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Nov 1, 2023
1 parent 22768c6 commit b973c22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
17 changes: 7 additions & 10 deletions next.mdx.compiler.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { compile, runSync } from '@mdx-js/mdx';
import * as runtime from 'react/jsx-runtime';
import * as jsxRuntime from 'react/jsx-runtime';
import { matter } from 'vfile-matter';

import { NEXT_REHYPE_PLUGINS, NEXT_REMARK_PLUGINS } from './next.mdx.mjs';
Expand All @@ -10,9 +10,9 @@ import { NEXT_REHYPE_PLUGINS, NEXT_REMARK_PLUGINS } from './next.mdx.mjs';
* This is our custom simple MDX Compiler that is used to compile Markdown and MDX
* this returns a serializable VFile as a string that then gets passed to our MDX Provider
*
* @param {VFile} source
* @param {import('vfile').VFile} source
* @param {'md' | 'mdx'} fileExtension
* @returns {Promise<{ content: VFile; headings: import('@vcarl/remark-headings').Heading[]; frontmatter: Record<string, any>}>}
* @returns {Promise<{ content: import('vfile').VFile; headings: import('@vcarl/remark-headings').Heading[]; frontmatter: Record<string, any>}>}
*/
export async function compileMDX(source, fileExtension) {
// Parses the Frontmatter to the VFile and removes from the original source
Expand All @@ -27,6 +27,8 @@ export async function compileMDX(source, fileExtension) {
// This instructs the MDX compiler to generate a minimal JSX-body
// to be consumed within MDX's `run` method, instead of a standalone React Application
outputFormat: 'function-body',
// Ensure compatibility with Server Components
providerImportSource: undefined,
});

// Retrieve some parsed data from the VFile metadata
Expand All @@ -40,16 +42,11 @@ export async function compileMDX(source, fileExtension) {
* This evaluates our MDX VFile into actual JSX eval'd code
* which is actually used by the MDX Provider
*
* @param {VFile} source
* @param {string} source
* @returns {import('mdx/types').MDXContent}
*/
export function runMDX(source) {
const { default: content } = runSync(source, {
// We need to pass the current JSX runtime down the road to the MDX Runtime
// so that it can properly evaluate the MDX into JSX
...runtime,
baseUrl: import.meta.url,
});
const { default: content } = runSync(source, jsxRuntime);

return content;
}
4 changes: 1 addition & 3 deletions providers/mdxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MDXComponents } from 'mdx/types';
import { useEffect, useMemo } from 'react';
import { useMemo } from 'react';
import type { FC } from 'react';

import { runMDX } from '@/next.mdx.compiler.mjs';
Expand All @@ -12,8 +12,6 @@ const combinedComponents: MDXComponents = {
};

export const MDXProvider: FC<{ content: string }> = ({ content }) => {
useEffect(() => window.startLegacyApp(), []);

// Parses the MDX Function and eval's it into a React Component
// We don't need asynchronous runtime here as we want to render the MDX
// as soon as it is available and be able to make initial renders
Expand Down

0 comments on commit b973c22

Please sign in to comment.