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
Get the plain text content of an MDAST node.
Installation
npm:
npm install mdast-util-to-string
Usage
var unified = require('unified');
var parse = require('remark-parse');
var toString = require('mdast-util-to-string');
var tree = unified()
.use(parse)
.parse('Some _emphasis_, **importance**, and `code`.');
console.log(toString(tree));
API
toString(node)
Get the text content of a node.
The algorithm checks value
of node
, then alt
, and finally title
.
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.
Related
License
MIT © Titus Wormer