Urara-Blog/node_modules/.pnpm-store/v3/files/de/f04a1b292db23d7660e7b7bc8965235a5619b1bf5f61aec0cad6cb2d07c2beb35cd6b51fedcbd7be809271f4093dec36dbbb8d9a9f9c14f371250b6c73d806
2022-08-14 01:14:53 +08:00

55 lines
1.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @typedef Options
* @property {boolean} [includeImageAlt=true]
*/
/**
* Get the text content of a node.
* Prefer the nodes 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('')
}