//e.g., // { // Text: [ // 'This is a test', // { Text: [Array] }, // { Text: [Array] }, // { Text: [Array] } // ] // } export type Text = string | { Text: string[] } | Array export interface DocumentConstructor { Text?: Array | string | { Text: string[] } } // documents contain text and templates // ignore templates // just select the text for rendering export default class Document { public Text?: Array | string constructor(opts: DocumentConstructor) { if (opts.Text) { this.Text = this.parseTextTag(opts.Text) } } public toString(): string { if (typeof this.Text === 'string') { return this.Text } else if (this.Text) { return this.Text.join('').replace(/^[ \t]+/gm, '') } else { return '' } } private parseTextTag(text: Text): string { // if text is an array if (Array.isArray(text)) { return text.map((t) => { if (typeof t === 'string') { return t.replace(/^[ \t]+/gm, '').concat('\n') } else if (Array.isArray(t.Text)) { return t.Text.join('').replace(/^[ \t]+/gm, '') } else { return '' } }).join('') } // if text is a string if (typeof text === 'string') { return text } // if text is an object if (typeof text === 'object') { const inner = text.Text as string|string[]|Text if (Array.isArray(inner)) { return inner.join('').replace(/^[ \t]+/gm, '') } else if (typeof inner === 'string') { return inner } else { return this.parseTextTag(inner) } } } }