A robust Next.js newsletter Next.js Weekly is sponsoring me π

A warm thanks π to @ErfanEbrahimnia, @recepkyk, and @LSeaburg for the support π
@ipikuka/handlers is a collection of rehype handlers for markdown / MDX that I used in my many projects.
unified is a project that transforms content with abstract syntax trees (ASTs) using the new parser micromark. rehype is a tool that transforms HTML with plugins. hast stands for HTML Abstract Syntax Tree (HAST) that rehype uses.
@ipikuka/handlers provides custom rehype handlers that is opinionated and well tested for @mdx-js/mdx and related projects like next-mdx-remote and next-mdx-remote-client.
The rehype handlers that exposed by @ipikuka/handlers:
defListviaremark-definition-listdefListTermviaremark-definition-listdefListDescriptionviaremark-definition-listabbrDefinitionwhich is undefined on behalf of@richardtowers/remark-abbr,htmla custom handler for removing undefined tags/components
See also Handlers from mdast-util-to-hast.
This package is suitable for ESM only. In Node.js (version 16+), install with npm:
npm install @ipikuka/handlersor
yarn add @ipikuka/handlersimport {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import handlers from "@ipikuka/handlers";
const file = await unified()
.use(remarkParse)
.use(remarkRehype, {allowDangerousHtml: true, handlers: handlers() })
.use(rehypeStringify, {allowDangerousHtml: true})
.process(source)Thanks to @ipikuka/handlers, the markdown/mdx content will support abbreviations, definition lists and removing unknown html nodes.
Without @ipikuka/handlers the depicted features will not work.
type Options = {
format?: "detect" | "md" | "mdx" | null | undefined;
};It is "md" | "mdx" | "detect" | null | undefined option to adjust remark plugins and whether or not to employ recma plugins.
It is optional, and default is mdx.
import { compile } from "@mdx-js/mdx";
import handlers from "@ipikuka/handlers";
const compiledSource = await compile(source, {
remarkRehypeOptions: {
handlers: handlers({ format: "md" })
}
})
console.log(toc); // now it has table of contents
// ...import { serialize } from "next-mdx-remote-client/serialize";
import handlers from "@ipikuka/handlers";
// ...
const mdxSource = await serialize<TFrontmatter, TScope>({
source,
options: {
mdxOptions: {
// ...,
remarkRehypeOptions: {
handlers: handlers({ format: "md" })
}
},
parseFrontmatter: true,
},
});
// ...import { serialize } from "next-mdx-remote/serialize";
import handlers from "@ipikuka/handlers";
const mdxSource = await serialize<TScope, TFrontmatter>(
source,
{
mdxOptions: {
// ...,
remarkRehypeOptions: {
handlers: handlers({ format: "md" })
}
},
parseFrontmatter: true,
},
);
// ...Handlers are utilized in converting mdast (Markdown abstract syntax tree) into hast (HTML abstract syntax tree).
This package is fully typed with TypeScript.
The package exports the type Options.
The handlers that are provided by this package work with unified version 6+, MDX version 3+, next-mdx-remote, next-mdx-remote-client and @next/mdx.
Use of rehype handlers involves creating hast, but doesn't lead to cross-site scripting (XSS) attacks.
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/handlers 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! π
remark-flexible-code-titlesβ Remark plugin to add titles or/and containers for the code blocks with customizable propertiesremark-flexible-containersβ Remark plugin to add custom containers with customizable properties in markdownremark-insβ Remark plugin to addinselement in markdownremark-flexible-paragraphsβ Remark plugin to add custom paragraphs with customizable properties in markdownremark-flexible-markersβ Remark plugin to add custommarkelement with customizable properties in markdownremark-flexible-tocβ Remark plugin to expose the table of contents viavfile.dataor via an option referenceremark-mdx-remove-esmβ Remark plugin to remove import and/or export statements (mdxjsEsm)remark-mdx-remove-expressionsβ Remark plugin to remove MDX expressions within curlybraces {} in MDX content
rehype-pre-languageβ Rehype plugin to add language information as a property topreelementrehype-highlight-code-linesβ Rehype plugin to add line numbers to code blocks and allow highlighting of desired code linesrehype-code-metaβ Rehype plugin to copycode.data.metatocode.properties.metastringrehype-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.
recma-mdx-escape-missing-componentsβ Recma plugin to set the default value() => nullfor the Components in MDX in case of missing or not provided so as not to throw an errorrecma-mdx-change-propsβ Recma plugin to change thepropsparameter into the_propsin thefunction _createMdxContent(props) {/* */}in the compiled source in order to be able to use{props.foo}like expressions. It is useful for thenext-mdx-remoteornext-mdx-remote-clientusers innextjsapplications.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 gettingReactinstance 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 thealt,src,href, andtitleattributes of markdown link and image syntax in MDX.
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).
unist-util-find-betweenβ Unist utility to find the nodes between two nodes.unified-log-treeβ Unified plugin to log abstract syntax trees (ASTs) for debugging without mutating.
MIT License Β© ipikuka