# The Presentation Layer: HTML as Codec Knowledge lives in documents, but documents live in the world. A graph in memory is computationally powerful, but a graph on a screen is where human understanding happens. We read, we scan, we follow links, we recognize typographic hierarchy. The web is the universal reading surface, and HTML is its native tongue. But historically, HTML has been a graveyard for semantics. When knowledge is rendered for the screen, the structural meaning is typically stripped away, leaving only visual formatting behind. The graph is lost in translation. The Semantic Web attempted to solve this with RDFa and JSON-LD — embedding machine-readable triples directly into HTML attributes or script tags. But these approaches suffer from a fundamental impedance mismatch. RDFa tries to map a stream-based scope (the document flow) onto a tree-based scope (the DOM hierarchy), resulting in nested spans, parser ambiguities, and brittle roundtrips. JSON-LD solves the machine side but abandons the presentation side entirely, creating a dual-source-of-truth problem where the visual HTML and the semantic JSON can drift out of sync. MD-LD rejects this compromise. It begins from a different premise: HTML does not need to *be* the graph. HTML only needs to be a **lossless presentation codec** for the text that produced the graph. This document describes the `render()` and `deconstruct()` pair — the bridge between the `Quad[]` runtime and the human eye. It explains how MD-LD turns HTML into a first-class transport format for knowledge graphs, enabling server/client equivalence, offline-first graph extraction, and privacy-preserving publishing, all without the overhead of the Semantic Web stack. --- ## HTML as a Lossless Codec The core insight of the MD-LD presentation layer is that the rendered HTML must be perfectly reversible. If you render an MD-LD document to HTML, and then deconstruct that HTML back to MD-LD, the resulting text must parse into the exact same quads as the original. ```js import { parse, render, deconstruct } from 'mdld-parse' const original = parse({ text: mdldString }) const html = render(mdldString) const reconstructed = parse({ text: deconstruct(html) }) // The invariant holds: // original.quads === reconstructed.quads ``` This is not a "best effort" serialization. It is a mathematical guarantee. The HTML is not a rendering of the graph; it is a serialization of the text that produced the graph. Because the text is the source of truth, and the HTML preserves the text, the HTML becomes a lossless transport layer. ```js const html = render(` [alice] # Alice {=alice:me .prov:Person label} Knows [Bob] {+alice:bob ?alice:knows} `) ``` The output is clean, semantic HTML: ```html

Alice

Knows Bob

``` Notice what is absent. There is no `typeof="prov:Person"`. There is no `rel="alice:knows"`. There are no nested spans attempting to chain subjects and objects. The renderer deliberately avoids RDFa. Instead, it relies on a single, powerful contract: the `data-annotation` attribute. --- ## The Annotation Contract The `data-annotation` attribute holds the exact, verbatim MD-LD syntax that generated the element, including the curly braces. This is the bridge back to the source. Why keep the braces? Why not just store the parsed JSON or the raw tokens? Because the braces make the attribute a **self-contained, valid MD-LD token**. 1. **DevTools as Debugger:** When you inspect an element in the browser, you see `{=alice:me .prov:Person label}`. You can copy it, paste it into a scratchpad, and it is immediately valid MD-LD. The inspector becomes a direct window into the source document. 2. **Trivial Reconstruction:** The `deconstruct()` function does not need to reverse-engineer a complex AST from HTML attributes. It simply finds the visible text of the element, appends the `data-annotation`, and reconstructs the original Markdown line. 3. **CSS Hooks:** The braces provide syntactic anchors for CSS attribute selectors, allowing you to style elements based on their semantic role without cluttering the DOM with ontology-specific classes. ```css /* Style all subject declarations */ [data-annotation^="{="] { font-weight: bold; } /* Style all retractions */ [data-annotation*=" -"] { text-decoration: line-through; opacity: 0.6; } /* Style specific entities */ [data-iri$="Person"] { border-left: 3px solid green; } ``` This is the first crystallization point: **presentation should not be coupled to ontology**. The renderer provides structural classes (`.mdld-heading`, `.mdld-link`, `.typed`) and raw data attributes (`data-iri`, `data-annotation`). How you style a `prov:Person` versus a `schema:Recipe` is a decision for your application's CSS, not the parser's HTML output. The renderer remains minimal, focused, and entirely agnostic to your vocabulary. --- ## Standard Markdown and Raw HTML MD-LD is a strict superset of Markdown, and the renderer honors that contract. Anything Markdown can express, MD-LD renders — the semantic layer is additive, never subtractive. **Raw HTML blocks** follow the CommonMark rules. A line beginning with a block-level tag (``, `
`, `
`, headings, lists, …) opens an HTML block that runs until a blank line; `