|
| 1 | +interface GetTextOptions { |
| 2 | + treatBlockAsNewline?: boolean; |
| 3 | + collapseSpaces?: boolean; |
| 4 | + trimResult?: boolean; |
| 5 | +} |
| 6 | + |
| 7 | +const BLOCK_ELEMENTS = [ |
| 8 | + 'div', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', |
| 9 | + 'ul', 'ol', 'li', 'table', 'tr', 'td', 'th', |
| 10 | + 'section', 'article', 'header', 'footer', 'nav', |
| 11 | + 'aside', 'main', 'figure', 'figcaption', 'blockquote', |
| 12 | + 'pre', 'form', 'fieldset', 'legend', 'dl', 'dt', 'dd', |
| 13 | + 'hr', 'br' |
| 14 | +]; |
| 15 | + |
| 16 | +function isBlockElement(node: Node): boolean { |
| 17 | + if (node.nodeType !== Node.ELEMENT_NODE) return false; |
| 18 | + const element = node as HTMLElement; |
| 19 | + |
| 20 | + |
| 21 | + if (BLOCK_ELEMENTS.includes(element.tagName.toLowerCase())) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + const style = window.getComputedStyle(element); |
| 27 | + return style.display === 'block' || |
| 28 | + style.display === 'flex' || |
| 29 | + style.display === 'grid' || |
| 30 | + style.display.startsWith('table'); |
| 31 | +} |
| 32 | + |
| 33 | +function isElementVisible(element: HTMLElement): boolean { |
| 34 | + const style = window.getComputedStyle(element); |
| 35 | + return style.display !== 'none' && |
| 36 | + style.visibility !== 'hidden' && |
| 37 | + style.opacity !== '0'; |
| 38 | +} |
| 39 | + |
| 40 | +export function getTextFromDOM( |
| 41 | + node: Node, |
| 42 | + options: GetTextOptions = {} |
| 43 | +): string { |
| 44 | + const { |
| 45 | + treatBlockAsNewline = true, |
| 46 | + collapseSpaces = true, |
| 47 | + trimResult = true |
| 48 | + } = options; |
| 49 | + |
| 50 | + let result = ''; |
| 51 | + let lastChar = ''; |
| 52 | + |
| 53 | + function processNode(currentNode: Node, isBlockContext: boolean) { |
| 54 | + if (!currentNode) return; |
| 55 | + if (currentNode.nodeType === Node.ELEMENT_NODE) { |
| 56 | + const element = currentNode as HTMLElement; |
| 57 | + if (!isElementVisible(element)) return; |
| 58 | + |
| 59 | + const isBlock = isBlockElement(currentNode); |
| 60 | + const tagName = element.tagName.toLowerCase(); |
| 61 | + |
| 62 | + if (tagName === 'br') { |
| 63 | + result += '\n'; |
| 64 | + lastChar = '\n'; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (tagName === 'hr') { |
| 69 | + result += '\n---\n'; |
| 70 | + lastChar = '\n'; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + if (tagName === 'pre') { |
| 76 | + const text = element.textContent || ''; |
| 77 | + if (text) { |
| 78 | + result += text; |
| 79 | + lastChar = text[text.length - 1] || ''; |
| 80 | + } |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const shouldAddNewline = treatBlockAsNewline && isBlock; |
| 85 | + const separator = shouldAddNewline ? '\n' : ' '; |
| 86 | + |
| 87 | + if (isBlock && result.length > 0 && lastChar !== '\n') { |
| 88 | + result += separator; |
| 89 | + lastChar = separator; |
| 90 | + } |
| 91 | + |
| 92 | + const currentIsBlockContext = isBlock || isBlockContext; |
| 93 | + for (const childNode of Array.from(element.childNodes)) { |
| 94 | + processNode(childNode, currentIsBlockContext); |
| 95 | + } |
| 96 | + |
| 97 | + if (isBlock && result.length > 0 && lastChar !== '\n') { |
| 98 | + result += separator; |
| 99 | + lastChar = separator; |
| 100 | + } |
| 101 | + } else if (currentNode.nodeType === Node.TEXT_NODE) { |
| 102 | + let text = currentNode.textContent || ''; |
| 103 | + if (text.trim() === '') return; |
| 104 | + text = text.replace(/\s+/g, ' '); |
| 105 | + if (text.startsWith(' ')) { |
| 106 | + if (result.length > 0 && lastChar !== ' ' && lastChar !== '\n') { |
| 107 | + result += ' '; |
| 108 | + lastChar = ' '; |
| 109 | + } |
| 110 | + text = text.substring(1); |
| 111 | + } |
| 112 | + |
| 113 | + if (text) { |
| 114 | + const endsWithSpace = text.endsWith(' '); |
| 115 | + const cleanText = endsWithSpace ? text.slice(0, -1) : text; |
| 116 | + result += cleanText; |
| 117 | + lastChar = cleanText[cleanText.length - 1] || ''; |
| 118 | + if (endsWithSpace && lastChar !== ' ' && lastChar !== '\n') { |
| 119 | + result += ' '; |
| 120 | + lastChar = ' '; |
| 121 | + } |
| 122 | + } |
| 123 | + } else { |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + const initialIsBlock = isBlockElement(node); |
| 130 | + processNode(node, initialIsBlock); |
| 131 | + |
| 132 | + if (collapseSpaces) { |
| 133 | + result = result.replace(/[ \t]+/g, ' '); |
| 134 | + result = result.replace(/\n{3,}/g, '\n\n'); |
| 135 | + } |
| 136 | + |
| 137 | + if (trimResult) { |
| 138 | + result = result.trim(); |
| 139 | + } |
| 140 | + |
| 141 | + return result; |
| 142 | +} |
0 commit comments