* Formats a CMS item's field data into structured plain text.
(item: WebflowItem, collectionName: string)
| 40 | * Formats a CMS item's field data into structured plain text. |
| 41 | */ |
| 42 | function itemToPlainText(item: WebflowItem, collectionName: string): string { |
| 43 | const lines: string[] = [] |
| 44 | const fieldData = item.fieldData || {} |
| 45 | |
| 46 | const title = (fieldData.name as string) || (fieldData.title as string) || 'Untitled' |
| 47 | lines.push(`# ${title}`) |
| 48 | lines.push(`Collection: ${collectionName}`) |
| 49 | |
| 50 | for (const [key, value] of Object.entries(fieldData)) { |
| 51 | if (value == null) continue |
| 52 | if (key === 'name' || key === 'title') continue |
| 53 | |
| 54 | if (Array.isArray(value)) { |
| 55 | const items = value.map((v) => { |
| 56 | if (typeof v === 'object' && v !== null) { |
| 57 | return JSON.stringify(v) |
| 58 | } |
| 59 | return String(v) |
| 60 | }) |
| 61 | lines.push(`${key}: ${items.join(', ')}`) |
| 62 | } else if (typeof value === 'object') { |
| 63 | lines.push(`${key}: ${JSON.stringify(value)}`) |
| 64 | } else if (typeof value === 'string' && /<[a-z][^>]*>/i.test(value)) { |
| 65 | lines.push(`${key}: ${htmlToPlainText(value)}`) |
| 66 | } else { |
| 67 | lines.push(`${key}: ${String(value)}`) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return lines.join('\n') |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Extracts a human-readable title from a Webflow CMS item. |
no test coverage detected