Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ fn run_extensions_build(
let distflag = if dist { "--minify" } else { "--sourcemap" };

// The NAPI build.
let mut napi_args = vec!["napi", "build", "--platform", "--output-dir", "src"];
let mut napi_args = vec!["exec", "napi", "build", "--platform", "--output-dir", "src"];
if dist {
napi_args.push("--release");
}
Expand All @@ -701,7 +701,7 @@ fn run_extensions_build(
target = tmp;
napi_args.extend(["--target", &target]);
}
run_script("npx", &napi_args, VSCODE_PATH, true)?;
run_script("pnpm", &napi_args, VSCODE_PATH, true)?;

// Ensure the JavaScript data structures generated from Rust are up to date.
run_cmd!(
Expand Down
283 changes: 121 additions & 162 deletions server/src/processing/cache-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ example fragment tag, after cache hydration: `<fragment id="id1"
contenteditable="false">See <a href="path/to/gather#bar">Bazzy things</a>, <a
href="path/to/another/gather#zap">Zappy things</a></fragment>`. 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), though a
Markdown document may include a fragment defined elsewhere; see
of the following code/doc block. In a Markdown document, a fragment's extent
is instead defined by the document's structure; see
<xref ref="cc-JzLRfBr83U"></xref>. Fragments may include the `following`
attribute to enclose a specific number of the following code/doc blocks; for
example, `<fragment id="bar" following="3"></fragment>` includes the current doc
Expand Down Expand Up @@ -222,165 +221,129 @@ things</h3>`:
Implementation of Fragments and gather elements
</h3>

<h2 id="cc-JzLRfBr83U">Inline inclusion of fragments</h2>

A gather element lists fragment content in a separate block, laid out like the
source it came from. An inline inclusion instead inserts the *text* of a
fragment into the surrounding content, where it takes on the formatting of its
context: included in a heading, the fragment's text becomes part of that
heading; included in a list item, part of that item. The syntax is `<include
ref="id"></include>`, where `ref` is the `id` of a `Fragment` (the name is
provisional; see the open questions). The element allows no attribute except
`ref`; all others are silently stripped during hydration. As for `<fragment>`,
content written inside an `<include>` in the source is moved after its closing
tag during hydration; the content which hydration places inside it is discarded
by dehydration.

An `<include>` may appear in any document, including a Markdown document. A
Markdown document can't define fragments (see
<xref ref="cc-gKXTXIZnFm"></xref>), but it can include one from a source file:
this lets a README or user guide quote the documentation kept beside the code
it describes, instead of a copy which drifts out of date.

For example, a source file contains this doc block, followed by a code block:

```c
// #### Install the *tools*
//
// <fragment id="id1" following="0"></fragment>
```
<h2 id="cc-JzLRfBr83U">Fragments in Markdown documents</h2>

A `<fragment>` may be defined in a Markdown document as well as in a source
file. The rules below replace the source-file rules in
<xref ref="cc-gKXTXIZnFm"></xref> for fragment extent, what `following` counts,
what `following="0"` contains, clamping `following`, and gather-list layout.
All other rules in that section apply unchanged.

### Fragment boundaries

Boundaries are computed on the HTML translated from Markdown, before cache
hydration.

* A *top-level block* is an element child of that HTML's root. Whitespace-only
text between blocks is ignored. Examples include paragraphs, headings,
lists (with all their items), tables, block quotes, code blocks, thematic
breaks, and elements written in raw HTML. One raw HTML block in the Markdown
may therefore produce several top-level blocks.
* A *heading* is an `<h1>`-`<h6>` top-level block, whether written in Markdown
or HTML. Gather elements written as headings therefore count as headings.
* The *anchor block* is the top-level block which contains the `<fragment>`
tag. A tag inside a list item, table cell, or block quote anchors to the
whole list, table, or block quote.

A fragment's extent is:

* **Without `following`: the section which contains the anchor block.** The
section starts at the nearest heading at or before the anchor block and ends
just before the next heading of the same or a higher level (`#` is the
highest), or at the end of the document. If no heading precedes the anchor
block, the section runs from the start of the document to just before its
first heading of any level.
* **With `following="n"`: the anchor block and the next `n` top-level blocks**,
without treating headings as boundaries. `following` is clamped, and an
unparsable value is an error, as in a source file.

The blocks in the extent are cleaned like any other fragment content (see
<xref ref="cc-2dkkF9YJnL"></xref>); in particular, `<fragment>` tags are
removed, so a paragraph which held only the tag contributes nothing. A gather
element shows a Markdown fragment as a single doc block with no indent,
preceded by the line number of its first line.

The extent depends only on the document's own source, so `Fragment::content`
stays in layer 3 (see <xref ref="cc-P-g5o100jN"></xref>); no new layer or kind
of dependency is needed.

### Interaction with gather elements

* A gather element in any file may list fragments from Markdown documents and
source files alike.
* A gather element inside a fragment's extent is an error, as in a source file:
each gather element which lists that fragment shows an error message for it.
A gather heading at the same or a higher level than the section's heading is
not inside the section; it ends it.
* The `<fragment>` tag hydrates to backlinks in place, exactly as in a source
file, and those backlinks are excluded from `Fragment::content` (layer 4).
* Extents may overlap or coincide; for example, two fragments without
`following` in the same section have the same content.

A Markdown document elsewhere in the project contains the heading `### Setup:
<include ref="id1"></include>`. After cache hydration, this heading becomes:
### Errors

```html
<h3>Setup: <include ref="id1" contenteditable="false">Install the <em>tools</em></include></h3>
This section adds no new error. The former rule that any fragment in a Markdown
document is an error is removed. The existing errors apply unchanged: a missing
or invalid `id`, a duplicate `id`, an unparsable `following`, and a gather
element inside the extent. An extent which is empty after cleaning, such as
`following="0"` on a paragraph holding only the tag, is not an error; it shows
no content, as an empty doc block does in a source file.

### Saving and reopening

Boundaries aren't stored. They're recomputed from the saved Markdown each time
the document is translated, so the saved source holds only the `<fragment>` tag
the user wrote. Dehydration removes the backlinks and `contenteditable`, as in a
source file, and the Faithful translation mode of the HTML-to-Markdown
conversion keeps the tag verbatim as inline raw HTML. An `id="*"` is replaced
as described in auto-assignment of ids.

The round trip must keep the tag in the same top-level block. In particular, a
paragraph which holds only a `<fragment>` tag must be saved as its own
paragraph, not merged into a neighbor. Reopening the file then reproduces the
same extent. Edits which add, remove, or change the level of a heading, or add
or remove blocks, change the extent on purpose. The resulting change to
`Fragment::content` outdates gather elements listing the fragment by the
existing rules.

### Examples

```markdown
Intro text. <fragment id="intro"></fragment>

## Install

<fragment id="install"></fragment>

Run `./bt install`. <fragment id="bt" following="0"></fragment>

### Windows

* Install `pwsh` first. <fragment id="win" following="0"></fragment>
* Then rerun the command.

## Run
```

The `<h4>` wrapper is dropped, since the host is already a heading; the `<em>`
stays. The paragraph which held the `<fragment>` tag is empty in the fragment's
content, since cleaning removed that tag, so it contributes nothing.

### Choosing the included content

The included content is built from the doc blocks of the fragment's content
(its `cc-fragment-doc-contents` elements), never from its layout wrappers
(`cc-fragment-*`, line numbers, indents) or its code blocks, which aren't doc
content. The *host* is the nearest block-level ancestor of the `<include>`: a
heading, `p`, `li`, `td`, `blockquote`, and so on. Doc blocks and Markdown
documents are both Markdown, so an `<include>` written alone on a line parses
as the only content of a `<p>`: an `<include>` always has a host.

1. **Block inclusion: the host is a `<p>` which is a direct child of its doc
block (or, in a Markdown document, of the document), and the `<include>` is
its only non-whitespace content.** The fragment's doc content is inserted
unchanged, keeping its headings, lists, code fences, and so on. Since a `<p>`
can't contain block content, the host `<p>` is replaced by `<div
class="cc-include" contenteditable="false">`, which holds the `<include>`,
now empty, followed by that content. Heading levels are not rewritten.
2. **Inline inclusion: every other case** (the usual one: the `<include>` sits
inside a heading, paragraph, or list item beside text). The content is
flattened to phrasing content, placed inside the `<include>`:
* Block wrappers (`p`, `h1`-`h6`, `div`, `blockquote`) are unwrapped and
their children kept, so the host's formatting applies, not the source's.
Heading levels in the source are discarded: an `<h1>` included in an
`<h4>` is `<h4>` text.
* Lists and tables are replaced by their text content, in document order,
separated by single spaces.
* The permitted content of an `<a>` (see
<xref ref="cc-2dkkF9YJnL"></xref>) is kept: inline formatting such as
`em`, `strong`, and `code`, inline math, and images. Links and
`<xref>`s are kept too, unless the `<include>` has an `<a>` ancestor
(in Markdown link text, for example), since links can't nest; there,
they are unwrapped and their content kept.
* Any other element, such as a diagram (`wc-mermaid`, `graphviz-graph`) or
an `iframe`, is omitted.
* Blocks left empty by these rules are dropped. Where unwrapping leaves two
blocks adjacent, a single space is inserted between them; `<br>` is kept.
Leading and trailing whitespace of the result is trimmed.
* A fragment with no doc content left by these rules produces an error
message.

In both cases, each `<xref>` in the included content which hydrated to a link
is re-hydrated from its `data-content` so that it links from the including
file, exactly as a gather list does (layer 5 under
<xref ref="cc-P-g5o100jN"></xref>); an error message is kept as is.

An `<include>` in the optional content of an `<xref>` is removed when that
content is <xref ref="cc-2dkkF9YJnL">cleaned</xref>, as a `<fragment>` is.

### Hydrated form and dehydration

The hydrated form always keeps the `<include ref="id">` element, so that
dehydration can recover `ref`, as an `<xref>` keeps its own:

* Inline inclusion: `<include ref="id" contenteditable="false">(content)
</include>`, in place.
* Block inclusion: `<div class="cc-include" contenteditable="false"><include
ref="id"></include>(content)</div>`, in place of the host `<p>`.
* An error: `<include ref="id" contenteditable="false"><span
class="cc-error">(message)</span></include>`, in place, in either case.

Dehydration removes the content and the `contenteditable` attribute of each
`<include>`, and replaces each `div.cc-include` with a `<p>` holding only its
`<include>`. The saved source is therefore the `<include ref="id"></include>`
the user wrote. In a Markdown document or doc block, this is inline raw HTML,
which the Faithful translation mode of the HTML-to-Markdown conversion keeps
verbatim, as it does an `<xref>`.

The Client must preserve the element: its TinyMCE configuration declares
`include` an inline custom element (`~include`) allowing the `contenteditable`
and `ref` attributes, as it does `xref`, and allows the `cc-include` class on a
`div`. The Server's sanitizer allowlist gains the same element, attributes, and
class.
* `intro`: no heading precedes it, so it contains `Intro text.` and stops at
`## Install`.
* `install`: the whole `## Install` section, including `### Windows` and its
list, stopping at `## Run`. The paragraph which held the tag is empty after
cleaning.
* `bt`: only the paragraph “Run `./bt install`.”, keeping the code formatting.
* `win`: the whole two-item list, since the list is the anchor block.

