Skip to content

ipikuka/mdx

Repository files navigation

@ipikuka/mdx

A robust Next.js newsletter Next.js Weekly is sponsoring me πŸ’– NextjsWeekly banner

A warm thanks πŸ™Œ to @ErfanEbrahimnia, @recepkyk, and @LSeaburg for the support πŸ’–


npm version npm downloads publish to npm code-coverage type-coverage typescript license

This package is an opinionated wrapper of next-mdx-remote-client. Since it is peer dependent to next-mdx-remote-client you need to install next-mdx-remote-client as well.

When should I use this?

@ipikuka/mdx is battery-included. You don't need to add any remark, rehype, remark plugins because I included them already according to my expertise. If you need to add a plugin let me know opening an issue.

The plugins used in @ipikuka/mdx comes from @ipikuka/plugins which provides remarkPlugins, rehypePlugins, recmaPlugins.

The rehype handlers used in @ipikuka/mdx comes from @ipikuka/handlers which provides remarkRehypeOptions["handlers"].

Thanks to @ipikuka/plugins and @ipikuka/handlers, markdown/MDX content will support:

  • bold texts, italic texts,
  • lists, blockquotes, headings,
  • table of contents (TOC),
  • containers, admonitions, callouts,
  • marked texts, inserted texts,
  • centered paragraphs, aligned paragraphs,
  • guillements,
  • gfm syntax (tables, strikethrough, task lists, autolinks, footnotes etc.),
  • highlighted and numbered code fences,
  • code titles,
  • autolink for headers,
  • abbreviations,
  • inline footnotes,
  • enhanced markdown image syntax,
  • definition lists etc. and many more.

Installation

This package is suitable for ESM module only. In Node.js (16.0+), install with npm:

npm install @ipikuka/mdx next-mdx-remote-client

# if you are using react@19 specifically
npm install @ipikuka/mdx next-mdx-remote-client@2

# if you are using react@18 specifically
npm install @ipikuka/mdx next-mdx-remote-client@1

or

yarn add @ipikuka/mdx next-mdx-remote-client

# if you are using react@19 specifically
yarn add @ipikuka/mdx next-mdx-remote-client@2

# if you are using react@18 specifically
yarn add @ipikuka/mdx next-mdx-remote-client@1

Usage

This package is peer dependant with react, react-dom and next-mdx-remote-client so it is assumed that you have already installed them.

Example with Next.js pages router

@ipikuka/mdx provides a serialize function. The serialize function is an opinionated wrapper of the serialize function of the next-mdx-remote-client which is a set of light utilities allowing MDX to be loaded within getStaticProps or gerServerSideProps and hydrated correctly on the client.

@ipikuka/mdx provides also hydrate function and MDXClient component for "pages" router.

See for more details about next-mdx-remote-client.

import { serialize } from "@ipikuka/mdx/serialize";
import { MDXClient } from "@ipikuka/mdx";

import ErrorComponent from "../components/ErrorComponent";
import { TableOfComponent, Test } from "../mdxComponents";

const components = {
  TableOfComponent,
  Test,
  wrapper: ({ children }) => <div className="mdx-wrapper">{children}</div>,
};

export default function Page({ mdxSource }) {
  if ("error" in mdxSource) {
    return <ErrorComponent error={mdxSource.error} />;
  }

  return <MDXClient {...mdxSource} components={components} />;
}

export async function getStaticProps() {
  const source = `---
title: My Article
---
<TableOfComponent toc={toc} />

Some **bold** and ==marked== text in MDX.

~|> Centered paragraph (thanks to remark-flexible-paragraphs)

With a component <Test />

::: tip The Title of The Container
The content of the tip (thanks to remark-flexible-containers)
:::
`;

  const mdxSource = await serialize({
    source,
    options: { parseFrontmatter: true },
  });

  return { props: { mdxSource } };
}

Example with Next.js app router

The @ipikuka/mdx provides evaluate function and MDXRemote component for "app" router.

See for more details about next-mdx-remote-client.

