Micro MDX Parser
micro-mdx-parser is a lightweight JavaScript parser engineered to efficiently convert Markdown or HTML content into a structured JSON representation. This package is specifically designed for scenarios requiring client-side parsing and component rendering within CMS-like applications, where a full MDX toolchain might be considered excessive. It is built as a fork of the `himalaya` parser, incorporating specific 'MDX-like' adjustments to better align with modern component-based content rendering needs. Currently stable at version 1.1.24, its release cadence is typically infrequent, characteristic of a mature, specialized utility library. Key differentiators include its minimal footprint, versatile compatibility across both browser and server environments, and its focus on generating a parseable JSON AST for programmatic content manipulation, rather than direct HTML output.
Common errors
-
TypeError: (0 , micro_mdx_parser__WEBPACK_IMPORTED_MODULE_0__.parse) is not a function
cause Incorrectly attempting to use a default import for `parse` in an environment where it's only exported as a named export.fixChange the import statement to use named import syntax: `import { parse } from 'micro-mdx-parser';` -
Error: Cannot find module 'micro-mdx-parser'
cause The package has not been installed or is not resolvable in the current environment.fixEnsure the package is installed via `npm install micro-mdx-parser` or `yarn add micro-mdx-parser`, and that your module resolution paths are correctly configured. -
Property 'content' does not exist on type 'Element' or 'Root' or 'Node'.
cause Attempting to access `content` directly on an element node (e.g., `h1`, `p`), which typically contains children nodes, not raw text content directly on the element itself. Text content is usually within child 'text' nodes.fixAccess text content through child nodes of type 'text'. For example, `element.children[0].content` if the first child is a text node, or iterate through children to find text nodes.
Warnings
- gotcha The package is 'MDX-like', meaning it processes some MDX-specific syntax like components (e.g., `<MyComponent />`), but it is not a full MDX compiler. It does not evaluate JavaScript expressions within Markdown or handle complex MDX features like imports or JSX in Markdown beyond basic component tags. Developers expecting a full MDX runtime will find it lacking.
- gotcha Being a fork of `himalaya`, `micro-mdx-parser` might exhibit subtle differences in parsing HTML compared to the original `himalaya` library, especially around edge cases or malformed HTML. While the core parsing logic is similar, the 'MDX-like' tweaks could introduce divergences.
- gotcha The parser is designed to be 'tiny' and lightweight, which might mean it does not include extensive error handling or recovery mechanisms for highly malformed input. Invalid or unexpected syntax might lead to truncated output or unhandled errors rather than detailed diagnostics.
- gotcha While the package supports both browser and server environments, browser-specific build tooling or polyfills might be required for older browser targets, as the underlying `himalaya` library might use modern JavaScript features.
Install
-
npm install micro-mdx-parser -
yarn add micro-mdx-parser -
pnpm add micro-mdx-parser
Imports
- parse
import parse from 'micro-mdx-parser';
import { parse } from 'micro-mdx-parser'; - parse
const parse = require('micro-mdx-parser').parse;const { parse } = require('micro-mdx-parser'); - ParserOutput
import type { ParserOutput } from 'micro-mdx-parser';
Quickstart
import { parse } from 'micro-mdx-parser';
const markdownContent = `# Hello MDX-like!
This is a paragraph with **bold** text and an _italic_ word.
<p>HTML can also be parsed.</p>
<MyComponent prop="value" />
`;
const htmlContent = `<div><h1>Welcome</h1><p>This is <strong>some</strong> HTML.</p></div>`;
const parsedMarkdown = parse(markdownContent);
const parsedHtml = parse(htmlContent);
console.log('Parsed Markdown AST:');
console.log(JSON.stringify(parsedMarkdown, null, 2));
console.log('\nParsed HTML AST:');
console.log(JSON.stringify(parsedHtml, null, 2));
// Example of accessing elements
if (parsedMarkdown.length > 0 && parsedMarkdown[0].type === 'element' && parsedMarkdown[0].tagName === 'h1') {
console.log('\nFirst element in Markdown is an H1:', parsedMarkdown[0].children[0].content);
}