11/**
2+ * @typedef {import('mdast').Root|import('mdast').Content } Node
3+ *
24 * @typedef Options
35 * @property {boolean } [includeImageAlt=true]
46 */
810 * Prefer the node’s plain-text fields, otherwise serialize its children,
911 * and if the given value is an array, serialize the nodes in it.
1012 *
11- * @param {unknown } node
13+ * @param {unknown } value
1214 * @param {Options } [options]
1315 * @returns {string }
1416 */
15- export function toString ( node , options = { } ) {
17+ export function toString ( value , options = { } ) {
1618 const { includeImageAlt = true } = options
17- return one ( node , includeImageAlt )
19+ return one ( value , includeImageAlt )
1820}
1921
2022/**
21- * @param {unknown } node
23+ * @param {unknown } value
2224 * @param {boolean } includeImageAlt
2325 * @returns {string }
2426 */
25- function one ( node , includeImageAlt ) {
27+ function one ( value , includeImageAlt ) {
2628 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 ) ) ||
3634 ''
3735 )
3836}
@@ -53,3 +51,11 @@ function all(values, includeImageAlt) {
5351
5452 return result . join ( '' )
5553}
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