Skip to content
Open
142 changes: 124 additions & 18 deletions server/src/processing/cache-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ An `<xref ref="id">optional<br>content</xref>` is a cross-reference to a
`Target` or a gather element whose id is `ref`. The cache hydrates it to a link
to that target; the link's content is the optional content if provided, or the
target's inner HTML otherwise: `<xref ref="id" contenteditable="false"
data-content="optional&amp;lt;br&amp;gt;content"><a
href="../path/to/page#id">optional<br>content</a></xref>`. This hydrated form is
id="generated-anchor" data-content="optional&amp;lt;br&amp;gt;content"><a
href="../path/to/page#id">optional<br>content</a></xref>`. The generated `id`
is the anchor a backlink returns to (see below). This hydrated form is
only present in the Client. Since the value of `data-content` is itself
attribute-encoded (see below), its serialized form is encoded twice.

Expand All @@ -59,7 +60,8 @@ Details:
* This element allows no attribute except for `ref`; all other attributes are
silently stripped during hydration. In particular, an `<xref>` is never a
`Target`, so an `id` attribute is stripped. While hydration temporarily adds
the `data-content` and `contenteditable` attributes, dehydration removes them.
the `data-content` and `contenteditable` attributes and the backlink anchor
(see below), dehydration removes them.

* The `<xref>` is marked `contenteditable="false"`; the optional content cannot
be edited in the Client.
Expand Down Expand Up @@ -116,8 +118,9 @@ Details:
Including cache hydration would cause the inner HTML to be updated each time
the file is processed, outdating the other file. Math hydration depends on
nothing in the cache, so the inner HTML is hydrated like optional content each
time it's rendered, both here and in fragment backlinks (see
<xref ref="cc-gKXTXIZnFm"></xref>).
time it's rendered: here, in fragment backlinks (see
<xref ref="cc-gKXTXIZnFm"></xref>), and in the lead link of a displaced
backlink container.

* If the `ref` attribute is missing, the `id` referred to isn't found, refers to
a duplicate id or a fragment, or is an invalid CSS identifier, the content is
Expand All @@ -128,6 +131,89 @@ Details:
would create a circular dependency (see <xref ref="cc-P-g5o100jN"></xref>);
with it, there is none, but a single rule is simpler.

During hydration, each target displays one backlink for each authored `<xref>`
that resolves to it. Following a backlink returns to that occurrence in its
source file. For example, if a paragraph in `guide.md` contains two
`<xref ref="setup"></xref>` elements, the `setup` target displays two
backlinks, one to each position in the paragraph.

Generated backlinks and the anchors they return to appear only in the hydrated
HTML, and are excluded from cached target content, `Fragment::content` and
saved source; a target inside a fragment therefore carries no backlink
container into the gather lists which render that fragment. Backlinks render in
a separate container outside the target, `<div class="cc-backlinks"
contenteditable="false">`, since a target may be void or empty. Dehydration
removes only backlink containers generated by hydration and preserves authored
elements with identical markup and their contents.
The container is placed after the target, and after any container the target's
own hydration adds (a gather element's `cc-gather-items`). Where a `<div>`
can't follow the target -- an inline target in a paragraph, or a target which
is a list item, table row or table cell -- it is placed after the target's
nearest ancestor whose parent can accept a `<div>`, since the parent's content
model forbids one there: the Client's re-parse closes a `<p>` and splits it,
fosters a `<div>` out of a table, and a `<div>` within a `<ul>` is simply
invalid. Such a container, which doesn't immediately follow its target, begins
with a link to it, labeled with the target's inner HTML, or with its id where
that is empty.

Backlink details:

* Only an authored `<xref>` which resolves to the target counts. A reference
which renders an error message instead of a link produces no backlink.
Neither does an `<xref>` nested in another `<xref>`'s optional content, which
<xref ref="cc-2dkkF9YJnL">cleaning</xref> unwraps; the authored `<xref>` in
a target's own inner HTML does count.
* Backlinks are ordered by the referring file's path, then by source order
within that file, since `IdEntry::dependents` is a `HashSet` whose iteration
order isn't deterministic; `gathers_referencing` sorts for the same reason.
* A backlink's text is the path of the file containing the reference, as in a
gather-list item link. (A fragment's backlink uses the gather element's inner
HTML as its text. An `<xref>`'s optional content is usually absent, and when
absent its content is the target's own inner HTML, which would make useless
link text.) References to the same target from one file are numbered in
source order (`guide.md (1)`, `guide.md (2)`) so that they can be told apart;
a file with a single reference to it is unnumbered.
* When a gather element re-hydrates an `<xref>` from a fragment's content, that
copy is not an occurrence and carries no anchor; the target keeps one
backlink, to the authored reference in its own file.
* To give each backlink a return destination, hydration gives every authored
`<xref>` which resolves a generated `id`, as in the hydrated form above, so
that a backlink is an ordinary link to `path/to/file#anchor`. The Client
brings the referring block into view even if it is not currently rendered,
then navigates to the exact occurrence. The generated anchor is absent from
the cache's `Target`s because targets are collected before hydration.
Dehydration must remove the anchor along with
`data-content`; hydration already strips every attribute but `ref`, so a
saved anchor would otherwise be dropped silently on the next load.
The anchor is derived from the source: the referenced id and the occurrence's
ordinal among this file's references to that id (for example
`cc-xref-setup-2`). Deriving it from the occurrence's order among *all* of
the file's references would shift every anchor below an inserted `<xref>`,
and the record diff below would then outdate the file defining nearly every
id this file references. The anchor must survive TinyMCE's sanitizer and must
not collide with an id in the document, as autogenerated ids must not; a
collision is resolved deterministically from the source, so that the record
and the rendered `href` agree.
* `IdEntry::dependents` records only which files refer to an id, which can't
show what a changed file referred to before. The cache therefore extends its
per-file list of cross-references (`FileEntry::xrefs`, already in source
order) with each occurrence's anchor and whether it resolved; backlinks read
the resolved ones, while the unlink round trip still needs every referenced
id, including those whose `IdEntry` is `Missing`. This record renders the
backlinks without re-reading every referring file. `commit_file` diffs it,
old against new, and outdates every file defining each id whose occurrences
changed -- all of them where the id is duplicated, none where nothing defines
it, since whenever it does appear the file defining it renders its own
backlinks in that same pass. Adding or removing a second reference to the
same id counts. An id defined in the file being committed needs no outdating,
since a commit never outdates its own file: the translation which commits the
record renders that file's backlinks from it. A moved referring file is
covered by the diff, since both ends commit; removing a file from the cache
applies the same diff with an empty new record.
* Backlink hydration finds the files referring to a target through
`IdEntry::dependents` and reads their records; see layer 6 of
<xref ref="cc-P-g5o100jN"></xref>.

<h3
data-gather="cc-49851a1jdJ cc-BHC6fJQ8bU cc-MwcxCRjY8b cc-FqIILGgnfo"
id="cc-oRzUk5hMXK"
Expand Down Expand Up @@ -268,7 +354,8 @@ lower-numbered layers:

1. `Target::inner_html` (including a gather element's) and `Target::gather_ids`
are pure functions of the source (pre-hydration) -- they depend on nothing.
2. `<xref>` hydration depends on layer 1 and its optional content only.
2. `<xref>` hydration depends on layer 1, its optional content, and the local
reference order and document ids used to derive return anchors.
3. `Fragment::content` depends on the source plus layer 2.
4. `<fragment>` backlink hydration depends on layer 1 only: it renders, for each
gather element which lists this fragment, that element's containing file,
Expand All @@ -281,10 +368,25 @@ lower-numbered layers:
everything about a target which this reads is embedded in the fragment's
content, so any change to it also changes that content, which outdates the
gathering file.

Only layers 1 and 3 are cached, and layer 3 depends only on layer 1 through
6. `<xref>` backlink hydration depends on the per-file record of resolved
occurrences (an output of layer 2) and, for the label of a displaced
container's lead link, on layer 1, which needs no dependency of its own
since that target is in the same file. It renders last, after fragment
contents are stored and gather lists are inserted: that ordering is what
keeps a backlink container out of `Fragment::content`, since the cleaner
removes content by tag and can't drop a `<div>` by its class. Its own output
is cached nowhere (see <xref ref="cc-HZyE2Bv-eI"></xref>).

Only layers 1 and 3 are cached, along with the per-file record of resolved
`<xref>` occurrences which renders backlinks (see
<xref ref="cc-HZyE2Bv-eI"></xref>), and layer 3 depends only on layer 1 through
layer 2. So a change travels at most two steps (C → B → A), which matches the
indirection example, ensuring that propagation terminates.
indirection example, ensuring that propagation terminates. A change to a file's
record of resolved occurrences outdates the file defining each affected id, and
that edge ends after one step, since a record change alters nothing the
reprocessed file's own cached state is built from -- its source, the targets
and fragments it reads, and its own occurrence record are all untouched -- so
neither layer 1 or 3 nor that record changes.

Supporting cross-references, fragments, and gather elements requires the cache
to track dependents of an id, so that files containing these dependencies can be
Expand Down Expand Up @@ -331,18 +433,22 @@ outdated file, mark its status as `Outdated` (see

### Gather elements: the reverse edge

`dependents` alone is not enough for gather elements, because the reference runs
in both directions: a gather element renders the *content* of each fragment it
lists, and each of those fragments renders a *backlink* to the gather element
(see layer 4 above). The set of gather elements referencing a fragment is
therefore not merely bookkeeping -- it is observable output in the fragment's
own file.
`dependents` alone is not enough for gather elements, as it is not enough for
cross-references now that a target renders backlinks (see
<xref ref="cc-HZyE2Bv-eI"></xref>); what is diffed differs --
`Target::gather_ids` here, the per-file record of resolved occurrences there.
The reference runs in both directions: a gather element renders the *content*
of each fragment it lists, and each of those fragments renders a *backlink* to
the gather element (see layer 4 above). The set of gather elements referencing
a fragment is therefore not merely bookkeeping -- it is observable output in
the fragment's own file.

This is why changes to the outgoing references of gather elements cannot be
determined by the unlink-all/relink-all round trip used for cross-references
(see `commit_file`): that round trip destroys the very information a diff would
need. `commit_file` instead diffs `Target::gather_ids` explicitly, and applies
this rule:
(see `commit_file`, whose documented premise -- that a target's file renders
nothing depending on who references it -- the occurrence record replaces): that
round trip destroys the very information a diff would need. `commit_file`
instead diffs `Target::gather_ids` explicitly, and applies this rule:

> When a gather element `G` is committed, outdate the file defining each id in
> the symmetric difference of `G`'s old and new `gather_ids`; if `G`'s inner
Expand Down
Loading