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`.
43 lines (35 loc) • 1.23 kB
text/typescript
/**
* Copyright (c) @talatkuyuk AKA @ipikuka
* SPDX-License-Identifier: MPL-2.0
*/
import { type Compatible, VFile } from "vfile";
import { matter } from "vfile-matter";
import type { PrepareResult } from "./types.js";
/**
* turns the source into vfile, gets the fronmatter, strips it out from the vfile
*
* @param source markdown or MDX source
* @param parseFrontmatter indicates whether or not the frontmatter should be parsed out of the source
* @returns the frontmatter and stripped vfile
*/
export function prepare<TFrontmatter extends Record<string, unknown> = Record<string, unknown>>(
source: Compatible,
parseFrontmatter?: boolean,
): PrepareResult<TFrontmatter> {
const vfile = looksLikeAVFile(source) ? source : new VFile(source);
// makes frontmatter available via vfile.data.matter
if (parseFrontmatter) matter(vfile, { strip: true });
const frontmatter = (vfile.data.matter ?? {}) as TFrontmatter;
return {
vfile,
frontmatter,
};
}
/**
* taken from @mdx-js/mdx/lib/util/resolve-file-and-options.js
*/
function looksLikeAVFile(value?: Compatible): value is VFile {
return Boolean(
value && typeof value === "object" && "message" in value && "messages" in value,
);
}