basant307/AI_Governance_Project
048
1/**2 * @typedef {import('mdast').Nodes} Nodes3 *4 * @typedef Options5 * Configuration (optional).6 * @property {boolean | null | undefined} [includeImageAlt=true]7 * Whether to use `alt` for `image`s (default: `true`).8 * @property {boolean | null | undefined} [includeHtml=true]9 * Whether to use `value` of HTML (default: `true`).10 */11 12/** @type {Options} */13const emptyOptions = {}14 15/**16 * Get the text content of a node or list of nodes.17 *18 * Prefers the node’s plain-text fields, otherwise serializes its children,19 * and if the given value is an array, serialize the nodes in it.20 *21 * @param {unknown} [value]22 * Thing to serialize, typically `Node`.23 * @param {Options | null | undefined} [options]24 * Configuration (optional).25 * @returns {string}26 * Serialized `value`.27 */28export function toString(value, options) {29 const settings = options || emptyOptions30 const includeImageAlt =31 typeof settings.includeImageAlt === 'boolean'32 ? settings.includeImageAlt33 : true34 const includeHtml =35 typeof settings.includeHtml === 'boolean' ? settings.includeHtml : true36 37 return one(value, includeImageAlt, includeHtml)38}39 40/**41 * One node or several nodes.42 *43 * @param {unknown} value44 * Thing to serialize.45 * @param {boolean} includeImageAlt46 * Include image `alt`s.47 * @param {boolean} includeHtml48 * Include HTML.49 * @returns {string}50 * Serialized node.51 */52function one(value, includeImageAlt, includeHtml) {53 if (node(value)) {54 if ('value' in value) {55 return value.type === 'html' && !includeHtml ? '' : value.value56 }57 58 if (includeImageAlt && 'alt' in value && value.alt) {59 return value.alt60 }61 62 if ('children' in value) {63 return all(value.children, includeImageAlt, includeHtml)64 }65 }66 67 if (Array.isArray(value)) {68 return all(value, includeImageAlt, includeHtml)69 }70 71 return ''72}73 74/**75 * Serialize a list of nodes.76 *77 * @param {Array<unknown>} values78 * Thing to serialize.79 * @param {boolean} includeImageAlt80 * Include image `alt`s.81 * @param {boolean} includeHtml82 * Include HTML.83 * @returns {string}84 * Serialized nodes.85 */86function all(values, includeImageAlt, includeHtml) {87 /** @type {Array<string>} */88 const result = []89 let index = -190 91 while (++index < values.length) {92 result[index] = one(values[index], includeImageAlt, includeHtml)93 }94 95 return result.join('')96}97 98/**99 * Check if `value` looks like a node.100 *101 * @param {unknown} value102 * Thing.103 * @returns {value is Nodes}104 * Whether `value` is a node.105 */106function node(value) {107 return Boolean(value && typeof value === 'object')108}109 