### Errors
```markdown
## Design <fragment id="design"></fragment>

Rationale.

<h3 id="notes" data-gather="bt win">Notes</h3>
```

Each of these replaces the content of the `<include>` with an error message,
shown wherever the include is; it never aborts hydration of the rest of the
file:

* `ref` is missing or empty, or isn't a valid CSS identifier.
* `ref` is not found, is defined more than once, or names a target or gather
element rather than a fragment.
* The fragment's own content is an error message (for example, it contains a
gather element, or is a fragment in a Markdown document); that message is
shown unchanged.
* The fragment contains no doc content after the rules of inline inclusion
above.

### Dependencies

An include is a reference like a cross-reference: its file is added to the
`IdEntry::dependents` of `ref`, including while the id is `Missing`. A change to
the fragment's content, or to the id's `IdState`, outdates the file by the
existing rules, so no new outdating rule is needed.

In terms of the layers under <xref ref="cc-P-g5o100jN">cache design</xref>:

* Include hydration is layer 6: it depends on layers 1 and 3 only, and nothing
depends on it. Like gather-list hydration, it runs after the file's own
fragments are committed, so including a fragment from the same file, or
from within the same doc block, needs no special case.
* An `<include>` inside a fragment is removed, along with its contents, when
`Fragment::content` is cleaned, as a `<fragment>` is. Were it kept, fragment
A including fragment B, which includes A, would outdate each other forever;
with the removal, a change still travels at most two steps. The cost is that a
gather element doesn't show what a listed fragment includes. For the same
reason, a fragment which includes itself shows its own content without the
included text.
* Likewise, `Target::inner_html` removes includes, since a cross-reference
renders it inside an `<a>`: an `<xref>` to a heading containing an include
shows the heading without the included text.

### Open questions

* Whether to reuse `<fragment>` itself instead of a new element. That would
conflict with the rules that a `<fragment>` requires an `id`, allows no
content, and hydrates to backlinks.
* Whether a block inclusion should rewrite heading levels to fit the section
it's in.
* Whether included content links back to its source, as a gather list does.
* `design` contains the gather element `notes`, so every gather element listing
`design` shows an error for it. Writing `following="1"` instead limits it to
the heading and `Rationale.`.

Search (future work)
--------------------
Expand Down Expand Up @@ -442,10 +405,6 @@ 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.
6. `<include>` hydration depends on layers 1 and 3, re-hydrating each `<xref>`
in the included content as layer 5 does; see
<xref ref="cc-JzLRfBr83U"></xref>. An `<include>` is removed from both
layers, so no cached state depends on this one.

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
Expand All @@ -456,8 +415,8 @@ 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 `<xref ref="id">`,
an `<include ref="id">`, or a `data-gather` list naming it. One uniform set
whose rendered output depends on this id, by either means -- an
`<xref ref="id">` 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).
Expand Down
Loading