What is mdast-util-to-string?
The mdast-util-to-string package is a utility to extract plain text strings from an MDAST (Markdown Abstract Syntax Tree) node. It is commonly used when working with markdown processing or AST manipulation to get the string representation of a node without any markdown syntax.
What are mdast-util-to-string's main functionalities?
Extracting string from MDAST node
This feature allows you to pass an MDAST node to the `toString` function and receive a plain text string in return. It's useful for extracting readable text from markdown content.
const toString = require('mdast-util-to-string');
const mdast = {
type: 'paragraph',
children: [{type: 'text', value: 'Hello, world!'}]
};
const text = toString(mdast);
console.log(text); // 'Hello, world!'
Other packages similar to mdast-util-to-string
hast-util-to-text
Similar to mdast-util-to-string, hast-util-to-text is a utility for converting HAST (Hypertext Abstract Syntax Tree) nodes to plain text strings. While mdast-util-to-string works with markdown, hast-util-to-text is used for HTML content.
unist-util-stringify-position
This package is similar in that it also operates on AST nodes, specifically UNIST nodes, to stringify the position of a node. It's different from mdast-util-to-string as it focuses on the position rather than extracting the text content.
remark-stringify
Remark-stringify is part of the remark ecosystem and is used to serialize markdown. It is similar to mdast-util-to-string in that it deals with markdown content, but it focuses on converting the entire MDAST back to a markdown string, rather than extracting plain text.
mdast-util-to-string
mdast utility to get the plain text
content of a node.
Installation
npm:
npm install mdast-util-to-string
mdast-util-to-string is also available for bower,
component, and
duo, and as an AMD, CommonJS, and globals
module, uncompressed and
compressed.
Usage
var mdast = require('mdast');
var toString = require('mdast-util-to-string');
var ast = mdast.parse('Some *emphasis*, **strongness**, and `code`.');
toString(ast);
API
toString(node)
Get the text content of a node.
The algorithm checks the value of node
, then its alt
, and then
its title
, in that order. If no value is found, the algorithm checks
the children of node
and joins them (without spaces or newlines).
This is not a markdown to plain-text library.
Use strip-markdown for that.
Parameters:
Returns: string
— text representation of node
.
License
MIT © Titus Wormer