|
1 | 1 | /** |
2 | | - * @typedef {import('mdast').Root|import('mdast').Content} Node |
3 | | - * |
4 | | - * @typedef Options |
5 | | - * Configuration (optional). |
6 | | - * @property {boolean} [includeImageAlt=true] |
7 | | - * Whether to use `alt` for `image`s. |
| 2 | + * @typedef {import('./lib/index.js').Options} Options |
8 | 3 | */ |
9 | 4 |
|
10 | | -/** |
11 | | - * Get the text content of a node or list of nodes. |
12 | | - * Prefers the node’s plain-text fields, otherwise serializes its children, |
13 | | - * and if the given value is an array, serialize the nodes in it. |
14 | | - * |
15 | | - * @param {unknown} value |
16 | | - * @param {Options} [options] |
17 | | - * @returns {string} |
18 | | - */ |
19 | | -export function toString(value, options = {}) { |
20 | | - const {includeImageAlt = true} = options |
21 | | - return one(value, includeImageAlt) |
22 | | -} |
23 | | - |
24 | | -/** |
25 | | - * @param {unknown} value |
26 | | - * @param {boolean} includeImageAlt |
27 | | - * @returns {string} |
28 | | - */ |
29 | | -function one(value, includeImageAlt) { |
30 | | - return ( |
31 | | - (node(value) && |
32 | | - (('value' in value && value.value) || |
33 | | - (includeImageAlt && 'alt' in value && value.alt) || |
34 | | - ('children' in value && all(value.children, includeImageAlt)))) || |
35 | | - (Array.isArray(value) && all(value, includeImageAlt)) || |
36 | | - '' |
37 | | - ) |
38 | | -} |
39 | | - |
40 | | -/** |
41 | | - * @param {Array<unknown>} values |
42 | | - * @param {boolean} includeImageAlt |
43 | | - * @returns {string} |
44 | | - */ |
45 | | -function all(values, includeImageAlt) { |
46 | | - /** @type {Array<string>} */ |
47 | | - const result = [] |
48 | | - let index = -1 |
49 | | - |
50 | | - while (++index < values.length) { |
51 | | - result[index] = one(values[index], includeImageAlt) |
52 | | - } |
53 | | - |
54 | | - return result.join('') |
55 | | -} |
56 | | - |
57 | | -/** |
58 | | - * @param {unknown} value |
59 | | - * @returns {value is Node} |
60 | | - */ |
61 | | -function node(value) { |
62 | | - return Boolean(value && typeof value === 'object') |
63 | | -} |
| 5 | +export {toString} from './lib/index.js' |
0 commit comments