* Extracts plain text from a Google Docs API structured document response. * Headings are prefixed with Markdown-style `#` markers.
(doc: DocsDocument)
| 79 | * Headings are prefixed with Markdown-style `#` markers. |
| 80 | */ |
| 81 | function extractTextFromDocsBody(doc: DocsDocument): string { |
| 82 | const elements = doc.body?.content |
| 83 | if (!elements) return '' |
| 84 | |
| 85 | const parts: string[] = [] |
| 86 | |
| 87 | for (const element of elements) { |
| 88 | const paragraph = element.paragraph |
| 89 | if (!paragraph?.elements) continue |
| 90 | |
| 91 | const prefix = headingPrefix(paragraph.paragraphStyle?.namedStyleType) |
| 92 | /** |
| 93 | * Each paragraph's final `textRun.content` already ends with `\n`. Strip |
| 94 | * it before joining with `\n` so a heading followed by a body paragraph |
| 95 | * is separated by a single newline, not two. |
| 96 | */ |
| 97 | const text = paragraph.elements |
| 98 | .map((el) => el.textRun?.content ?? '') |
| 99 | .join('') |
| 100 | .replace(/\n+$/, '') |
| 101 | |
| 102 | if (text.trim()) { |
| 103 | parts.push(`${prefix}${text}`) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return parts.join('\n').trim() |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Fetches the structured content of a Google Doc via the Docs API and |
no test coverage detected