YAML Front Matter Preservation in CodeChat Editor ================================================= Status ------ Final specification. The rules below are normative for implementations. Purpose ------- Preserve YAML metadata at the beginning of a Markdown document across Markdown to HTML and HTML to Markdown conversion in CodeChat Editor. YAML is opaque text: the editor must not parse, validate, reorder, or reformat it. Opaqueness does not exempt it from this specification's character, UTF-8, and HTML representation rules. Scope ----- This specification covers only a YAML block at the start of Markdown, recognized by pulldown-cmark as a YAML-style metadata block. HTML represents it with `

Hello

``` Whitespace around the element may follow the existing HTML serializer, but must not become part of the YAML payload. HTML to Markdown ---------------- 1. A matching element is a YAML element (see [Terms](#terms)). Extra attributes are allowed. 2. Front matter can only be the first element of a Markdown document's HTML: only comments and text consisting solely of TAB, LF, FF, CR, or SPACE may precede it. If the first element is a matching element, decode it using the representation rules below. 3. A matching element in any other position, including any matching element in a doc block of a source file, and a second matching element, are an explicit conversion error. Never select or discard one silently. 4. Recognize and exclude elements on the parsed DOM before invoking the normal converter. Exclude exactly the recognized YAML element node from the HTML passed to the converter. Do not remove adjacent text nodes or comments. The excluded element must not produce visible Markdown or become an ordinary script or code block. 5. Prepend a front matter block to the converted Markdown using canonical LF delimiters and the exact payload character sequence: `---\n` + payload + LF + `---\n`, then one blank line if converted Markdown follows. Word wrapping (dprint, in `HtmlToMarkdownWrapped`) must leave the front matter unchanged. 6. If no YAML element is found, convert the HTML as usual. The YAML element must not change conversion of the rest of the document. Exclusion occurs after DOM parsing and before the normal converter; retained text nodes and comments keep the converter's existing behavior. Preservation and error cases ---------------------------- * Preserve payload characters and ordering, including indentation, spaces, comments, blank lines, and YAML key order. Line endings follow the editor's existing handling (`eol_convert` in `server/src/translation.rs`). Reconstructed delimiter lines use LF. * An empty payload read from HTML is an explicit conversion error, since pulldown-cmark does not read `---\n---\n` as a metadata block. * If the element has a `data-codechat-yaml-base64` attribute, the payload is its value decoded as standard RFC 4648 Base64, then as strict UTF-8. Invalid Base64 or invalid UTF-8 is an explicit conversion error; never discard the element. An element with that attribute must have no text content; otherwise report an explicit conversion error. * Without that attribute, the payload is the element's DOM `textContent` after HTML parsing; do not perform another character-reference or parser-normalization pass. * Emit the payload as script text when it contains neither U+003C (less-than) nor U+0000 (NUL). Write literal text, including ampersands. Otherwise, put the payload's UTF-8 bytes, Base64-encoded, in `data-codechat-yaml-base64` and leave the element empty. Both representations use the same script type. * Do not reject a payload because it is invalid YAML. YAML validation is out of scope. * If a payload read from HTML (direct text or Base64) has a first line that is empty or contains only whitespace, or contains a line that pulldown-cmark reads as a closing delimiter (`---` or `...`, optionally followed by spaces), report a conversion error before emitting ambiguous Markdown. * `` sequence. * Text that pulldown-cmark does not read as front matter (an empty block, a blank first line, no closing delimiter, a `---` block after other content) produces no YAML element and renders as ordinary Markdown. * HTML without a YAML element converts as before; a first YAML element is excluded from ordinary Markdown conversion. * A YAML element that is not first, and a second YAML element, cause an explicit error and are never silently discarded. * `type="APPLICATION/YAML"` and `type="application/yaml "` are not matches. * Invalid Base64, Base64 decoding to invalid UTF-8, a Base64 element with text content, an empty payload, a blank first line, and a closing-delimiter line in a payload read from HTML report explicit conversion errors. * Markdown containing `` still has that element removed by sanitization. * The real editor serializer/parser does not display or remove metadata when opening and saving.