next-mdx-remote-client
Version:
A wrapper of the `@mdx-js/mdx` for the `nextjs` applications in order to load MDX content. It is a fork of `next-mdx-remote`.
59 lines • 2.24 kB
JavaScript
/**
* Copyright (c) @talatkuyuk AKA @ipikuka
* SPDX-License-Identifier: MPL-2.0
*/
import * as React from "react";
import * as jsxRuntime from "react/jsx-runtime";
import * as jsxDevRuntime from "react/jsx-dev-runtime";
// TODO: use run and runSync function from "@mdx-js/mdx"
/**
* creates an object to be used for the function construction
*
* @param options RunOptions
* @returns keys and values of the object
*/
function prepareConstruction(options) {
const constructionScope = {
runMdxOptions: {
useMDXComponents: options.mdxOptions?.useMDXComponents,
baseUrl: options.mdxOptions?.baseUrl,
jsx: jsxRuntime.jsx,
jsxs: jsxRuntime.jsxs,
jsxDEV: jsxDevRuntime.jsxDEV,
Fragment: jsxRuntime.Fragment, // doesn't matter
React,
},
frontmatter: options.frontmatter,
...options.scope,
};
return {
keys: Object.keys(constructionScope),
values: Object.values(constructionScope),
};
}
/**
* parses and runs the javascript code syncronously in the compiled MDX source.
*/
export function runSync(compiledSource, options) {
const { keys, values } = prepareConstruction(options);
/* v8 ignore next */
const SyncFunction = function () { }.constructor;
// constructs the compiled source utilizing Reflect API with "function constructor"
const hydrateFn = Reflect.construct(SyncFunction, keys.concat(compiledSource));
const { default: Content, ...mod } = hydrateFn(...values);
return { Content, mod };
}
/**
* parses and runs the javascript code asyncronously in the compiled MDX source.
*/
export async function runAsync(compiledSource, options) {
const { keys, values } = prepareConstruction(options);
/* v8 ignore next */
const AsyncFunction = async function () { }.constructor;
// await new Promise((resolve) => setTimeout(resolve, 500));
// constructs the compiled source utilizing Reflect API with "async function constructor"
const hydrateFn = Reflect.construct(AsyncFunction, keys.concat(compiledSource));
const { default: Content, ...mod } = await hydrateFn(...values);
return { Content, mod };
}
//# sourceMappingURL=run.js.map