import { Suspense } from "react";
import { MDXRemote } from "@ipikuka/mdx/rsc";

import { ErrorComponent, LoadingComponent } from "../components";
import { TableOfComponent, Test } from "../mdxComponents";

const components = {
  TableOfComponent,
  Test,
  wrapper: ({ children }) => <div className="mdx-wrapper">{children}</div>,
};

export default async function Page() {
  const source = `---
title: My Article
---
<TableOfComponent toc={toc} />

Some **bold** and ==marked== text in MDX.

~|> Centered paragraph (thanks to remark-flexible-paragraphs)

With a component <Test />

::: tip The Title of The Container
The content of the tip (thanks to remark-flexible-containers)
:::
`;

  return (
    <Suspense fallback={<LoadingComponent />}>
      <MDXRemote
        source={source}
        options={{ parseFrontmatter: true }}
        components={components}
        onError={ErrorComponent}
      />
    </Suspense>
  );
}

Options

@ipikuka/mdx has the same options with next-mdx-remote-client as a wrapper.

See next-mdx-remote-client.

Types

@ipikuka/mdx is fully typed with TypeScript and exposes the same types as next-mdx-remote-client does.

See next-mdx-remote-client.

Compatibility

It is a Nextjs compatible package.

Security

This package has the same security concerns with next-mdx-remote-client.

My Plugins

I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to have a look my plugins.

Support My Work (become a sponsor πŸš€)

If you find @ipikuka/plugins or any of my projects is useful and helpful, please consider supporting my work. Your sponsorship means a lot to me and keeps these projects alive and updated! πŸ’–

My sponsors are going to be featured at the very top of the page and proudly displayed on my Sponsor Wall.

Thank you for supporting open source! πŸ™Œ

My Remark Plugins

My Rehype Plugins

  • rehype-pre-language – Rehype plugin to add language information as a property to pre element
  • rehype-highlight-code-lines – Rehype plugin to add line numbers to code blocks and allow highlighting of desired code lines
  • rehype-code-meta – Rehype plugin to copy code.data.meta to code.properties.metastring
  • rehype-image-toolkit – Rehype plugin to enhance Markdown image syntax ![]() and Markdown/MDX media elements (<img>, <audio>, <video>) by auto-linking bracketed or parenthesized image URLs, wrapping them in <figure> with optional captions, unwrapping images/videos/audio from paragraph, parsing directives in title for styling and adding attributes, and dynamically converting images into <video> or <audio> elements based on file extension.

My Recma Plugins

  • recma-mdx-escape-missing-components – Recma plugin to set the default value () => null for the Components in MDX in case of missing or not provided so as not to throw an error
  • recma-mdx-change-props – Recma plugin to change the props parameter into the _props in the function _createMdxContent(props) {/* */} in the compiled source in order to be able to use {props.foo} like expressions. It is useful for the next-mdx-remote or next-mdx-remote-client users in nextjs applications.
  • recma-mdx-change-imports – Recma plugin to convert import declarations for assets and media with relative links into variable declarations with string URLs, enabling direct asset URL resolution in compiled MDX.
  • recma-mdx-import-media – Recma plugin to turn media relative paths into import declarations for both markdown and html syntax in MDX.
  • recma-mdx-import-react – Recma plugin to ensure getting React instance from the arguments and to make the runtime props {React, jsx, jsxs, jsxDev, Fragment} is available in the dynamically imported components in the compiled source of MDX.
  • recma-mdx-html-override – Recma plugin to allow selected raw HTML elements to be overridden via MDX components.
  • recma-mdx-interpolate – Recma plugin to enable interpolation of identifiers wrapped in curly braces within the alt, src, href, and title attributes of markdown link and image syntax in MDX.

My Unist Utils and Unified Plugins

I also build low-level utilities and plugins for the Unified ecosystem that can be used across Remark, Rehype, Recma, and other unist-based abstract syntax trees (ASTs).

License

MIT License Β© ipikuka

About

It is an opinionated wrapper of `next-mdx-remote-client`.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors