# MD-LD — Core Specification (Minimal)

**Markdown-Linked Data**

A deterministic, streaming-friendly semantic annotation layer for CommonMark Markdown that emits RDF quads **only** from explicit `{...}` annotations.

## 1. Core Rules

- Markdown-preserving (remove `{...}` → valid Markdown)
- No implicit semantics, explicit only
- Single-pass streaming, no backtracking
- No blank nodes, all IRIs
- Every quad traces to `{...}` source

## 2. Graph Model

**Facts**: Subject → Predicate → Object
- **IRIs**: Subjects + objects (identifiers)
- **Literals**: Objects only (data values)

## 3. Available Nodes

- **S**: Current subject (`{=IRI}`, `{=#fragment}`)
- **O**: Object resource (links, images, `{=IRI}`)
- **L**: Literal value (from value carrier)

## 4. Value Carriers

`{...}` attaches to exactly one:

**Inline**: `[text]` `*text*` `**text**` `` `text` ``
**Block**: `# Heading` `- item` `> quote` ````lang````
**Links**: `http://url` `[label](url)` `![alt](url)` `<url>`

**Rule**: `{...}` immediately follows carrier.

## 5. Attachment

1. Nearest preceding value carrier, OR
2. Stand alone (current subject or following list)

Ambiguous = no output.

## 6. Subjects

```
{=IRI}        # Full IRI, persists
{=#fragment}  # Relative to current subject
{+iri}        # Temporary object for local  ?/! predicates
{+#fragment}  # Temporary soft fragment
```

### Primary Metadata

MD-LD tracks four primary metadata fields during single-pass parsing:

| Field | Source | Purpose |
|-------|--------|---------|
| **primarySubject** | First non-fragment `{=subject}` | Document identity |
| **primaryType** | First `.Class` declaration | Document category |
| **primaryLabel** | First `{label}` literal | Human-readable name |
| **primaryComment** | First `{comment}` literal | Human-readable description |

**Selection Rules:**
- First occurrence only (fixed once detected)
- Never cleared on `{=}` reset
- Returns `null` if not declared

**parse()**: Returns `{ quads, remove, statements, origin, context, primarySubject, primary, md }`
- `primary`: `{ subject, type, label, comment }` (semantic surface descriptor)
**merge()**: Returns `{ quads, remove, statements, origin, context, primarySubjects, primary }`
- `primarySubjects`: ordered array of canonical identities
- `primary`: array of semantic surface descriptors
**generate()**: Accepts `{ quads, context, primarySubject }` — places primary subject first for round-trip safety

## 7. Elevated Statements

Auto-detects `rdf:Statement` patterns (type + subject + predicate + object) to create golden graph SPO quads.

**API**: `result.statements` array contains elevated quads.

## 8. Types

```
.Class        # S rdf:type schema:Class
.Person .Astronaut  # Multiple types
```

## 9. Predicates

| Syntax | Direction | Use |
|--------|-----------|-----|
| `p`     | S → L     | Literal properties |
| `?p`    | S → O     | Object properties |
| `!p`    | O → S     | Reverse properties |

Create quad only if all nodes exist.

### 9.1 Remove Polarity

Prefix `-` for remove tokens: `-p`, `-?p`, `-!p`, `-.C`

Route against live buffer: cancel if present, else add to remove array.

Result: `{ quads, remove, statements, origin, context }`

Invariant: `quads ∩ remove = ∅`

## 10. Literals

From value carrier only.

```
^^datatypeIRI  # Typed data
@lang          # Language tag
```

Common: `xsd:string`, `xsd:integer`, `xsd:gYear`, `xsd:date`.

## 10. Objects

From: `[text](url)`, `![alt](url)`, `{=IRI}`

**Example**:
```md
## References {=ex:references}
[Image](https://example.com/img.jpg) {?image label}
[W3C](https://www.w3.org/RDF) {?dct:references label}
[Alice] {=tag:alice@example.com,2026:me ? label}
```

## 11. Lists

Lists are pure Markdown structure with **no semantic scope**. Each list item requires explicit annotations.

```md
[prj] <tag:project@example.com,2026-02-03:>
[foaf] <http://xmlns.com/foaf/0.1/>

## Local Projects Team {=prj:team .foaf:Group label}

Team members:

- **Alice Cooper** {+prj:alice ?foaf:member .foaf:Person foaf:name}
- **Bruce Miles** {+prj:bruce ?foaf:member .foaf:Person foaf:name}
- **Eva Clark** {+prj:eva ?foaf:member .foaf:Person foaf:name}
```

**Key Rules:**
- No semantic propagation from list scope
- Each item must have explicit annotations
- Use `+IRI` to maintain subject chaining for repeated object properties
- Nested lists have no inheritance

**Ordered lists** require explicit rdf:List construction:
```md
[ex] <http://example.org/>

# Manual list construction {=ex:manualList label}
[head] {=ex:l1 ?sh:in .rdf:List}
[a] {+ex:A ?rdf:first}
[list2] {=ex:l2 ?rdf:rest}
[b] {+ex:B ?rdf:first}
[nil] {+rdf:nil ?rdf:rest}
{=}
```

## 12. Reverse Relations

`!p` flips direction only.

```md
Used in: {!schema:hasPart}
- Bread {=ex:bread}
# Creates: ex:bread schema:hasPart S
```

## 13. Code Blocks

`{...}` on opening fence:

```md
```js {=ex:code .SoftwareSourceCode schema:text }
console.log("hi")
```
```

## 14. Context

### Default Context

```json
{
    "@vocab": "http://www.w3.org/2000/01/rdf-schema#",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "sh": "http://www.w3.org/ns/shacl#",
    "prov": "http://www.w3.org/ns/prov#"
}
```

### Adding Prefixes

```md
[alice] <tag:alice@example.com,2026:>
[wd] <https://www.wikidata.org/entity/>
```

### Prefix Folding: Hierarchical IRI Authoring

Build namespace hierarchies by referencing previously declared prefixes:

```md
[my] <tag:mymail@domain.com,2026:>
[j] <my:journal:>
[p] <my:property:>
[org] <https://org.example.com/>
[person] <org:person/>
[emp] <person:employee/>
```

**Resolution Rules:**
- Prefixes must be declared before they can be referenced (forward-reference only)
- Circular references are treated as literal strings
- Later declarations override earlier ones

### Setting Vocabulary

```md
[@vocab] <http://schema.org/>
```

**Rules:** Apply forward, later overrides earlier, single-pass resolution.

## 15. Forbidden

Implicit labels, structural inference, auto-subjects, literal lists, blank nodes, key=value, predicate guessing, backtracking, CURIE in links, **semantic list propagation**.

## 16. Conformance

Must: Follow routing rules, emit only from `{...}`, single-pass, deterministic, traceable, support all predicate forms (`p`, `?p`, `!p`).
