|
| 1 | +/** |
| 2 | + * @typedef {import('hast').Root} Root |
| 3 | + * @typedef {import('hast').Content} Content |
| 4 | + * @typedef {import('hast').Text} Text |
| 5 | + * @typedef {Root|Content} Node |
| 6 | + * |
| 7 | + * @typedef Options |
| 8 | + * Configuration. |
| 9 | + * @property {string} [comment='more'] |
| 10 | + * Comment value to search for. |
| 11 | + * @property {number} [maxSearchSize=2048] |
| 12 | + * How far to search for the comment before bailing. |
| 13 | + * The goal of this project is to find user-defined explicit excerpts, that |
| 14 | + * are assumed to be somewhat reasonably placed. |
| 15 | + * This option prevents searching giant documents for some comment |
| 16 | + * that probably won’t be found at the end. |
| 17 | + * @property {Content[]} [ignore=[]] |
| 18 | + * Nodes to exclude from the resulting tree. |
| 19 | + * These are not counted towards `size`. |
| 20 | + */ |
| 21 | + |
| 22 | +import {truncate} from 'hast-util-truncate' |
| 23 | + |
| 24 | +/** |
| 25 | + * Truncate the tree to a certain comment. |
| 26 | + * Returns a copy of the given tree if a comment is found, and `undefined` |
| 27 | + * otherwise. |
| 28 | + * |
| 29 | + * @template {Node} Tree |
| 30 | + * @param {Tree} tree |
| 31 | + * @param {Options} [options] |
| 32 | + * @returns {Tree|undefined} |
| 33 | + */ |
| 34 | +export function excerpt(tree, options = {}) { |
| 35 | + const {comment = 'more', ignore, maxSearchSize = 2048} = options |
| 36 | + let found = false |
| 37 | + const result = preorder(truncate(tree, {ignore, size: maxSearchSize})) |
| 38 | + |
| 39 | + // @ts-expect-error: `result` is most likely a clone of `tree` |
| 40 | + return found ? result : undefined |
| 41 | + |
| 42 | + /** |
| 43 | + * @param {Node} node |
| 44 | + * @returns {Node|undefined} |
| 45 | + */ |
| 46 | + function preorder(node) { |
| 47 | + if (node.type === 'comment' && node.value.trim() === comment) { |
| 48 | + found = true |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if ( |
| 53 | + // @ts-expect-error: integrate w/ `mdast-util-mdx` |
| 54 | + (node.type === 'mdxFlowExpression' || |
| 55 | + // @ts-expect-error |
| 56 | + node.type === 'mdxTextExpression') && |
| 57 | + // @ts-expect-error |
| 58 | + node.data && |
| 59 | + // @ts-expect-error |
| 60 | + node.data.estree && |
| 61 | + // @ts-expect-error |
| 62 | + node.data.estree.comments && |
| 63 | + // @ts-expect-error |
| 64 | + node.data.estree.comments.some( |
| 65 | + (/** @type {import('acorn').Comment} */ node) => |
| 66 | + node.value.trim() === comment |
| 67 | + ) |
| 68 | + ) { |
| 69 | + found = true |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + /** @type {typeof node} */ |
| 74 | + const replacement = {...node} |
| 75 | + |
| 76 | + if ('children' in node) { |
| 77 | + /** @type {Content[]} */ |
| 78 | + const children = [] |
| 79 | + let index = -1 |
| 80 | + |
| 81 | + while (++index < node.children.length) { |
| 82 | + const result = preorder(node.children[index]) |
| 83 | + |
| 84 | + if (found) { |
| 85 | + break |
| 86 | + } |
| 87 | + |
| 88 | + // @ts-expect-error: assume content model matches. |
| 89 | + if (result) children.push(result) |
| 90 | + } |
| 91 | + |
| 92 | + // @ts-expect-error: assume content model matches. |
| 93 | + replacement.children = children |
| 94 | + } |
| 95 | + |
| 96 | + return replacement |
| 97 | + } |
| 98 | +} |
0 commit comments