mdast-util-to-string
Advanced tools
Comparing version 3.0.0 to 3.1.0
/** | ||
* @typedef Options | ||
* @property {boolean} [includeImageAlt=true] | ||
*/ | ||
/** | ||
* Get the text content of a node. | ||
@@ -7,4 +11,8 @@ * Prefer the node’s plain-text fields, otherwise serialize its children, | ||
* @param {unknown} node | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function toString(node: unknown): string | ||
export function toString(node: unknown, options?: Options): string | ||
export type Options = { | ||
includeImageAlt?: boolean | ||
} |
29
index.js
/** | ||
* @typedef Options | ||
* @property {boolean} [includeImageAlt=true] | ||
*/ | ||
/** | ||
* Get the text content of a node. | ||
@@ -7,5 +12,16 @@ * Prefer the node’s plain-text fields, otherwise serialize its children, | ||
* @param {unknown} node | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function toString(node) { | ||
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 ( | ||
@@ -17,6 +33,6 @@ (node && | ||
// @ts-ignore looks like an image. | ||
node.alt || | ||
(includeImageAlt ? node.alt : '') || | ||
// @ts-ignore looks like a parent. | ||
('children' in node && all(node.children)) || | ||
(Array.isArray(node) && all(node)))) || | ||
('children' in node && all(node.children, includeImageAlt)) || | ||
(Array.isArray(node) && all(node, includeImageAlt)))) || | ||
'' | ||
@@ -28,5 +44,6 @@ ) | ||
* @param {Array.<unknown>} values | ||
* @param {boolean} includeImageAlt | ||
* @returns {string} | ||
*/ | ||
function all(values) { | ||
function all(values, includeImageAlt) { | ||
/** @type {Array.<string>} */ | ||
@@ -37,3 +54,3 @@ var result = [] | ||
while (++index < values.length) { | ||
result[index] = toString(values[index]) | ||
result[index] = one(values[index], includeImageAlt) | ||
} | ||
@@ -40,0 +57,0 @@ |
{ | ||
"name": "mdast-util-to-string", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "mdast utility to get the plain text content of a node", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -43,7 +43,7 @@ # mdast-util-to-string | ||
### `toString(node)` | ||
### `toString(node[, options])` | ||
Get the text content of a [node][] or list of nodes. | ||
The algorithm checks `value` of `node`, then `alt`, and finally `title`. | ||
The algorithm checks `value` of `node` and then `alt`. | ||
If no value is found, the algorithm checks the children of `node` and joins them | ||
@@ -55,2 +55,6 @@ (without spaces or newlines). | ||
###### `options.includeImageAlt` | ||
Whether to use `alt` (`boolean`, default: `true`) | ||
## Security | ||
@@ -57,0 +61,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8948
67
138