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`.
57 lines • 2.11 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/**
* Copyright (c) @talatkuyuk AKA @ipikuka
* SPDX-License-Identifier: MPL-2.0
*/
import { compile } from "../lib/compile.js";
import { prepare } from "../lib/prepare.js";
import { runSync, runAsync } from "../lib/run.js";
import { passVfileDataIntoScope } from "../lib/util.js";
/**
* "compiles" the MDX source and "runs" the javascript code in the compiled source, so it basically "evaluates" the MDX.
*
* returns the frontmatter, exported object and the react server component to be rendered on the server.
*
*/
export async function evaluate({ source, options = {}, components = {}, }) {
const { mdxOptions = {}, disableExports, disableImports, scope = {}, parseFrontmatter, vfileDataIntoScope, debug, } = options;
const { vfile, frontmatter } = prepare(source, parseFrontmatter);
const { baseUrl, ...compileMDXOptions } = mdxOptions;
try {
const { compiledSource } = await compile(vfile, {
mdxOptions: compileMDXOptions,
disableExports,
disableImports,
debugCompiledSource: debug?.compiledSource,
});
if (vfileDataIntoScope)
passVfileDataIntoScope(compiledSource.data, vfileDataIntoScope, scope);
// This check is necessary otherwise "await" expression in the compiledSource throws a syntax error
const { Content, mod } = disableImports
? runSync(String(compiledSource), {
frontmatter,
scope,
})
: await runAsync(String(compiledSource), {
mdxOptions: { baseUrl },
frontmatter,
scope,
});
return {
content: _jsx(Content, { components: components }),
mod,
frontmatter,
scope,
};
}
catch (error) {
return {
content: _jsx("div", { className: "mdx-empty" }),
mod: {},
frontmatter: frontmatter,
scope,
error: error,
};
}
}
//# sourceMappingURL=evaluate.js.map