|
| 1 | +export interface ChunkItem { |
| 2 | + text: string |
| 3 | + element: Element | null |
| 4 | +} |
| 5 | + |
1 | 6 | export function useArticleText() { |
2 | | - const getArticleText = (): string => { |
3 | | - if (typeof document === 'undefined') return '' |
| 7 | + const getArticleChunks = (): ChunkItem[] => { |
| 8 | + if (typeof document === 'undefined') return [] |
4 | 9 | const docElement = document.querySelector('.vp-doc') |
5 | | - if (!docElement) return '' |
6 | | - |
7 | | - const clone = docElement.cloneNode(true) as HTMLElement |
8 | | - |
9 | | - // Remove non-text UI elements |
10 | | - clone.querySelectorAll('.line-numbers-wrapper, .copy, .header-anchor, script, style, .read-aloud-container, nav, footer, .VPNav, .VPFooter, button, img, svg, .vp-code, pre, code').forEach(el => el.remove()) |
11 | | - |
12 | | - let text = clone.innerText || clone.textContent || '' |
13 | | - |
14 | | - // Normalize text for natural speech pacing |
15 | | - text = text |
16 | | - .replace(/#{1,6}\s+/g, '. ') |
17 | | - .replace(/^[\s]*[-*+]\s+/gm, '. ') |
18 | | - .replace(/\n+/g, '. ') |
19 | | - .replace(/\s+/g, ' ') |
20 | | - .replace(/\b(e\.g\.|i\.e\.|vs\.|etc\.)/gi, match => match.replace(/\./g, '')) |
21 | | - .replace(/([.!?])\s*([A-Z])/g, '$1 $2') |
22 | | - .replace(/`([^`]+)`/g, '$1') |
23 | | - .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') |
24 | | - .trim() |
25 | | - |
26 | | - return text |
27 | | - } |
| 10 | + if (!docElement) return [] |
| 11 | + |
| 12 | + const elements = docElement.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, blockquote') |
| 13 | + const chunks: ChunkItem[] = [] |
| 14 | + |
| 15 | + elements.forEach((el) => { |
| 16 | + // Exclude UI controls, read-aloud component, pre & code blocks |
| 17 | + if (el.closest('.read-aloud-container, pre, .vp-code')) return |
| 18 | + |
| 19 | + const clone = el.cloneNode(true) as HTMLElement |
| 20 | + clone.querySelectorAll('.line-numbers-wrapper, .copy, .header-anchor, script, style, .read-aloud-container, button, img, svg, code').forEach(child => child.remove()) |
| 21 | + |
| 22 | + let text = clone.innerText || clone.textContent || '' |
| 23 | + text = text |
| 24 | + .replace(/\s+/g, ' ') |
| 25 | + .replace(/\b(e\.g\.|i\.e\.|vs\.|etc\.)/gi, match => match.replace(/\./g, '')) |
| 26 | + .replace(/`([^`]+)`/g, '$1') |
| 27 | + .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') |
| 28 | + .trim() |
| 29 | + |
| 30 | + if (!text) return |
| 31 | + |
| 32 | + const rawSentences = text.match(/[^.!?\n]+[.!?\n]+/g) || [text] |
| 33 | + let current = '' |
28 | 34 |
|
29 | | - const splitIntoChunks = (text: string): string[] => { |
30 | | - const rawSentences = text.match(/[^.!?\n]+[.!?\n]+/g) || [text] |
31 | | - const result: string[] = [] |
32 | | - let current = '' |
33 | | - |
34 | | - for (const sentence of rawSentences) { |
35 | | - const trimmed = sentence.trim() |
36 | | - if (!trimmed) continue |
37 | | - if ((current + ' ' + trimmed).length > 250) { |
38 | | - if (current) result.push(current.trim()) |
39 | | - current = trimmed |
40 | | - } else { |
41 | | - current = current ? current + ' ' + trimmed : trimmed |
| 35 | + for (const sentence of rawSentences) { |
| 36 | + const trimmed = sentence.trim() |
| 37 | + if (!trimmed) continue |
| 38 | + if ((current + ' ' + trimmed).length > 250) { |
| 39 | + if (current) chunks.push({ text: current.trim(), element: el }) |
| 40 | + current = trimmed |
| 41 | + } else { |
| 42 | + current = current ? current + ' ' + trimmed : trimmed |
| 43 | + } |
42 | 44 | } |
| 45 | + if (current.trim()) { |
| 46 | + chunks.push({ text: current.trim(), element: el }) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + return chunks |
| 51 | + } |
| 52 | + |
| 53 | + const highlightElement = (el: Element | null) => { |
| 54 | + if (typeof document === 'undefined') return |
| 55 | + |
| 56 | + document.querySelectorAll('.read-aloud-highlight').forEach(item => { |
| 57 | + item.classList.remove('read-aloud-highlight') |
| 58 | + }) |
| 59 | + |
| 60 | + if (el) { |
| 61 | + el.classList.add('read-aloud-highlight') |
| 62 | + el.scrollIntoView({ behavior: 'smooth', block: 'nearest' }) |
43 | 63 | } |
44 | | - if (current.trim()) result.push(current.trim()) |
45 | | - return result.length > 0 ? result : [text] |
| 64 | + } |
| 65 | + |
| 66 | + const clearHighlight = () => { |
| 67 | + if (typeof document === 'undefined') return |
| 68 | + document.querySelectorAll('.read-aloud-highlight').forEach(item => { |
| 69 | + item.classList.remove('read-aloud-highlight') |
| 70 | + }) |
46 | 71 | } |
47 | 72 |
|
48 | 73 | return { |
49 | | - getArticleText, |
50 | | - splitIntoChunks, |
| 74 | + getArticleChunks, |
| 75 | + highlightElement, |
| 76 | + clearHighlight, |
51 | 77 | } |
52 | 78 | } |
0 commit comments