Socket
Socket
Sign inDemoInstall

mdast-util-to-string

Package Overview
Dependencies
2
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.1.0 to 3.1.1

lib/index.d.ts

19

index.d.ts

@@ -1,17 +0,2 @@

/**
* @typedef Options
* @property {boolean} [includeImageAlt=true]
*/
/**
* Get the text content of a node.
* Prefer the node’s plain-text fields, otherwise serialize its children,
* and if the given value is an array, serialize the nodes in it.
*
* @param {unknown} node
* @param {Options} [options]
* @returns {string}
*/
export function toString(node: unknown, options?: Options): string
export type Options = {
includeImageAlt?: boolean
}
export {toString} from './lib/index.js'
export type Options = import('./lib/index.js').Options
/**
* @typedef Options
* @property {boolean} [includeImageAlt=true]
* @typedef {import('./lib/index.js').Options} Options
*/
/**
* Get the text content of a node.
* Prefer the node’s plain-text fields, otherwise serialize its children,
* and if the given value is an array, serialize the nodes in it.
*
* @param {unknown} node
* @param {Options} [options]
* @returns {string}
*/
export function toString(node, options) {
var {includeImageAlt = true} = options || {}
return one(node, includeImageAlt)
}
/**
* @param {unknown} node
* @param {boolean} includeImageAlt
* @returns {string}
*/
function one(node, includeImageAlt) {
return (
(node &&
typeof node === 'object' &&
// @ts-ignore looks like a literal.
(node.value ||
// @ts-ignore looks like an image.
(includeImageAlt ? node.alt : '') ||
// @ts-ignore looks like a parent.
('children' in node && all(node.children, includeImageAlt)) ||
(Array.isArray(node) && all(node, includeImageAlt)))) ||
''
)
}
/**
* @param {Array.<unknown>} values
* @param {boolean} includeImageAlt
* @returns {string}
*/
function all(values, includeImageAlt) {
/** @type {Array.<string>} */
var result = []
var index = -1
while (++index < values.length) {
result[index] = one(values[index], includeImageAlt)
}
return result.join('')
}
export {toString} from './lib/index.js'
{
"name": "mdast-util-to-string",
"version": "3.1.0",
"version": "3.1.1",
"description": "mdast utility to get the plain text content of a node",

@@ -32,23 +32,26 @@ "license": "MIT",

"files": [
"lib/",
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"@types/node": "^18.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
"xo": "^0.53.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"

@@ -65,7 +68,3 @@ },

"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},

@@ -72,0 +71,0 @@ "remarkConfig": {

@@ -11,11 +11,40 @@ # mdast-util-to-string

**[mdast][]** utility to get the plain text content of a node.
[mdast][] utility to get the text content of a node.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`toString(value[, options])`](#tostringvalue-options)
* [`Options`](#options)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a tiny utility that gets the textual content of a node.
## When should I use this?
This utility is useful when you have a node, say a heading, and want to get the
text inside it.
This package does not serialize markdown, that’s what
[`mdast-util-to-markdown`][mdast-util-to-markdown] does.
Similar packages, [`hast-util-to-string`][hast-util-to-string] and
[`hast-util-to-text`][hast-util-to-text], do the same but on [hast][].
## Install
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
This package is [ESM only][esm].
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
[npm][]:
```sh

@@ -25,12 +54,24 @@ npm install mdast-util-to-string

In Deno with [`esm.sh`][esmsh]:
```js
import {toString} from 'https://esm.sh/mdast-util-to-string@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {toString} from 'https://esm.sh/mdast-util-to-string@3?bundle'
</script>
```
## Use
```js
import unified from 'unified'
import remarkParse from 'remark-parse'
import {unified} from 'unified'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toString} from 'mdast-util-to-string'
var tree = unified()
.use(remarkParse)
.parse('Some _emphasis_, **importance**, and `code`.')
const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')

@@ -42,20 +83,44 @@ console.log(toString(tree)) // => 'Some emphasis, importance, and code.'

This package exports the following identifiers: `toString`.
This package exports the identifier [`toString`][api-tostring].
There is no default export.
### `toString(node[, options])`
### `toString(value[, options])`
Get the text content of a [node][] or list of nodes.
Get the text content of a node or list of nodes.
The algorithm checks `value` of `node` and then `alt`.
If no value is found, the algorithm checks the children of `node` and joins them
(without spaces or newlines).
Prefers the node’s plain-text fields, otherwise serializes its children,
and if the given value is an array, serialize the nodes in it.
> This is not a markdown to plain-text library.
> Use [`strip-markdown`][strip-markdown] for that.
###### Parameters
###### `options.includeImageAlt`
* `value` (`unknown`)
— thing to serialize, typically [`Node`][node]
* `options` ([`Options`][api-options], optional)
— configuration
Whether to use `alt` (`boolean`, default: `true`)
###### Returns
Serialized `value` (`string`).
### `Options`
Configuration (TypeScript type).
###### Fields
* `includeImageAlt` (`boolean`, default: `true`)
— whether to use `alt` for `image`s
## Types
This package is fully typed with [TypeScript][].
It exports the additional type [`Options`][api-options].
## Compatibility
Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 14.14+ and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
## Security

@@ -69,15 +134,11 @@

* [`nlcst-to-string`](https://github.com/syntax-tree/nlcst-to-string)
— Get text content in nlcst
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-to-string)
— Get text content in hast
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/main/packages/hast-util-to-string)
— get text content in hast
* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text)
— Get text content in hast according to the `innerText` algorithm
* [`hast-util-from-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-from-string)
— Set text content in hast
— get text content in hast according to the `innerText` algorithm
## Contribute
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.

@@ -123,2 +184,8 @@

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license

@@ -128,16 +195,26 @@

[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
[health]: https://github.com/syntax-tree/.github
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[mdast]: https://github.com/syntax-tree/mdast
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
[hast]: https://github.com/syntax-tree/hast
[hast-util-to-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string
[hast-util-to-text]: https://github.com/syntax-tree/hast-util-to-text
[node]: https://github.com/syntax-tree/mdast#nodes
[strip-markdown]: https://github.com/remarkjs/strip-markdown
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[hast]: https://github.com/syntax-tree/hast
[api-tostring]: #tostringvalue-options
[api-options]: #options
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc