1
1
/**
2
+ * @typedef {import('mdast').Root|import('mdast').Content } Node
3
+ *
2
4
* @typedef Options
3
5
* @property {boolean } [includeImageAlt=true]
4
6
*/
8
10
* Prefer the node’s plain-text fields, otherwise serialize its children,
9
11
* and if the given value is an array, serialize the nodes in it.
10
12
*
11
- * @param {unknown } node
13
+ * @param {unknown } value
12
14
* @param {Options } [options]
13
15
* @returns {string }
14
16
*/
15
- export function toString ( node , options = { } ) {
17
+ export function toString ( value , options = { } ) {
16
18
const { includeImageAlt = true } = options
17
- return one ( node , includeImageAlt )
19
+ return one ( value , includeImageAlt )
18
20
}
19
21
20
22
/**
21
- * @param {unknown } node
23
+ * @param {unknown } value
22
24
* @param {boolean } includeImageAlt
23
25
* @returns {string }
24
26
*/
25
- function one ( node , includeImageAlt ) {
27
+ function one ( value , includeImageAlt ) {
26
28
return (
27
- ( node &&
28
- typeof node === 'object' &&
29
- // @ts -ignore looks like a literal.
30
- ( node . value ||
31
- // @ts -ignore looks like an image.
32
- ( includeImageAlt ? node . alt : '' ) ||
33
- // @ts -ignore looks like a parent.
34
- ( 'children' in node && all ( node . children , includeImageAlt ) ) ||
35
- ( Array . isArray ( node ) && all ( node , includeImageAlt ) ) ) ) ||
29
+ ( node ( value ) &&
30
+ ( ( 'value' in value && value . value ) ||
31
+ ( includeImageAlt && 'alt' in value && value . alt ) ||
32
+ ( 'children' in value && all ( value . children , includeImageAlt ) ) ) ) ||
33
+ ( Array . isArray ( value ) && all ( value , includeImageAlt ) ) ||
36
34
''
37
35
)
38
36
}
@@ -53,3 +51,11 @@ function all(values, includeImageAlt) {
53
51
54
52
return result . join ( '' )
55
53
}
54
+
55
+ /**
56
+ * @param {unknown } value
57
+ * @returns {value is Node }
58
+ */
59
+ function node ( value ) {
60
+ return Boolean ( value && typeof value === 'object' )
61
+ }
0 commit comments