3
3
*
4
4
* @typedef Options
5
5
* Configuration (optional).
6
- * @property {boolean } [includeImageAlt=true]
6
+ * @property {boolean | null | undefined } [includeImageAlt=true]
7
7
* Whether to use `alt` for `image`s.
8
8
*/
9
9
10
10
/**
11
11
* Get the text content of a node or list of nodes.
12
+ *
12
13
* Prefers the node’s plain-text fields, otherwise serializes its children,
13
14
* and if the given value is an array, serialize the nodes in it.
14
15
*
15
16
* @param {unknown } value
16
- * @param {Options } [options]
17
+ * Thing to serialize, typically `Node`.
18
+ * @param {Options | null | undefined } [options]
19
+ * Configuration (optional).
17
20
* @returns {string }
21
+ * Serialized `value`.
18
22
*/
19
- export function toString ( value , options = { } ) {
20
- const { includeImageAlt = true } = options
21
- return one ( value , includeImageAlt )
23
+ export function toString ( value , options ) {
24
+ const includeImageAlt = ( options || { } ) . includeImageAlt
25
+ return one (
26
+ value ,
27
+ typeof includeImageAlt === 'boolean' ? includeImageAlt : true
28
+ )
22
29
}
23
30
24
31
/**
32
+ * One node or several nodes.
33
+ *
25
34
* @param {unknown } value
35
+ * Thing to serialize.
26
36
* @param {boolean } includeImageAlt
37
+ * Include image `alt`s.
27
38
* @returns {string }
39
+ * Serialized node.
28
40
*/
29
41
function one ( value , includeImageAlt ) {
30
42
return (
@@ -38,9 +50,14 @@ function one(value, includeImageAlt) {
38
50
}
39
51
40
52
/**
53
+ * Serialize a list of nodes.
54
+ *
41
55
* @param {Array<unknown> } values
56
+ * Thing to serialize.
42
57
* @param {boolean } includeImageAlt
58
+ * Include image `alt`s.
43
59
* @returns {string }
60
+ * Serialized nodes.
44
61
*/
45
62
function all ( values , includeImageAlt ) {
46
63
/** @type {Array<string> } */
@@ -55,8 +72,12 @@ function all(values, includeImageAlt) {
55
72
}
56
73
57
74
/**
75
+ * Check if `value` looks like a node.
76
+ *
58
77
* @param {unknown } value
78
+ * Thing.
59
79
* @returns {value is Node }
80
+ * Whether `value` is a node.
60
81
*/
61
82
function node ( value ) {
62
83
return Boolean ( value && typeof value === 'object' )
0 commit comments