import { Node } from 'prosemirror-model'; /** Search/preview representation (data-model.md `page_content_cache`). */ export function docToPlainText(doc: Node): string { const text = doc.textBetween(0, doc.content.size, '\n\n', (leaf) => { if (leaf.type.name === 'image') return (leaf.attrs.alt as string) ?? ''; // A wikilink contributes its shown text (explicit display or the slug) so // search and previews include what the reader sees (issue #46). if (leaf.type.name === 'wikilink') { return (leaf.attrs.displayText as string | null) ?? (leaf.attrs.targetSlug as string); } // A mention reads as `@username` (issue #150), so search finds it. if (leaf.type.name === 'mention') return `@${leaf.attrs.username as string}`; // A date marker contributes its ISO date (issue #152). if (leaf.type.name === 'date_marker') return leaf.attrs.date as string; return ''; }); return text.replace(/\n{3,}/g, '\n\n').trim(); }