Registry / serialization / mdast-util-to-string

mdast-util-to-string

JSON →
library4.0.0jsnpmunverified

mdast-util-to-string is a focused utility within the unified ecosystem designed to extract the plain text content from an mdast (Markdown Abstract Syntax Tree) node or a list of nodes. It is currently stable at version 4.0.0. The project maintains compatibility with maintained versions of Node.js, typically releasing new major versions when older Node.js versions become unmaintained, which often includes breaking changes like switching to ESM or dropping specific API support. This package prioritizes text extraction over full markdown serialization, differentiating itself from `mdast-util-to-markdown`. It's particularly useful for scenarios like generating excerpts, search indexing, or displaying a node's textual content without its formatting, similar to `hast-util-to-string` for hast trees. It ships with full TypeScript support.

npm install mdast-util-to-string
INSTALL
IMPORT
SIG · MDAST-UTIL-TO-STRI
M
mdast-util-to-string
serializationjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toString
import { toString } from 'mdast-util-to-string'
const toString = require('mdast-util-to-string')
mdast-util-to-string is ESM-only since v3. The CommonJS require() syntax will result in an error.
Options
import type { Options } from 'mdast-util-to-string'
Import the Options type for TypeScript when configuring the `toString` function.
toString
import {toString} from 'https://esm.sh/mdast-util-to-string@4'
For Deno or browser environments, use esm.sh to import directly.

This example demonstrates how to parse a markdown string into an mdast tree and then use `toString` to extract the plain text content from the entire tree or specific nodes, including options for handling HTML.

import {fromMarkdown} from 'mdast-util-from-markdown' import {toString} from 'mdast-util-to-string' // A sample markdown string with various inline elements const markdown = '# Hello World\n\nThis is a paragraph with _emphasis_, **importance**, and `code`.\n\nIt also has an [image](https://example.com/image.png "Image Title") and an <a>link</a>.' // Parse the markdown string into an mdast syntax tree const tree = fromMarkdown(markdown) // Get the plain text content of the entire tree console.log('Plain text content:', toString(tree)) // Get the plain text content of a specific node, e.g., the heading const headingNode = tree.children[0]; console.log('Heading text:', toString(headingNode)) // Get the plain text content of the paragraph with `includeHtml` option const paragraphNode = tree.children[1]; console.log('Paragraph text (with HTML):', toString(paragraphNode, {includeHtml: true})) console.log('Paragraph text (without HTML):', toString(paragraphNode, {includeHtml: false}))
Debug
Known issues
breakingmdast-util-to-string v4.0.0 and newer require Node.js 16 or greater. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or higher. If unable to upgrade, you must remain on mdast-util-to-string v3.x.
affects: >=4.0.0
breakingmdast-util-to-string has been ESM-only since v3.0.0. CommonJS `require()` statements will fail. Additionally, v4.0.0 introduced changes to use the `export` map, deprecating direct imports of private APIs.
fix
Migrate your codebase to use ES Modules (ESM) import syntax: `import { toString } from 'mdast-util-to-string'`. Avoid using internal or private paths.
affects: >=3.0.0
breakingVersion 3.0.0 removed support for `node.title` property. If you relied on `node.title` for text content, this will no longer be included.
fix
Review your markdown structures and mdast processing logic. If you need to extract titles, ensure they are represented in other supported node properties or structures that `toString` can process.
affects: >=3.0.0
gotchaBy default, `toString` will include the `alt` text for `image` nodes and the `value` of HTML nodes. This behavior can be controlled with options.
fix
Use the `includeImageAlt: false` option to ignore image alt text or `includeHtml: false` to ignore HTML node values if you do not want them included in the output string.
affects: >=3.2.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require()` an ESM-only package.
fix
Change `const toString = require('mdast-util-to-string')` to `import { toString } from 'mdast-util-to-string'` and ensure your project is configured for ESM.
ReferenceError: require is not defined
Running ESM code that incorrectly uses `require` in an environment that expects ESM (e.g., Node.js with `"type": "module"` in package.json or a modern bundler).
fix
Use `import { toString } from 'mdast-util-to-string'` instead of `require`. Ensure your `package.json` either has `"type": "module"` or uses `.mjs` file extensions for ESM files.
SyntaxError: Unexpected token 'export'
Trying to run ESM code in a Node.js environment that doesn't support it or isn't configured for it (e.g., old Node.js version, missing `"type": "module"`).
fix
Upgrade Node.js to version 16+ and ensure your `package.json` contains `"type": "module"` or explicitly use `.mjs` file extensions for your module files.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
mdast-util-to-string — npm install mdast-util-to-string · libregistry