The CodeChat Editor, a word processor for software developers.
Copyright (C) 2024-2026 Bryan A. Jones. This file is part of the CodeChat Editor. The CodeChat Editor is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The CodeChat Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the CodeChat Editor. If not, see [http://www.gnu.org/licenses](http://www.gnu.org/licenses). Cache spec ========== The cache stores the location (file name and ID) and content (inner HTML or code/doc blocks for fragments) of targets and fragments. Targets are HTML elements (excluding ``s and ``s) with an ID. The goal of the cache is to support cross-references and gather elements, and to assist in ensuring that all IDs are unique within a project. This means that cross-references and gather elements persist across moving or renaming files, since the IDs will be found in the cache. Non-project files support a subset of this functionality: the "project" consists only of the current file. Only targets, gather elements, cross-references, and fragments to items within the file work as expected; references to other files do not. The cache reflects data read directly from disk/IDE; content edited in the Client does not update the cache until it's written to disk/IDE, at which point the cache must re-process the file. Since the Client is designed around an autosave principle which updates disk/IDE regularly, there's little gap between the two. The cache hydrates data to the Client; the dehydration routes are responsible for removing all cache hydration artifacts.

Cross-references

An `optional
content
` 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: `optional
content
`. 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. Details: * This element allows no attribute except for `ref` and `id`; all other attributes are silently stripped during hydration. An `id` makes the `` a bidirectional cross-reference (see ), not a `Target`; both hydration and dehydration keep it, since the backlink links to it. While hydration temporarily adds the `data-content` and `contenteditable` attributes, dehydration removes them. * The `` is marked `contenteditable="false"`; the optional content cannot be edited in the Client. * The optional content is the [permitted content for an ``](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/a#technical_summary); this content is cleaned during hydration to ensure compliance, except as noted below for block-level content in a paragraph. Cleaning strips `id` and `data-gather` attributes, removes `` elements, and unwraps nested ``, so the optional content never defines a target, gather element, cross-reference, auto-assigned `id`, or fragment. The cleaned content is then hydrated to support math. * If the cleaned content, when serialized as HTML, contains at least one non-[whitespace](https://developer.mozilla.org/en-US/docs/Glossary/Whitespace#in_html) character, this content becomes the generated link content. This allows visually empty content (for example, an empty ``), but avoids defining which HTML nodes count as non-empty. However, TinyMCE removes empty content (such as an empty ``), which causes constant re-translation messages, an unfortunate cost for this design simplicity. * Otherwise, the inner HTML from the target or gather element is used instead; for gather elements, the gathered code/doc blocks are not used. * Note that cleaning can't correct block-level content in a paragraph, since the HTML parser acts first. For example, `See
text
more.` parses as an `` with no optional content, followed by a `
` and the text `more.`. * The optional content as parsed, but before hydration and cleaning, is always stored (even when an error is produced) in the `data-content` attribute of the ``, then restored during dehydration. Dehydration doesn't descend into the restored content, which was not hydrated when saved. * Any previous `data-content` value is overwritten during hydration, then removed during dehydration. * If `data-content` is missing during dehydration, the `` optional content is empty. * The value for `data-content` is attribute-encoded to avoid removal by TinyMCE's sanitizer. * Leading and trailing whitespace may be trimmed: when TinyMCE loads the content, its XSS attribute sanitizer calls DOMPurify, which trims. The Server preserves this whitespace. * When no optional content is provided, the link's content is the target's inner HTML, which is always taken before cache hydration, to prevent circular dependencies (see also ): ```html

See

See

``` 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 ). * 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 instead an appropriate error message, replacing the displayed optional content or the target's inner HTML. * Cross-references to fragments are not allowed. Without optional content this would create a circular dependency (see ); with it, there is none, but a single rule is simpler.

Bidirectional cross-references

An `` which carries an `id`, such as ``, is a *bidirectional cross-reference*: in addition to the link from the `` to its target, the target displays a *backlink*, a link from the target back to the ``. An `` without an `id` is a one-way cross-reference and behaves exactly as described above. This supports a hierarchy of specifications (concept, requirements, detailed design), where a reader follows an `` down one level and a backlink back up. The design mirrors the fragment and gather element pair (see ), which is already bidirectional: the bidirectional cross-reference plays the role of the gather element, since its reference is written in the source, while its target plays the role of the fragment, since it renders backlinks computed from the references which name it. The core rules are: * **Identity.** The ``'s `id` belongs to the project's single id namespace, so it must be a valid CSS identifier and unique in the project (see , "Missing and duplicate ids"); it is also the anchor the backlink links to. It counts as a definition of its id exactly as a target or fragment does, so an `IdEntry` whose id is defined by both a bidirectional cross-reference and a target, fragment, or second bidirectional cross-reference is `IdState::Multiple`. A bidirectional cross-reference is neither a `Target` nor a `Fragment`, so neither another `` nor a `data-gather` list may refer to it; such a reference renders an error message. Its displayed content is itself the output of cross-reference hydration, so allowing this would make the hydration of one `` depend on that of another. * **Backlink data.** For each bidirectional cross-reference, the cache records its containing file, `id`, `ref`, and *backlink text*, the content of the backlink's link. Like `Target::inner_html`, each of these is computed from the source of the ``'s file before hydration, so it depends on nothing else in the cache; in particular, the backlink text never includes the target's inner HTML. TODO (Pavel): define how the backlink text is computed; replace this note with a cross-reference to that definition once it exists. * **Rendering.** Hydration adds to each target one backlink per bidirectional cross-reference which names it, ordered by containing file and then by `id`, as fragment backlinks are. This markup is marked `contenteditable="false"`, exists only in the Client, and is removed by dehydration, like a gather list. TODO (Pavel): specify where the markup goes for an inline target, a block target, and a gather element, and which elements carry the `cc-backlinks` class; replace this note with a cross-reference to that subsection once it exists. * **Coherence.** Backlinks are excluded from `Target::inner_html`, `Fragment::content`, and the optional content of any ``, just as fragment backlinks are excluded from `Fragment::content`. Were they included, a new backlink would change the target's inner HTML, outdating every file which references the target, including the file of the `` which produced the backlink. Since nothing reads a target's backlinks, rebuilding the target's file to update them outdates nothing further, so propagation still terminates. * **Reverse edge.** As with gather elements, the set of bidirectional cross-references which name a target is observable output in the target's file, so the unlink-all/relink-all round trip used for one-way cross-references cannot detect a change to it (see "Gather elements: the reverse edge" under ). Instead, committing a file diffs its old and new bidirectional cross-references, matched by `id`, and outdates the file of each target whose backlinks were added, removed, or changed; when a bidirectional cross-reference's `ref` changes, this outdates the files of both the old and the new target. One-way cross-references keep the round trip. A bidirectional cross-reference reports the same errors in its `ref` as a one-way cross-reference (see the Details above). Its `id` is checked independently of its `ref`, and the backlink exists only when both are valid: * **Invalid `id`.** An `id` which is not a valid CSS identifier, including the empty `id=""`, renders an error message in place of the ``'s content, as for any invalid id (see ). It defines no id and produces no backlink. * **Duplicate `id`.** A valid `id` always counts as a definition, even when the `ref` has an error, so that uniqueness doesn't depend on the `ref`. When its `IdState` is `Multiple`, the `` renders an error message naming the duplicate and produces no backlink. Every bidirectional cross-reference which produces a backlink therefore has a unique `id`, so matching old and new ones by `id` when committing a file is unambiguous. * **`ref` errors.** When the `ref` is missing, invalid, not found, duplicated, or names a fragment or a bidirectional cross-reference (including the `` itself, as in ``), the `` renders the one-way error message and no backlink appears anywhere. The `id` still defines its anchor, and the backlink appears once the `ref` resolves to a `Target` or gather element. * When both the `id` and the `ref` have errors, the message reports both. * **Nested ``.** An `` with an `id` inside the optional content of another `` keeps its `id` in the source, since `data-content` preserves that content verbatim, but is never hydrated: cleaning unwraps it (see ). It therefore defines no id and produces no backlink. Two sanitizers must preserve both new pieces of markup, since each removes the attributes and classes it doesn't allow: * The Server sanitizes all hydrated HTML before sending it to the Client (`minify` in `processing.rs`). It already allows `id` on every element, but allows classes only per element, so it must allow `cc-backlinks` (see below), plus `contenteditable` on each element the backlink markup uses. * In the Client, the `` entry in `extended_valid_elements` in `tinymce-config.mts` must allow `id` in addition to `contenteditable` and `ref`; otherwise TinyMCE strips the `id`, and the next save turns the bidirectional cross-reference into a one-way one. A bidirectional cross-reference is still an inline element, so `custom_elements` keeps `~xref` unchanged. * The backlink markup uses only standard HTML elements, marked by a class and `contenteditable="false"`, as the gather list does, so TinyMCE's default schema keeps it and no new custom element is needed. Dehydration recognizes the backlink markup by its reserved class, `cc-backlinks`, and removes each element carrying it, together with its contents, wherever it appears, as it removes a gather list; this also discards a copy the user pasted elsewhere. Matching by class rather than by position keeps dehydration independent of where the markup is placed. The `` itself dehydrates exactly as a one-way cross-reference, except that its `id` is kept: `contenteditable` and `data-content` are removed and the optional content is restored from `data-content`.

Implementation of Cross-references

Fragments and gather elements

A gather element such as `

Bazzy things

` is a `Target` with the `data-gather` attribute. It becomes a list of the content of fragments it refers to after hydration by the cache. An example fragment tag, after cache hydration: `See
Bazzy things, Zappy things`. A fragment's content by default includes the content of the current doc block and the content of the following code/doc block; fragments are not allowed in Markdown documents (in this case, the fragment content consists of an error message). Fragments may include the `following` attribute to enclose a specific number of the following code/doc blocks; for example, `` includes the current doc block along with the next 3 code/doc blocks; `following` must be a whole number. Details: * Fragment content may not include a gather element; in this case, the list in each gather element which includes this fragment shows an error message instead. * Fragments do support indirection: gather element A includes content from fragment B, which contains a cross-reference to target C. Changes to target C make B and A outdated. * If a gather element refers to an `id` that is a `Target`, not a `Fragment`, the resulting output for this in the list of fragments is an error message. * If a gather element refers to an `id` that wasn't found or is a duplicate, its content will be replaced by an error message. * Fragments store an HTML rendering of the code and doc blocks they contain, excluding the content produced by hydrating the `` tags, to avoid duplication and circular dependencies. Cross-references in this rendering are hydrated in the fragment's file, so their links lead from there; a gather element re-hydrates each which hydrated to a link so that it leads from the gather element's file instead. An error message reads the same anywhere, so it's kept as is. See layer 4 under : this exclusion is what keeps a gather element and the fragments it lists from outdating each other forever. The HTML rendering of a fragment reproduces the layout of the source it came from: each doc block includes its indent, each line of a code block or the first line of a doc block is preceded by that line's number, and the two are aligned -- a doc block and a line of code indented equally in the source begin in the same column, with the line numbers in a gutter of their own to the left of both. * The backlinks a `` hydrates to are derived from the gather elements which list it: for each such element, its containing file, its `id`, and its inner HTML (the link text). A fragment's rendered output therefore depends on the gather elements which reference it -- the reverse of the direction in which references are written; see . * No content is allowed in a ``. Any content placed here is moved after the fragment's closing tag. Otherwise, the next save would erase it; this avoids data loss when a user first creates a fragment tag, but hasn't closed it. * A `` is valid; it contains only the current doc block. * The `following` attribute is clamped if it would exceed the number of code/doc blocks in the document. * If the `following` value cannot be parsed to a whole number, an error message replaces the fragment content. * A gather element, as a type of `Target`, requires an `id`. A `data-gather` attribute on an element without an `id` produces an error message which requests the missing `id`. * Likewise, a fragment requires an id, producing an error message when the `id` is missing. * All ids must be valid [CSS identifiers](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/ident) per [MDN recommendations](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/id). Invalid ids produce error messages in the hydrated tag content. Example hydration of the gather tag `

Bazzy things

`: ```html

Bazzy things

(first item content) ... (last item content)
```

Implementation of Fragments and gather elements

Search (future work) -------------------- The cache will support searching the (cleaned) inner HTML of all `Target`s and gather elements; search will not include `Fragment` content. Auto-assignment of ids ---------------------- If `id="*"` on a fragment, target, gather element, or `` (making it a bidirectional cross-reference), the cache replaces this with a random autogenerated `id` placed in the resulting HTML content, but this new `id` is not yet recorded in the cache. It enters the cache only when source containing it is next translated: the Server translates each edit the Client saves back to the Client, to check for changes, which commits the edit; otherwise, the consumer reads the file once it's written to disk. This helps avoid cases where the cache and file content become unsynchronized: if the `id` is placed in the cache before the write and the write fails, or if the file is never written (it was being scanned, but not actively edited, so the file content wasn't written back). TODO: an `id="*"` typed in the IDE reaches the source only when the Client sends back the HTML containing its replacement, which it does only once the user edits in the Client; until then, each translation of the IDE's text assigns a different id. Resolve this by sending the replacement to the IDE when a translation assigns ids. The autogenerated id must be a valid [CSS identifier](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/ident). Autogenerated ids must be checked to make sure they don't collide with an existing id.

Cache design

The cache is a single plain-data structure, shared as an `Arc>`; all consistency comes from that one lock, so no per-item locking (and therefore no lock ordering) is needed. Items refer to each other by key -- files by path, targets and fragments by id -- rather than by `Arc`/`Weak` pointers. This keeps the structure acyclic, `Send`, and (in the future) serializable, and avoids garbage-collecting stale weak references. Note that the watcher, consumer tx channel, and connections struct cache members aren't serializable, but can be re-created after deserialization. Cached state always converges by design. Each layer depends only on 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. `` hydration depends on layer 1 and its optional content only. 3. `Fragment::content` depends on the source plus layer 2. 4. `` backlink hydration depends on layer 1 only: it renders, for each gather element which lists this fragment, that element's containing file, `id`, and inner HTML. Critically, this output is *excluded* from `Fragment::content` (layer 3); were it included, a gather element and each fragment it lists would outdate one another forever. 5. Gather-list hydration depends on layers 1 and 3. It also re-hydrates each `` within a fragment's content (layer 2) which hydrated to a link, so that it links from the gathering file. That needs no dependency of its own: 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 layer 2. So a change travels at most two steps (C → B → A), which matches the indirection example, ensuring that propagation terminates. Supporting cross-references, fragments, and gather elements requires the cache to track dependents of an id, so that files containing these dependencies can be marked `Outdated` (see below); these are sent to the `Cache::consumer_tx` channel for processing. Dependencies are tracked at file granularity, on the id rather than on the item defining it: `IdEntry::dependents` is the set of files whose rendered output depends on this id, by any means -- an `` or a `data-gather` list naming it. One uniform set suffices in all three `IdState`s because the typical action taken on a dependent is marking this file outdated (fragments also use their dependents to generate "See x" links). Updating dependencies requires a definition of what constitutes a difference which would trigger outdating dependencies. The state which is checked for differences is: * Target/gather element: type (a target/gather element), path of the containing file, id, IdState, and inner HTML. (A cross-reference with optional content doesn't depend on the target's inner HTML; it is nonetheless outdated when that HTML changes, for simplicity.) * Fragment: type (a fragment), path of the containing file, id, IdState, and content. A gather element's `gather_ids` are absent from this list because no dependent reads them: a cross-reference to a gather element renders only its inner HTML. The fragments a gather element lists do depend on them, through the reverse edge described below. This also makes indirection (gather A includes fragment B, whose content cross-references target C) work without extra machinery: a change to C outdates B's file; reprocessing B's file changes B's content, which outdates A's file. Updating the cache is a multi-phase process; see . One output from this process (see ) is a list of outdated files. For each outdated file, mark its status as `Outdated` (see ) then: 1. If the outdated file is the project TOC, then send `TocOutdated` to all connections. 2. If the outdated file is the current file for one or more connections, then send `CurrentFileOutdated` to those connections. 3. If neither 1 or 2 is true, the `Rescan(file)` message is sent to itself via the consumer TX channel for later processing. ### 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. 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: > 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 > HTML also changed, outdate the file defining each id in the union instead, > since the backlink text those fragments render comes from it. A gather element moving to a different file needs no special case: the old file's commit sees the element deleted (new `gather_ids` empty) and the new file's commit sees it added, so both ends of the move outdate the fragments. Propagation terminates here for the reason given under layer 4: rebuilding a fragment's file updates its backlinks, but backlinks are excluded from `Fragment::content`, so nothing further is outdated. ### Missing and duplicate ids An id referenced before (or without) being defined has an `IdEntry` whose state is `IdState::Missing`; `dependents` holds the files waiting on it. When the id later appears, the state transition marks those files outdated, and they simply remain dependents; when a defined id disappears, the state returns to `Missing` and the dependents are again waiters. An `IdEntry` which is `Missing` with no dependents carries no information and is removed. Duplicate ids are never renamed (file timestamps can't reliably identify the original, and renaming would silently modify user content). Instead, duplicates are reported as errors to the user. ### Misc All file names (stored as `PathBuf`) must be canonicalized absolute paths.

HTML cleaning

HTML stored as cross-reference optional content, `Target` inner HTML, or as `Fragment` content requires cleaning: * To avoid duplicate IDs, all `id` attributes are stripped. * Note that images, URLs, etc. may not work if the referring path in their new location isn't valid; these simply aren't supported. * Cross-reference optional content and `Target` inner HTML must allow only the [permitted content for an `` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/a#technical_summary). In particular, it strips ``s and unwraps ``s, replacing them with cleaned optional content. * `Fragment` content must preserve the `data-content` attribute of each ``, since a gather element re-hydrates the `` from it so that its link leads from the gather element's file.
Implementation of HTML cleaning
Cache population and maintenance -------------------------------- The cache must contain information about all renderable files in the project. The following design employs a file watcher to update the cache when files change, and a file walker to populate the cache initially and responds to directory changes. The `AppState` structure keeps an LRU map of project path -> cache (note that the cache includes a watcher and walker). Adding/removing connections resizes the LRU size to (connections \* `LRU_ENTRIES_PER_CONNECTION`) + 1; however, the cache for all current connections may not be dropped during an LRU eviction. This LRU-based approach seeks to balance cache memory consumption with the time required to load the cache for an uncached project. The cache fully supports multiple connections to the same project. It does not walk symlinks, since these could lead to two cache entries for the same file. There's no explicit support for nested projects. The cache initially registers a watcher with the provided project path and a consumer thread which receives results from the walker, along with a message channel connecting them. ### Watcher/walker The watcher/walker thread monitors the root directory of the project and all children for changes using the [notify-debouncer-full](https://docs.rs/notify-debouncer-full/latest/notify_debouncer_full/) crate. At startup: 1. It registers the watcher, to ensure that any changes during the walk are still reported. * The watcher sends file names of files that were created, modified, or deleted and the path of created or deleted directories to the walker encoded as `WatcherPathState`s via the walker channel; renames are treated as a delete of the `from` path followed by a create of the `to` path. * If the watcher encounters lost events (`EventKind::Other` with `Flag::Rescan`), it requests a re-walk of the entire project. 2. Even if the watcher fails, it then runs a filesystem walker loop rooted at the project path, initially sending it the root directory to walk and providing it the consumer channel and a second walker channel. A closed channel signals the watcher/walker thread to exit. Note that this approach means extraneous watcher events from ignored directories, such as `.git`, and additional use of OS resources such as inotify watches. #### Walker The walker is based on the [ignore](https://docs.rs/ignore/latest/ignore/) crate, allowing users to exclude files from the project. After the initial traverse of the project, it keeps an [IncrementalIgnore](https://docs.rs/ignore/latest/ignore/struct.IncrementalIgnore.html) for checking files in the `WatcherPathState`s received from the watcher. * If the path is a deleted file or directory and is not ignored by the walker, it's sent directly to the consumer thread. * If the path refers to any of the walker's ignore files (per the [ignore docs](https://docs.rs/ignore/latest/ignore/struct.WalkBuilder.html), this is `.gitignore`, `.git/info/exclude`, and `.ignore`): * The walker and incremental ignores are reloaded. * For `.gitignore` and `.ignore`, the path containing this file is re-walked; for `.git/info/exclude`, the entire tree is re-walked. For simplicity, this occurs even outside a Git repository, or in subrepos, etc. TODO: also have the watcher monitor global ignore files. * If the path is a file and is not ignored by the walker, it's sent to the consumer thread. * If the path is a directory, it's walked; any files found are sent to the consumer thread. Before performing a walk, the walker sends a `WalkStarted` to the consumer thread via the `Cache::consumer_tx` channel; when the walk is completed, it sends a `WalkFinished`. After processing a path, the walker then loops to wait for the next path. The thread ends when either channel is closed (indicating the cache was dropped). It ignores any errors produced during the walk, assuming that files it can't walk aren't usable by the IDE. ### Consumer A consumer reads paths from the receiver paired to the `Cache::consumer_tx` channel. It holds a weak reference to the cache, and exits when this channel is closed or the weak reference can't be upgraded. It is implemented as a thread. The consumer decides whether a file changed by its stamp, as Git's index does: a snapshot of the file's length and modification time (plus its change time and inode on Unix), taken before its content was read. A stamp taken within a few seconds of the file's last write never matches, since a later write in the same timestamp tick could leave this metadata unchanged (Git's "racy clean" problem). * If the file exists and doesn't match its cached stamp or if the file's status is `Outdated`: * If the file is the TOC and doesn't match its cached stamp, send `TocChanged` to all connections once the processing below is done, even if it ignores the file: each connection's sidebar renders the TOC from the text it last loaded, which it must therefore re-load (see `Connections` below). * If the file is the current file for any of the cache's connections, ignore it. This is handled by the standard IDE to Client translation mechanism. * If not translatable, the file is still added to the cache, to prevent reprocessing if the directory is re-walked. * If translatable, it translates the file to the CodeChatForWeb format in order to update the cache for that file. If the `SaveOnScan` option is enabled, it then translates the file back to source code and writes the result if there are any changes, updating the file's stamp in the cache after the write. This costs additional CPU time and produces watcher events and may rewrite files open in the IDE, motivating the choice of a user-selected option. * If the path doesn't exist, search the cache for it; if found, remove it. Otherwise, assume the path was a directory. Search the cache for all files in this directory, removing them. * A `Rescan(file)` asks the consumer to bring a file the cache already has up to date: one a commit outdated, or a connection's current file once no connection shows it, since the consumer ignores current files. Unlike a path from the walker, a `Rescan` hasn't passed the walker's ignore rules. It's therefore skipped if the cache has since forgotten the file (a walk may have found it newly ignored, for example), and it doesn't keep a walk in progress from forgetting the file. * If this is a `WalkStarted`, put all files in the cache which this directory contains in a set. Remove files produced by the walk from this set. After `WalkFinished`, remove any files remaining in the set from the cache, since these no longer exist. Neither kind of removal applies to a file a connection shows -- its current file, or the TOC -- since the connection's IDE may still hold the file's text. Instead, each connection whose current file it is re-translates it, and, if it's the TOC, every connection re-loads it. The cache doesn't record images or multimedia files referenced by a source file as dependencies, requiring the user to manually refresh the page when these files change. ### Connections Every connection in a project also shows the project's TOC in its Client's sidebar, so whenever a commit marks the TOC outdated, the cache sends `TocOutdated` to every connection in the project. The connection re-renders the TOC from the text its sidebar last loaded -- from the IDE if the IDE has the TOC open, otherwise from disk -- then, if the rendering changed, sends the new HTML to the Client in an `UpdateToc` message. The Client replaces the contents of its sidebar in place, which preserves the sidebar's scroll position along with the listener its page attached to intercept clicks on the TOC's links; reloading the sidebar would discard both. Committing the TOC itself doesn't outdate the TOC, since a commit never outdates its own file. This matters because each sidebar load translates, and therefore commits, the TOC: were that commit to outdate the TOC, each refresh would trigger another. Instead, edits to the TOC reach the sidebar in other ways: * When a connection's current file is the TOC, the connection refreshes its sidebar after each edit to the TOC, whether made in the IDE or the Client. * When the TOC changes on disk, the consumer sends `TocChanged` to every connection in the project. Since the TOC's text then differs from the text the sidebar last loaded, this prompts the connection to fetch the TOC's new text from its IDE (with a `LoadFile`), falling back to disk, before re-rendering it. ### Shutdown Overall shutdown sequence: 1. The LRU map drops a `Cache` and the upgraded Weak reference by the consumer thread is dropped. 2. The `Cache` drops the `_watcher` and the `consumer_tx` channel. 3. When dropped, the `_watcher` closes its channel to the walker, stops the watcher, drops its `consumer_tx` channel, and exits. 4. The consumer thread exits when either its consumer rx channel closes or the weak pointer fails to upgrade.

Links in hydrated content

Some content, such as math, cross-references, fragments, and gather lists (see and ), sits inside an element marked `contenteditable=false`. TinyMCE's link plugin offers no context menu items for non-editable content, so a right click (mouse) or long press (touch) on one of these links is ignored. Therefore, when the user performs either of these actions, the Client must show the TinyMCE context menu, whose only item, "Open link", follows the link as a Ctrl+click does. A right click or long press on non-editable content beside its links offers no "Open link". With a non-editable element selected, the keyboard's context menu shortcut (the Menu key, or Shift+F10) shows the same menu, which follows the element's first link, as Alt+Enter does. The Client also follows these links in the other ways it follows any link (see `Link navigation` in [implementation.md](../../../docs/implementation.md)): * Ctrl+click (Cmd+click on macOS) follows the clicked link. * Alt+Enter, with the non-editable element selected, follows its first link. TODO ---- * `SaveOnScan` and search functionality are not implemented yet. This is future work.