import type { ContentEditorPreviewText } from './content-editor-preview-text' import { stripMarkdownSyntax } from './strip-markdown-syntax' const HEADING_LINE = /^\s{0,3}#{1,6}\s+/ const FENCE_LINE = /^\s{0,3}(```|~~~|\$\$)/ // The phone form shows the first lines of the content instead of the editor: a leading heading // becomes the preview title and the following prose its body. Code fences and tables are skipped // so the card reads as text. export function getContentEditorPreviewText(markdown: string): ContentEditorPreviewText { const lines = markdown.split('\n') let title: string | null = null const body: string[] = [] let insideFence = false for (const line of lines) { if (FENCE_LINE.test(line)) { insideFence = !insideFence continue } if (insideFence || line.trim().startsWith('|')) continue const text = stripMarkdownSyntax(line) if (!text) continue if (title === null && body.length === 0 && HEADING_LINE.test(line)) { title = text continue } body.push(text) if (body.join(' ').length > 400) break } return { title, body: body.join(' ') } }