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`.
48 lines • 1.52 kB
JavaScript
/**
* Copyright (c) @talatkuyuk AKA @ipikuka
* SPDX-License-Identifier: MPL-2.0
*/
import { compile as compileMDX } from "@mdx-js/mdx";
import remarkMdxRemoveEsm, { clsx } from "remark-mdx-remove-esm";
import { createFormattedMDXError } from "./util.js";
/**
* composes an opinionated version of CompileOptions of the "@mdx-js/mdx"
*
* @param options CompileOptions
* @returns CompileOptions of the "@mdx-js/mdx"
*/
function composeCompileOptions(options = {}) {
const { mdxOptions = {}, disableImports, disableExports } = options;
const mdxRemoveEsmOptions = clsx([disableExports && "export", disableImports && "import"]);
const remarkPlugins = [
...(mdxOptions.remarkPlugins || []),
[remarkMdxRemoveEsm, mdxRemoveEsmOptions],
];
return {
...mdxOptions,
remarkPlugins,
outputFormat: "function-body",
};
}
/**
* compiles the vfile source via the compile of the "@mdx-js/mdx".
*
* returns the compiled source.
*/
export async function compile(vfile, options) {
try {
const compiledSource = await compileMDX(vfile, composeCompileOptions(options));
// for debugging
if (options?.debugCompiledSource) {
console.log(String(compiledSource));
}
// await new Promise((resolve) => setTimeout(resolve, 500));
return {
compiledSource,
};
}
catch (error) {
throw createFormattedMDXError(error, String(vfile));
}
}
//# sourceMappingURL=compile.js.map