parseMdx(options)

Parse a local MDX file for server-side rendering. This is a standalone function for one-off operations without creating a docs instance.

Usage

import { parseMdx } from "@ariadocs/react";

const { raw, content, frontmatter, toc, MDX } = await parseMdx({
  contentDir: "contents/docs",
  slug: "getting-started",
});

Parameters

options

PropertyTypeRequiredDescription
contentDirstringYesPath to the content directory
slugstringYesFile path without .mdx extension
remarkPluginsRemarkPluginsNoArray of remark plugins
rehypePluginsRehypePluginsNoArray of rehype plugins
mdxComponentsMDXComponentsNoCustom MDX components for rendering

Returns

Returns a ParseResult object:

PropertyTypeDescription
rawstringRaw MDX content
contentstringMDX content without frontmatter
frontmatterobjectParsed frontmatter data
tocTocItem[]Table of contents extracted from headings
MDXReactNodeRendered MDX component

Example

import { parseMdx } from "@ariadocs/react";
import {
  remarkGfm,
  rehypePrism,
  rehypeSlug,
  rehypeAutolinkHeadings,
} from "@ariadocs/react/plugins";

export default async function DocPage() {
  const { MDX, frontmatter, toc } = await parseMdx({
    contentDir: "contents/docs",
    slug: "getting-started",
    remarkPlugins: [remarkGfm],
    rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings, rehypePrism],
  });

  return (
    <article>
      <h1>{frontmatter.title}</h1>
      <nav>
        {toc.map((item) => (
          <a key={item.id} href={`#${item.id}`}>
            {item.value}
          </a>
        ))}
      </nav>
      {MDX}
    </article>
  );
}

Custom Frontmatter

You can type the frontmatter with a generic:

interface MyFrontmatter {
  title: string;
  description: string;
  author: string;
  tags: string[];
}

const { frontmatter } = await parseMdx<MyFrontmatter>({
  contentDir: "contents/docs",
  slug: "article",
});

// frontmatter.title, frontmatter.author, etc. are typed!