# Markdown in and out `@texuli/editor/markdown` reads Markdown into an editor document and writes an editor document back as Markdown. It is framework-free, never touches the DOM and produces no HTML. It wraps the owned codec in [`@texuli/core/editor-markdown`](../../core/docs/core-editor.md#markdown-codec-editor-markdown) (ADR-003, [EDITOR_ARCHITECTURE](../../../docs/EDITOR_ARCHITECTURE.md) section 9), and it is not in the package barrel, so hosts that never read or write Markdown do not pay for it. ```ts import { documentToMarkdown, markdownToDocument } from '@texuli/editor/markdown'; // Untrusted by default: agent output, stored text, a pasted file. const { doc, diagnostics } = markdownToDocument('# Plan\n\n- [ ] Draft\n- [x] Review'); const saved = documentToMarkdown(doc); // '# Plan\n\n- [ ] Draft\n- [x] Review' ``` | Export | Meaning | | --- | --- | | `markdownToDocument(source, options?)` | Reads Markdown into `{ doc, diagnostics }`, a valid `EditorDocument`. Never throws; a non-string source is read as its string form | | `documentToMarkdown(doc, options?)` | Writes the document as Markdown that reads back to the same document | | `MarkdownToDocumentOptions` | `trust` (`'untrusted'` by default), `newId`, lower `limits`, `maxSource` (the source cap) and `extensions` (host inline extensions, below) | | `DocumentToMarkdownOptions` | `trust` (`'untrusted'` by default): the tier every link is checked at again before it is written; `extensions`: the host inline extensions written as tokens | | `MARKDOWN_CODEC_LIMITS` | `source` 100,000 characters, `inlineDepth` 6 | | `importMarkdown(text, options?)` | "Paste as Markdown": clipboard text read by the codec at the `untrusted` tier into a `PasteResult` (source `'markdown'`), the shape `@texuli/editor/paste` returns. The controller's `pasteMarkdown` loads it on demand | | `documentToDialect(doc, dialect, options?)` | Writes the document for a chat platform (below): `{ value, losses }`. Never throws | | `DocumentToDialectOptions` | `trust` (`'untrusted'` by default): the tier links are checked at again | | `DocumentToDialectResult`, `DialectLoss` | The text, and what the format lost or changed: `{ code, message, count }`, grouped by message | | `EDITOR_EXPORT_DIALECTS`, `EDITOR_DIALECT_NAMES` | The nine formats in menu order and their visible names | | `MarkdownDialect` | `'slack'`, `'discord'`, `'whatsapp'`, `'telegram-markdown-v2'`, `'telegram-html'`, `'teams'`, `'google-chat'`, `'mattermost'`, `'plain-text'` | ## What it reads and writes The closed subset the editor holds, the same one `@texuli/blocks` text blocks and the catalog's rich text parse: | Markdown | Editor document | | --- | --- | | Paragraphs; `\` or two spaces before a newline | `paragraph`; a `break` run | | `#` to `####` (`#####` and `######` read as level 4, with a `heading-clamped` diagnostic) | `heading` levels 1–4 | | `-`, `*`, `+` and `1.` or `1)` lists, nested by 2 to 4 spaces | `list` of kind `bullet` or `ordered` (with `start`) | | `- [ ]` and `- [x]` | `list` of kind `todo` with `checked` items | | `>` | `quote` (blocks nest at most 4 levels; deeper lines become text with a `depth-capped` diagnostic) | | Fenced code with an optional language | `code` with `lang` | | `---`, `***`, `___` | `divider` | | `![alt](ref)` alone in a paragraph | `media`, whose `ref` the host resolves (`resolveMedia`); inside text an image is its alt text | | `**strong**`, `*em*` or `_em_`, `~~strike~~`, `` `code` ``, `[text](href)`, `` | The `strong`, `em`, `strike`, `code` and `link` marks | The writer always uses the forms in the first column: `-` for bullets, `*` for em, three backticks for code (more when the code holds a run of backticks), four-space indents for nested blocks, and a blank line before a nested list. Writing is stable: what it writes reads and writes back unchanged. What the subset cannot spell is dropped on write: empty paragraphs, breaks in a heading (written as spaces), and breaks or whitespace at the edges of a block or a mark. ## Host inline extensions Mentions, references and other host entities round trip with their entity id when the host registers them and passes the same `extensions` both ways. A host registers a name and a sigil, never a pattern: the grammar is fixed, `{|