Rich text / foldkit-richtext-markdown
Edit the Markdown itself
A SourceSession lets a user switch to editing raw Markdown and switch back without losing unedited IDs or unsupported marks. Keep the session value in your application Model while source mode is active:
import { closeSource, openSource } from 'foldkit-richtext-markdown'
// Enter source mode: draft starts as the printed document.
const session = openSource(parsed.document)
// Replace draft on each keystroke in the source textarea.
const edited = { ...session, draft: '# Title\n\nNew text\n' }
// Leave source mode (pure read, also usable as a live preview).
const closed = closeSource(edited, parsed.document, { mint: () => crypto.randomUUID() })openSource prints the document and preserves any marks Markdown lacks syntax for in unprintable. If the user exits without editing (closed.changed === false), closeSource returns the original Document untouched with its IDs and unprintable marks intact. When edited, closeSource parses the draft and returns diagnostics listing lost unprintable content followed by any parse warnings, letting your application warn before committing closed.document.
Pass closed.style back into the next openSource(document, { style }) so writers keep their preferred emphasis and bullet syntax across mode switches. Passing { selection } to openSource also maps the rich caret offset into session.caret, and updating caret in the textarea lets closeSource compute closed.selection back in the rich document (null when the draft is empty or block counts changed while unedited).