|
| 1 | +/** |
| 2 | + * @typedef Options |
| 3 | + * @property {boolean} [includeImageAlt=true] |
| 4 | + */ |
| 5 | + |
1 | 6 | /** |
2 | 7 | * Get the text content of a node. |
3 | 8 | * Prefer the node’s plain-text fields, otherwise serialize its children, |
4 | 9 | * and if the given value is an array, serialize the nodes in it. |
5 | 10 | * |
6 | 11 | * @param {unknown} node |
| 12 | + * @param {Options} [options] |
| 13 | + * @returns {string} |
| 14 | + */ |
| 15 | +export function toString(node, options) { |
| 16 | + var {includeImageAlt = true} = options || {} |
| 17 | + return one(node, includeImageAlt) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {unknown} node |
| 22 | + * @param {boolean} includeImageAlt |
7 | 23 | * @returns {string} |
8 | 24 | */ |
9 | | -export function toString(node) { |
| 25 | +function one(node, includeImageAlt) { |
10 | 26 | return ( |
11 | 27 | (node && |
12 | 28 | typeof node === 'object' && |
13 | 29 | // @ts-ignore looks like a literal. |
14 | 30 | (node.value || |
15 | 31 | // @ts-ignore looks like an image. |
16 | | - node.alt || |
| 32 | + (includeImageAlt ? node.alt : '') || |
17 | 33 | // @ts-ignore looks like a parent. |
18 | | - ('children' in node && all(node.children)) || |
19 | | - (Array.isArray(node) && all(node)))) || |
| 34 | + ('children' in node && all(node.children, includeImageAlt)) || |
| 35 | + (Array.isArray(node) && all(node, includeImageAlt)))) || |
20 | 36 | '' |
21 | 37 | ) |
22 | 38 | } |
23 | 39 |
|
24 | 40 | /** |
25 | 41 | * @param {Array.<unknown>} values |
| 42 | + * @param {boolean} includeImageAlt |
26 | 43 | * @returns {string} |
27 | 44 | */ |
28 | | -function all(values) { |
| 45 | +function all(values, includeImageAlt) { |
29 | 46 | /** @type {Array.<string>} */ |
30 | 47 | var result = [] |
31 | 48 | var index = -1 |
32 | 49 |
|
33 | 50 | while (++index < values.length) { |
34 | | - result[index] = toString(values[index]) |
| 51 | + result[index] = one(values[index], includeImageAlt) |
35 | 52 | } |
36 | 53 |
|
37 | 54 | return result.join('') |
|
0 commit comments