This package is an opinionated wrapper of the next-mdx-remote.
The @ipikuka/next-mdx-remote
provides a serialize
function. The serialize
function is an opinionated wrapper of the serialize
function of the next-mdx-remote
which is a set of light utilities allowing MDX to be loaded within getStaticProps
or gerServerSideProps
and hydrated correctly on the client.
The plugins used in the @ipikuka/next-mdx-remote
comes from @ipikuka/plugins
.
@ipikuka/plugins
provides remarkPlugins
, rehypePlugins
, recmaPlugins
, and remarkRehypeOptions
.
Thanks to @ipikuka/plugins
, the markdown/mdx content will support table of contents, containers, markers, aligned paragraphs, gfm syntax (tables, strikethrough, task lists, autolinks etc.), inserted texts, highlighted code fences, code titles, autolink for headers, definition lists etc. in addition to standard markdown syntax like bold texts, italic texts, lists, blockquotes, headings etc.
For other mdxOptions
see https://github.com/hashicorp/next-mdx-remote#apis.
This package is suitable for ESM module only. In Node.js (16.0+), install with npm:
npm install @ipikuka/next-mdx-remote
or
yarn add @ipikuka/next-mdx-remote
This package is peer dependant with react
, react-dom
; so it is assumed that you have already installed.
import { MDXRemote } from "@ipikuka/next-mdx-remote";
import { serialize } from "@ipikuka/next-mdx-remote/serialize";
import * as components from "../components/mdxcomponents";
export default function TestPage({ mdxSource }) {
return (
<div className="mdx-wrapper">
<MDXRemote {...mdxSource} components={components} />
</div>
);
}
export async function getStaticProps() {
// the source can be from a local file, database, anywhere
const source = "Some mdx content with a component <Test />";
const mdxSource = await serialize(source);
return { props: { mdxSource } };
}
The @ipikuka/next-mdx-remote
serialize function accepts the same options with next-mdx-remote
.
All options are optional.
type SerializeOptions = {
/**
* Pass-through variables for use in the MDX content
*/
scope?: Record<string, unknown>;
/**
* These options are passed to the MDX compiler.
* See [the MDX docs.](https://github.com/mdx-js/mdx/blob/master/packages/mdx/index.js).
*/
mdxOptions?: Omit<CompileOptions, "outputFormat" | "providerImportSource"> & {
useDynamicImport?: boolean;
};
/**
* Indicate whether or not frontmatter should be parsed out of the MDX. Defaults to false
*/
parseFrontmatter?: boolean;
};
For example, you can specify the source is markdown md
. (Default is MDX mdx
).
const source = "Some **bold text** and ==marked text==";
const mdSource = await serialize(source, { mdxOptions: { format: "md" } });
Need a playground with single page web application. (PR is wellcome)
This package is fully typed with TypeScript and exposes the types as the official next-mdx-remote
does.
It is a Nextjs
compatible package.
MIT © ipikuka