[Reference](https://komalang.org/reference/)

Version 0.0.0, in development. The first published version will be 0.1.0.

# Koma document package

A `.koma` file is the readable text source. A `.komadoc` file is a Koma document package that carries the source and its referenced assets. The format may be called KomaDoc in prose. KomaDocs is the application and service. The package is a Koma-specific format based on TextPack, the compressed form of TextBundle v2. It follows the [TextBundle specification](https://textbundle.org/spec/) for `type` and derived formats. General Markdown editing and rendering compatibility is outside the format's contract.

## Structure

A ZIP archive holds one TextBundle directory containing `info.json`, `text.koma`, and `assets/`. A newly created directory is named `Content.textbundle/`. In `info.json`, `version` is the integer 2, `type` identifies Koma source, and Koma-specific metadata lives under its identifier. The prototype identifier `com.github.amachino.koma` carries no guarantee of macOS UTI registration. The outer derived UTI declares `org.textbundle.compressed` as its parent when adopted by the native app. Preserve other applications' metadata on resave.

## Source and dependencies

Collect referenced images, tables, logos, and custom themes with their assets. A pinned theme keeps every file covered by its integrity hash. Following TextBundle, local asset references in the packaged source resolve under `assets/`. Preserve the original `.koma` and assets in place. When a packaged copy requires rewritten references, edit only their source ranges and preserve quoting, comments, and line endings. Report unsafe transformations and missing or unauthorised assets, and withhold the package rather than saving an incomplete copy as complete. Fetching external URLs requires explicit action.

### Embedded themes

- A shared copy, created by Export as .komadoc or `koma pack`, always contains the theme that defines the document's appearance. Include even a built-in theme as a pinned snapshot in `assets/themes/koma-snapshot-<digest>/`, and point `theme:` at the snapshot. Embed the default theme when the source omits `theme:`. Updates to KomaDocs and `koma` leave the package's theme basis intact.
  - Fonts stay outside the package. KomaDocs and `koma` both include the default fonts. Rendering may still vary at the pixel level.
  - The `@font-face` checks reject themes that use custom fonts and explain the reason.
  - Test: Unit tests cover embedding built-in and default themes as snapshots. E2E checks that `koma export` can render a package using its bundled theme alone.

- After KomaDocs saves a package in place, uses Save As to create a package, or overwrites it after a conflict, the app shows the same document as reopening the package. It reads the written package through the reopening path and adopts the stored source and pinned theme. The `theme:` value, theme label, and rendered pages match before and after reopening.
  - Rewriting a reference to the pinned theme is part of saving and adds no Undo or Redo entry. Edits made before the rewrite remain undoable. A successful save clears the unsaved state, and another unchanged save writes the same package.
  - A source edit made during saving takes precedence. Leave the live source intact and embed the same pinned theme on the next save.
  - Copies made for sharing, download, or resolution through Save to Another File leave the open document intact.
  - Test: Unit tests cover source, theme, unsaved state, and history after saving. E2E checks that saving a built-in theme to `.komadoc`, reopening, and saving again preserves the theme label, `theme:`, and page images.

### Editable themes

A package can carry one editable theme in addition to pinned snapshots. The source sets `theme:` to `./assets/themes/<name>/theme.koma`. The name is at most 64 characters, uses lowercase ASCII letters, digits, and hyphens, and begins with a letter or digit. Names beginning with `koma-snapshot` or `koma-legacy` are reserved. Validate an editable theme by matching its referenced asset set exactly to the packaged files. Reading and writing reject missing assets, unused bundled assets, assets outside the theme directory, and path collisions. The font check still applies. Preserve the theme file's BOM and line endings and the raw bytes of unmodified assets.

Edit Theme duplicates an official theme or a pinned snapshot into this format and changes `theme:` as a single undoable edit. Preserve the official and source themes. If the document already has an editable theme, open its source without duplicating it. The source pane shows the theme in place of the document. The hidden document editor retains source, unsaved changes, history, and cursor. Undo acts on the active editor.

A complete theme edit with available assets and no custom fonts updates the theme in the package draft and redraws the document. Unused assets are omitted from the save set and return if used again. Explain incomplete edits and keep the last applied theme until the edit is repaired or undone. Returning to the document requires a complete theme. Saving writes the last applied theme and uses the existing explicit save path, including comparing the baseline bytes, offering conflict choices, and retaining the draft until success. Opening another document, or choosing Reload in a conflict, ends theme editing. Unapplied text is discarded on reload. The document returns with the last applied theme.

### Package cover

- A package may carry a cover: a picture of its first page at `Content.textbundle/preview.png`, following the TextBundle and Keynote convention of a preview beside the content. The cover is a PNG at most 640 pixels wide, 1280 pixels tall and 256 KiB. It is never content: no reference names it, opening, rendering and exporting never need it, and a package without one opens exactly as before.
  - On opening, the cover entry passes the same archive checks as every other entry: path traversal, links, duplicate names, case and Unicode collisions, and corrupt archive structure still refuse the whole package. A cover that is not a PNG with an `IHDR` header first and `IEND` last, is larger than its bounds, or fails to unpack is ignored: the package opens without it and nothing shows it. An oversized entry is never unpacked.
  - Saving in place, Save As, the conflict choices and the package download write a fresh cover inside the same package write, drawn from the first page by the app's page renderer and reduced to fit the bounds. Export as .komadoc does the same for the sharing copy. The cover is drawn before the save takes its snapshot. When drawing fails, takes longer than three seconds or gives an unusable picture, the save writes the package's previous valid cover, or none, and continues. Drawing the cover adds no Undo entry and changes neither the source nor the assets.
  - `koma pack` keeps a valid cover of a package it repacks and adds none to a `.koma` source, so packing needs no browser. `koma pack --cover` draws the first page with the installed browser `koma export` uses and writes it when it is usable; without a browser, or when drawing fails, it writes the package without a new cover and says so on stderr. Source edits by `koma format --write` and `koma check --write` keep the package's entries, including its cover.
  - In the macOS app, opening or saving a package gives its cover to the recent document snapshot, and the chooser shows it in place of the source preview. The snapshot keeps a cover only as base64 of a PNG within 256 KiB, so listing still reads only the app's own folder.
  - Test: Unit tests cover the bounds check, a save and reopen round trip with the cover, a package without one opening identically, oversized, non-PNG, corrupt, traversing and duplicate cover entries, the source and Undo history after a save, a failed or late drawing keeping the previous cover, `koma pack` keeping a cover and drawing one with `--cover`, and the recent snapshot receiving it.

## Opening, editing, and saving

Inspect the ZIP contents, identifier, and version before opening. The TextBundle directory holds only `info.json`, `text.koma`, `assets/` and the optional cover. Reject path traversal, links, duplicate names, case and Unicode collisions, corrupt entries, and excessive unpacked data. Keep the package in memory without extracting it to disk. Keep the source, assets, and unknown metadata together. Saving generates a complete replacement package. Use normal conflict protection when the destination changes externally. On failure, retain the original package and recovery draft. The retired `.koma.zip` format is outside this contract.

## Implementation

The CLI can create a portable package with `koma pack`. KomaDocs reads and writes document packages. The package and save tests cover preserving assets and metadata, detecting external changes, and retaining recovery drafts.
