@wastech-mdlint/viewer 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -2
  3. package/assets/favicon.ico +0 -0
  4. package/assets/viewer.css +4184 -0
  5. package/dist/asset-routes.d.ts +4 -0
  6. package/dist/asset-routes.js +107 -0
  7. package/dist/blame.d.ts +8 -0
  8. package/dist/blame.js +54 -0
  9. package/dist/branch-changes.d.ts +8 -0
  10. package/dist/branch-changes.js +49 -0
  11. package/dist/candidate-lint-worker.d.ts +1 -0
  12. package/dist/candidate-lint-worker.js +67 -0
  13. package/dist/candidate-lint.d.ts +39 -0
  14. package/dist/candidate-lint.js +115 -0
  15. package/dist/change-banner.d.ts +38 -0
  16. package/dist/change-banner.js +108 -0
  17. package/dist/change-diff.d.ts +23 -0
  18. package/dist/change-diff.js +140 -0
  19. package/dist/change-marks.d.ts +32 -0
  20. package/dist/change-marks.js +363 -0
  21. package/dist/changes-article.d.ts +15 -0
  22. package/dist/changes-article.js +244 -0
  23. package/dist/changes-routes.d.ts +12 -0
  24. package/dist/changes-routes.js +271 -0
  25. package/dist/changes.d.ts +113 -0
  26. package/dist/changes.js +324 -0
  27. package/dist/cli-args.d.ts +21 -0
  28. package/dist/cli-args.js +93 -0
  29. package/dist/client/banner-height.d.ts +1 -0
  30. package/dist/client/banner-height.js +33 -0
  31. package/dist/client/change-marks.d.ts +1 -0
  32. package/dist/client/change-marks.js +56 -0
  33. package/dist/client/changes.d.ts +1 -0
  34. package/dist/client/changes.js +183 -0
  35. package/dist/client/chart-option.d.ts +3 -0
  36. package/dist/client/chart-option.js +76 -0
  37. package/dist/client/charts.d.ts +1 -0
  38. package/dist/client/charts.js +54 -0
  39. package/dist/client/clipboard.d.ts +1 -0
  40. package/dist/client/clipboard.js +43 -0
  41. package/dist/client/code-copy.d.ts +1 -0
  42. package/dist/client/code-copy.js +29 -0
  43. package/dist/client/column-resize.d.ts +1 -0
  44. package/dist/client/column-resize.js +120 -0
  45. package/dist/client/config-dom.d.ts +13 -0
  46. package/dist/client/config-dom.js +47 -0
  47. package/dist/client/config-draft.d.ts +73 -0
  48. package/dist/client/config-draft.js +218 -0
  49. package/dist/client/config-editor.d.ts +3 -0
  50. package/dist/client/config-editor.js +677 -0
  51. package/dist/client/config-fields.d.ts +35 -0
  52. package/dist/client/config-fields.js +339 -0
  53. package/dist/client/config-json.d.ts +18 -0
  54. package/dist/client/config-json.js +96 -0
  55. package/dist/client/config-preview.d.ts +9 -0
  56. package/dist/client/config-preview.js +91 -0
  57. package/dist/client/config-rules.d.ts +14 -0
  58. package/dist/client/config-rules.js +202 -0
  59. package/dist/client/config-types.d.ts +91 -0
  60. package/dist/client/config-types.js +4 -0
  61. package/dist/client/copy-path.d.ts +1 -0
  62. package/dist/client/copy-path.js +32 -0
  63. package/dist/client/dashboard-requests.d.ts +11 -0
  64. package/dist/client/dashboard-requests.js +28 -0
  65. package/dist/client/dashboard.d.ts +1 -0
  66. package/dist/client/dashboard.js +119 -0
  67. package/dist/client/diagnostics.d.ts +1 -0
  68. package/dist/client/diagnostics.js +164 -0
  69. package/dist/client/diagrams.d.ts +1 -0
  70. package/dist/client/diagrams.js +81 -0
  71. package/dist/client/diff-expand.d.ts +1 -0
  72. package/dist/client/diff-expand.js +42 -0
  73. package/dist/client/display-settings-model.d.ts +74 -0
  74. package/dist/client/display-settings-model.js +229 -0
  75. package/dist/client/display-settings.d.ts +1 -0
  76. package/dist/client/display-settings.js +411 -0
  77. package/dist/client/echarts.d.ts +30 -0
  78. package/dist/client/echarts.js +14 -0
  79. package/dist/client/graph-layout.d.ts +88 -0
  80. package/dist/client/graph-layout.js +198 -0
  81. package/dist/client/graph.d.ts +2 -0
  82. package/dist/client/graph.js +566 -0
  83. package/dist/client/history.d.ts +21 -0
  84. package/dist/client/history.js +86 -0
  85. package/dist/client/live-reload-model.d.ts +28 -0
  86. package/dist/client/live-reload-model.js +52 -0
  87. package/dist/client/live-reload.d.ts +9 -0
  88. package/dist/client/live-reload.js +138 -0
  89. package/dist/client/menu.d.ts +14 -0
  90. package/dist/client/menu.js +176 -0
  91. package/dist/client/navigation-feedback.d.ts +1 -0
  92. package/dist/client/navigation-feedback.js +141 -0
  93. package/dist/client/open-config.d.ts +1 -0
  94. package/dist/client/open-config.js +48 -0
  95. package/dist/client/outline-spy.d.ts +1 -0
  96. package/dist/client/outline-spy.js +60 -0
  97. package/dist/client/prompts.d.ts +7 -0
  98. package/dist/client/prompts.js +65 -0
  99. package/dist/client/review.d.ts +1 -0
  100. package/dist/client/review.js +379 -0
  101. package/dist/client/search.d.ts +1 -0
  102. package/dist/client/search.js +199 -0
  103. package/dist/client/segmented.d.ts +13 -0
  104. package/dist/client/segmented.js +70 -0
  105. package/dist/client/source-lines.d.ts +16 -0
  106. package/dist/client/source-lines.js +47 -0
  107. package/dist/client/theme-toggle.d.ts +10 -0
  108. package/dist/client/theme-toggle.js +100 -0
  109. package/dist/client/theme.d.ts +0 -0
  110. package/dist/client/theme.js +50 -0
  111. package/dist/client/tree-marks.d.ts +31 -0
  112. package/dist/client/tree-marks.js +102 -0
  113. package/dist/client/tree-state.d.ts +0 -0
  114. package/dist/client/tree-state.js +179 -0
  115. package/dist/client/viewer.d.ts +1 -0
  116. package/dist/client/viewer.js +53 -0
  117. package/dist/config-editor.d.ts +166 -0
  118. package/dist/config-editor.js +378 -0
  119. package/dist/config-routes.d.ts +18 -0
  120. package/dist/config-routes.js +213 -0
  121. package/dist/context-view.d.ts +41 -0
  122. package/dist/context-view.js +295 -0
  123. package/dist/corpus-routes.d.ts +31 -0
  124. package/dist/corpus-routes.js +258 -0
  125. package/dist/corpus.d.ts +134 -0
  126. package/dist/corpus.js +653 -0
  127. package/dist/dashboard-article.d.ts +14 -0
  128. package/dist/dashboard-article.js +290 -0
  129. package/dist/dashboard.d.ts +94 -0
  130. package/dist/dashboard.js +468 -0
  131. package/dist/diagnostics.d.ts +23 -0
  132. package/dist/diagnostics.js +44 -0
  133. package/dist/document-routes.d.ts +39 -0
  134. package/dist/document-routes.js +396 -0
  135. package/dist/event-hub.d.ts +39 -0
  136. package/dist/event-hub.js +120 -0
  137. package/dist/findings-panel.d.ts +4 -0
  138. package/dist/findings-panel.js +74 -0
  139. package/dist/folder-page.d.ts +8 -0
  140. package/dist/folder-page.js +74 -0
  141. package/dist/graph-view.d.ts +66 -0
  142. package/dist/graph-view.js +236 -0
  143. package/dist/highlight.d.ts +24 -0
  144. package/dist/highlight.js +100 -0
  145. package/dist/http-send.d.ts +9 -0
  146. package/dist/http-send.js +53 -0
  147. package/dist/index.d.ts +2 -0
  148. package/dist/index.js +72 -0
  149. package/dist/open-file.d.ts +36 -0
  150. package/dist/open-file.js +87 -0
  151. package/dist/page-tree.d.ts +3 -0
  152. package/dist/page-tree.js +105 -0
  153. package/dist/page.d.ts +73 -0
  154. package/dist/page.js +478 -0
  155. package/dist/prompts.d.ts +25 -0
  156. package/dist/prompts.js +82 -0
  157. package/dist/render-cache.d.ts +23 -0
  158. package/dist/render-cache.js +38 -0
  159. package/dist/render-document.d.ts +60 -0
  160. package/dist/render-document.js +440 -0
  161. package/dist/request-path.d.ts +17 -0
  162. package/dist/request-path.js +51 -0
  163. package/dist/review-context.d.ts +24 -0
  164. package/dist/review-context.js +109 -0
  165. package/dist/review-queries.d.ts +26 -0
  166. package/dist/review-queries.js +69 -0
  167. package/dist/review-state.d.ts +106 -0
  168. package/dist/review-state.js +251 -0
  169. package/dist/review-store.d.ts +145 -0
  170. package/dist/review-store.js +530 -0
  171. package/dist/router.d.ts +28 -0
  172. package/dist/router.js +294 -0
  173. package/dist/search.d.ts +22 -0
  174. package/dist/search.js +164 -0
  175. package/dist/security.d.ts +95 -0
  176. package/dist/security.js +196 -0
  177. package/dist/server.d.ts +21 -0
  178. package/dist/server.js +224 -0
  179. package/dist/speculation-rules.d.ts +4 -0
  180. package/dist/speculation-rules.js +49 -0
  181. package/dist/state-dir.d.ts +6 -0
  182. package/dist/state-dir.js +31 -0
  183. package/dist/tree-marks.d.ts +49 -0
  184. package/dist/tree-marks.js +108 -0
  185. package/dist/viewer-context.d.ts +37 -0
  186. package/dist/viewer-context.js +74 -0
  187. package/dist/watch.d.ts +41 -0
  188. package/dist/watch.js +135 -0
  189. package/package.json +57 -4
@@ -0,0 +1,140 @@
1
+ import { h } from "hastscript";
2
+ import { contextGaps, } from "@wastech-mdlint/core";
3
+ // A diff drawn as HTML, and the small pieces every view of a change shares: the status badge and the
4
+ // `+added −removed` count. Kept apart from the Changes page and a document's banner, which both draw
5
+ // diffs, so the two can never draw the same diff two ways.
6
+ const STATUS_NAMES = {
7
+ A: "Added",
8
+ M: "Modified",
9
+ D: "Deleted",
10
+ R: "Renamed",
11
+ };
12
+ /** The word a status letter stands for, as the badge's title and the banner's sentence say it. */
13
+ export function statusName(status) {
14
+ return STATUS_NAMES[status];
15
+ }
16
+ export function statusBadge(status) {
17
+ return h(`span.change-status.change-status-${status}`, { title: STATUS_NAMES[status] }, status);
18
+ }
19
+ export function stats(diff) {
20
+ return `+${diff.added} −${diff.removed}`;
21
+ }
22
+ export function plural(count, noun) {
23
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
24
+ }
25
+ /**
26
+ * Each hunk of a diff on its own, inert inside a `<template>` until the page script shows one beside
27
+ * the block it changed. Rendered here, by the code that renders the Changes page, so the browser
28
+ * never draws a diff of its own that could disagree with it.
29
+ */
30
+ export function renderHunkTemplate(diff) {
31
+ return h("template.change-hunks", diff.hunks.map((hunk, index) => h("div.change-hunk", { dataHunk: index }, renderDiff({
32
+ hunks: [hunk],
33
+ added: 0,
34
+ removed: 0,
35
+ newlineAtEndChanged: false,
36
+ }))));
37
+ }
38
+ /**
39
+ * A diff as a table: base and head line numbers, a marker, the line. A paired line's changed words —
40
+ * or cells — are `del` and `ins`, so they read as changes to assistive technology as well as by color.
41
+ * A line moved elsewhere says where to, and the line it became says where from.
42
+ *
43
+ * With `headText`, the unchanged lines between the hunks are in the table too, hidden, each run in a
44
+ * body of its own behind a button the page script shows: the context a reader opens around a change
45
+ * comes from the server that drew the diff, so the browser never computes a line of its own.
46
+ */
47
+ export function renderDiff(diff, headText) {
48
+ if (diff.hunks.length === 0) {
49
+ return h("p.diff-empty", diff.newlineAtEndChanged
50
+ ? "Only the final line break changed."
51
+ : "The text is the same on both sides.");
52
+ }
53
+ const gaps = new Map((headText === undefined ? [] : contextGaps(diff, headText)).map((gap) => [
54
+ gap.before,
55
+ gap,
56
+ ]));
57
+ const bodies = [];
58
+ let rows = [];
59
+ const close = () => {
60
+ if (rows.length > 0) {
61
+ bodies.push(h("tbody", rows));
62
+ rows = [];
63
+ }
64
+ };
65
+ const gapBody = (index) => {
66
+ const gap = gaps.get(index);
67
+ if (gap !== undefined) {
68
+ close();
69
+ // Above the first hunk the lines nearest the change are the last ones, so they open from the
70
+ // bottom; everywhere else from the top, following the hunk above.
71
+ bodies.push(renderGap(gap, index === 0 ? "end" : "start"));
72
+ }
73
+ };
74
+ diff.hunks.forEach((hunk, index) => {
75
+ gapBody(index);
76
+ rows.push(h("tr.diff-hunk", [
77
+ h("td", { colSpan: 4 }, `@@ −${hunk.baseStart},${hunk.baseLines} +${hunk.headStart},${hunk.headLines} @@`),
78
+ ]));
79
+ for (const line of hunk.lines) {
80
+ rows.push(diffRow(line));
81
+ }
82
+ });
83
+ gapBody(diff.hunks.length);
84
+ if (diff.newlineAtEndChanged) {
85
+ rows.push(h("tr.diff-note", [
86
+ h("td", { colSpan: 4 }, "The final line break changed too."),
87
+ ]));
88
+ }
89
+ close();
90
+ return h("div.diff-wrap", h("table.diff", bodies));
91
+ }
92
+ // How many unchanged lines one press of a gap's button opens.
93
+ const GAP_STEP = 20;
94
+ function renderGap(gap, from) {
95
+ return h("tbody.diff-gap", { dataFrom: from, dataStep: GAP_STEP }, [
96
+ h("tr.diff-expand", { hidden: true }, h("td", { colSpan: 4 }, h("button.diff-expand-button", { type: "button" }, gap.lines.length <= GAP_STEP
97
+ ? `Show ${plural(gap.lines.length, "unchanged line")}`
98
+ : `Show ${GAP_STEP} of ${gap.lines.length} unchanged lines`))),
99
+ ...gap.lines.map((line) => {
100
+ const row = diffRow({ kind: "context", ...line });
101
+ row.properties.hidden = true;
102
+ return row;
103
+ }),
104
+ ]);
105
+ }
106
+ const MARKERS = {
107
+ added: "+",
108
+ removed: "−",
109
+ context: "",
110
+ };
111
+ // Where a moved line went or came from; `undefined` for a line that did not move.
112
+ function movedNote(line) {
113
+ if (line.kind === "removed" && line.movedTo !== undefined) {
114
+ return `moved to line ${line.movedTo}`;
115
+ }
116
+ if (line.kind === "added" && line.movedFrom !== undefined) {
117
+ return `moved from line ${line.movedFrom}`;
118
+ }
119
+ return undefined;
120
+ }
121
+ function diffRow(line) {
122
+ const moved = movedNote(line);
123
+ const base = line.kind === "added" ? "" : String(line.baseLine);
124
+ const head = line.kind === "removed" ? "" : String(line.headLine);
125
+ const tag = line.kind === "removed" ? "del" : "ins";
126
+ const code = line.kind !== "context" && line.segments !== undefined
127
+ ? line.segments.map((segment) => segment.changed
128
+ ? h(tag, segment.text)
129
+ : { type: "text", value: segment.text })
130
+ : [{ type: "text", value: line.text }];
131
+ return h(`tr.diff-line.diff-${line.kind}${moved === undefined ? "" : ".diff-moved"}`, [
132
+ h("td.diff-number", base),
133
+ h("td.diff-number", head),
134
+ h("td.diff-marker", MARKERS[line.kind]),
135
+ h("td.diff-code", [
136
+ ...code,
137
+ moved === undefined ? undefined : h("span.diff-move", moved),
138
+ ]),
139
+ ]);
140
+ }
@@ -0,0 +1,32 @@
1
+ import type { Root as HastRoot } from "hast";
2
+ import type { HeadChanges } from "@wastech-mdlint/core";
3
+ export type ChangeKind = "added" | "changed";
4
+ /** How one block changed, and which of the diff's hunks show it. */
5
+ export type BlockChange = {
6
+ kind: ChangeKind;
7
+ hunks: number[];
8
+ };
9
+ /** A range of 1-based source lines, first and last inclusive. */
10
+ export type LineRange = {
11
+ start: number;
12
+ end: number;
13
+ };
14
+ /** One side of the comparison, rendered: its tree and which of its lines hold no text. */
15
+ export type MarkedSide = {
16
+ tree: HastRoot;
17
+ frontmatter?: LineRange & {
18
+ raw: string;
19
+ };
20
+ blank: (line: number) => boolean;
21
+ };
22
+ /**
23
+ * Mark the head's changed blocks and put back the base blocks the branch removed, folded where they
24
+ * stood. Returns how the front matter changed, which the page marks on its card.
25
+ *
26
+ * A block is `added` when every line of text in it is new, and `changed` when it holds a new or
27
+ * edited line, or lost lines that were part of it, that no list item within it accounts for: the
28
+ * innermost block wins, so an edited item marks the item and not its list. Blank lines are left out
29
+ * of "every line", since the line diff matches a blank line with any other blank line and so may keep
30
+ * one that sat inside a block it otherwise removed or added.
31
+ */
32
+ export declare function markChanges(head: MarkedSide, base: MarkedSide, changes: HeadChanges): BlockChange | undefined;
@@ -0,0 +1,363 @@
1
+ import { h } from "hastscript";
2
+ import { visit } from "unist-util-visit";
3
+ /**
4
+ * Mark the head's changed blocks and put back the base blocks the branch removed, folded where they
5
+ * stood. Returns how the front matter changed, which the page marks on its card.
6
+ *
7
+ * A block is `added` when every line of text in it is new, and `changed` when it holds a new or
8
+ * edited line, or lost lines that were part of it, that no list item within it accounts for: the
9
+ * innermost block wins, so an edited item marks the item and not its list. Blank lines are left out
10
+ * of "every line", since the line diff matches a blank line with any other blank line and so may keep
11
+ * one that sat inside a block it otherwise removed or added.
12
+ */
13
+ export function markChanges(head, base, changes) {
14
+ const headBlocks = blocksIn(head.tree, false);
15
+ // The card as a block of its own, so its lines are matched like any other block's; its node is a
16
+ // stand-in, never placed in the tree.
17
+ const card = head.frontmatter === undefined
18
+ ? undefined
19
+ : frontmatterBlock(head.frontmatter, h("section"), head.tree);
20
+ const markable = card === undefined ? headBlocks : [card, ...headBlocks];
21
+ const { removed, lost } = compareWithBase(base, changes);
22
+ const lostBy = new Map();
23
+ for (const { removal, at } of lost) {
24
+ const node = blockLosing(markable, removal.after, at);
25
+ if (node !== undefined) {
26
+ lostBy.set(node, [...(lostBy.get(node) ?? []), removal.hunk]);
27
+ }
28
+ }
29
+ const marks = new Map();
30
+ collectMarks(markable, { changes, blank: head.blank, lostBy }, marks);
31
+ for (const [node, change] of marks) {
32
+ if (node !== card?.node) {
33
+ node.properties.dataChange = change.kind;
34
+ node.properties.dataChangeHunks = change.hunks.join(" ");
35
+ }
36
+ }
37
+ insertRemoved(head.tree, headBlocks, removed);
38
+ return card === undefined ? undefined : marks.get(card.node);
39
+ }
40
+ function rangeOf(node) {
41
+ if (node.position !== undefined) {
42
+ return { start: node.position.start.line, end: node.position.end.line };
43
+ }
44
+ // A block that scrolls sideways is wrapped, and the wrapper carries its lines instead.
45
+ const { dataLine, dataLineEnd } = node.properties;
46
+ return typeof dataLine === "number" && typeof dataLineEnd === "number"
47
+ ? { start: dataLine, end: dataLineEnd }
48
+ : undefined;
49
+ }
50
+ const isList = (node) => node.tagName === "ul" || node.tagName === "ol";
51
+ function blocksIn(container, itemsOnly) {
52
+ const blocks = [];
53
+ for (const child of container.children) {
54
+ if (child.type !== "element" || (itemsOnly && child.tagName !== "li")) {
55
+ continue;
56
+ }
57
+ const range = rangeOf(child);
58
+ if (range === undefined) {
59
+ continue;
60
+ }
61
+ const list = isList(child);
62
+ blocks.push({
63
+ ...range,
64
+ node: child,
65
+ container,
66
+ list,
67
+ children: list ? blocksIn(child, true) : listsWithin(child),
68
+ });
69
+ }
70
+ return blocks;
71
+ }
72
+ function listsWithin(node) {
73
+ const lists = [];
74
+ const walk = (parent) => {
75
+ for (const child of parent.children) {
76
+ if (child.type !== "element") {
77
+ continue;
78
+ }
79
+ const range = isList(child) ? rangeOf(child) : undefined;
80
+ if (range !== undefined) {
81
+ lists.push({
82
+ ...range,
83
+ node: child,
84
+ container: parent,
85
+ list: true,
86
+ children: blocksIn(child, true),
87
+ });
88
+ }
89
+ else {
90
+ walk(child);
91
+ }
92
+ }
93
+ };
94
+ walk(node);
95
+ return lists;
96
+ }
97
+ function frontmatterBlock(range, node, container) {
98
+ return {
99
+ start: range.start,
100
+ end: range.end,
101
+ node,
102
+ container,
103
+ list: false,
104
+ children: [],
105
+ };
106
+ }
107
+ function linesOf(range) {
108
+ const lines = [];
109
+ for (let line = range.start; line <= range.end; line += 1) {
110
+ lines.push(line);
111
+ }
112
+ return lines;
113
+ }
114
+ const within = (range, line) => line >= range.start && line <= range.end;
115
+ // Every block that can carry a mark, at any depth: everything but the lists themselves.
116
+ function markableBlocks(blocks) {
117
+ return blocks.flatMap((block) => [
118
+ ...(block.list ? [] : [block]),
119
+ ...markableBlocks(block.children),
120
+ ]);
121
+ }
122
+ function innermost(blocks) {
123
+ let found;
124
+ for (const block of blocks) {
125
+ if (found === undefined ||
126
+ block.end - block.start <= found.end - found.start) {
127
+ found = block;
128
+ }
129
+ }
130
+ return found;
131
+ }
132
+ function collectMarks(blocks, inputs, marks) {
133
+ const { changes, blank, lostBy } = inputs;
134
+ for (const block of blocks) {
135
+ if (block.list) {
136
+ collectMarks(block.children, inputs, marks);
137
+ continue;
138
+ }
139
+ const change = classify(block, changes, blank, lostBy.get(block.node));
140
+ // A wholly new block is marked once, not again on every new item inside it.
141
+ if (change?.kind !== "added") {
142
+ collectMarks(block.children, inputs, marks);
143
+ }
144
+ if (change !== undefined) {
145
+ marks.set(block.node, change);
146
+ }
147
+ }
148
+ }
149
+ function classify(block, changes, blank, lostHunks) {
150
+ const textLines = linesOf(block).filter((line) => !blank(line));
151
+ if (lostHunks === undefined &&
152
+ textLines.length > 0 &&
153
+ textLines.every((line) => changes.lines.get(line)?.kind === "added")) {
154
+ return {
155
+ kind: "added",
156
+ hunks: hunksOf(textLines.map((line) => changes.lines.get(line).hunk)),
157
+ };
158
+ }
159
+ const ownLines = linesOf(block).filter((line) => changes.lines.has(line) &&
160
+ !block.children.some((child) => within(child, line)));
161
+ if (ownLines.length === 0 && lostHunks === undefined) {
162
+ return undefined;
163
+ }
164
+ return {
165
+ kind: "changed",
166
+ hunks: hunksOf([
167
+ ...ownLines.map((line) => changes.lines.get(line).hunk),
168
+ ...(lostHunks ?? []),
169
+ ]),
170
+ };
171
+ }
172
+ function hunksOf(indexes) {
173
+ return [...new Set(indexes)].sort((a, b) => a - b);
174
+ }
175
+ /**
176
+ * Read the base against the diff. `removed`: the base's blocks whose every line of text left without
177
+ * a counterpart, outermost first — a list whose items all went is one removed list, while a list that
178
+ * kept some items has its removed items listed one by one; a block with an edited line is not removed,
179
+ * since the head shows it, changed. `lost`: removed lines that were part of a block that survived,
180
+ * such as a table's last row or a line of a wrapped paragraph.
181
+ */
182
+ function compareWithBase(base, changes) {
183
+ const gone = new Map();
184
+ for (const removal of changes.removals) {
185
+ for (const line of removal.baseLines) {
186
+ gone.set(line, removal);
187
+ }
188
+ }
189
+ const blocks = blocksIn(base.tree, false);
190
+ if (base.frontmatter !== undefined) {
191
+ blocks.unshift(frontmatterBlock(base.frontmatter, h("section.frontmatter", { ariaLabel: "Front matter" }, h("pre", base.frontmatter.raw)), base.tree));
192
+ }
193
+ const removed = [];
194
+ const kept = [];
195
+ const walk = (level) => {
196
+ for (const block of level) {
197
+ const textLines = linesOf(block).filter((line) => !base.blank(line));
198
+ if (textLines.length > 0 && textLines.every((line) => gone.has(line))) {
199
+ removed.push({ block, after: gone.get(textLines[0]).after });
200
+ continue;
201
+ }
202
+ if (!block.list) {
203
+ kept.push(block);
204
+ }
205
+ walk(block.children);
206
+ }
207
+ };
208
+ walk(blocks);
209
+ const lost = [];
210
+ for (const [line, removal] of gone) {
211
+ if (removed.some(({ block }) => within(block, line))) {
212
+ continue;
213
+ }
214
+ const holder = innermost(kept.filter((block) => within(block, line)));
215
+ // A blank line counts only from inside a block with no items in it, such as a code block: between
216
+ // two list items it is the list's spacing, not a line of either item.
217
+ if (holder === undefined ||
218
+ (base.blank(line) &&
219
+ (holder.children.length > 0 ||
220
+ line === holder.start ||
221
+ line === holder.end))) {
222
+ continue;
223
+ }
224
+ const remaining = linesOf(holder).filter((other) => !gone.has(other) && !base.blank(other));
225
+ lost.push({
226
+ removal,
227
+ at: lostAt(remaining, line),
228
+ });
229
+ }
230
+ return { removed, lost };
231
+ }
232
+ // Where in its block a removed line was: before every line the block still has, after every one,
233
+ // or between them.
234
+ function lostAt(remaining, line) {
235
+ if (remaining.every((other) => other > line)) {
236
+ return "start";
237
+ }
238
+ if (remaining.every((other) => other < line)) {
239
+ return "end";
240
+ }
241
+ return "middle";
242
+ }
243
+ // Which blocks to look in, in order, for the one that lost lines: lines lost at a block's start
244
+ // look first at the block that begins right after them, lines lost at its end at the block that
245
+ // ends right before them, and lines lost in its middle at the block they were inside.
246
+ const LOSING_ORDER = {
247
+ start: ["before", "inside", "behind"],
248
+ end: ["behind", "inside", "before"],
249
+ middle: ["inside", "behind", "before"],
250
+ };
251
+ /** The head block that lines removed after head line `after` were part of. */
252
+ function blockLosing(blocks, after, at) {
253
+ const candidates = markableBlocks(blocks);
254
+ const tests = {
255
+ inside: (block) => after >= block.start && after < block.end,
256
+ before: (block) => block.start === after + 1,
257
+ behind: (block) => block.end === after,
258
+ };
259
+ for (const test of LOSING_ORDER[at].map((name) => tests[name])) {
260
+ const found = innermost(candidates.filter(test));
261
+ if (found !== undefined) {
262
+ return found.node;
263
+ }
264
+ }
265
+ return undefined;
266
+ }
267
+ /**
268
+ * Put each removed block back where it stood, folded: between the head's blocks, or between the items
269
+ * of a list when it stood inside one. Neighbors that stood at the same place share one fold.
270
+ */
271
+ function insertRemoved(tree, headBlocks, removed) {
272
+ const groups = new Map();
273
+ for (const { block, after } of removed) {
274
+ const { container, before } = insertionPoint(headBlocks, tree, after);
275
+ let byPlace = groups.get(container);
276
+ if (byPlace === undefined) {
277
+ byPlace = new Map();
278
+ groups.set(container, byPlace);
279
+ }
280
+ const group = byPlace.get(before) ?? { container, blocks: [] };
281
+ group.blocks.push(block);
282
+ byPlace.set(before, group);
283
+ }
284
+ for (const byPlace of groups.values()) {
285
+ for (const [before, group] of byPlace) {
286
+ const children = group.container.children;
287
+ const index = before === undefined ? children.length : children.indexOf(before);
288
+ children.splice(index, 0, fold(group.container, group.blocks));
289
+ }
290
+ }
291
+ }
292
+ // Inside a list only when the place is strictly inside it, between two of its items. Removed lines
293
+ // right after a list's last item could as well have followed the list, and a top-level block put back
294
+ // inside a list would read as one of its items.
295
+ function insertionPoint(blocks, container, after) {
296
+ const inside = (block) => after >= block.start && after < block.end;
297
+ const holder = blocks.find(inside);
298
+ if (holder !== undefined) {
299
+ const list = holder.list ? holder : holder.children.find(inside);
300
+ if (list !== undefined) {
301
+ return insertionPoint(list.children, list.node, after);
302
+ }
303
+ }
304
+ return {
305
+ container,
306
+ before: blocks.find((block) => block.start > after)?.node,
307
+ };
308
+ }
309
+ function fold(container, blocks) {
310
+ const items = blocks.every((block) => block.node.tagName === "li");
311
+ const noun = items ? "list item" : "block";
312
+ const details = h("details.change-removed", [
313
+ h("summary", `${blocks.length} ${noun}${blocks.length === 1 ? "" : "s"} removed`),
314
+ h("div.change-removed-body", blocks.map((block) => detach(block))),
315
+ ]);
316
+ // A list holds only items; the fold becomes one, kept out of the list's numbering by the stylesheet.
317
+ return container.type === "element" && isList(container)
318
+ ? h("li.change-removed-item", details)
319
+ : details;
320
+ }
321
+ /**
322
+ * A removed block, ready to show inside the head's page. An item keeps its own list around it — an
323
+ * ordered one starting at the number the item had — so it renders as the item it was. Everything
324
+ * that ties it to its place in the base goes: base line numbers, which are not lines of this file,
325
+ * and ids and anchor names, which could duplicate the head's and send a link to the removed copy.
326
+ */
327
+ function detach(block) {
328
+ let node = block.node;
329
+ const [only] = node.children;
330
+ if (node.tagName === "div" &&
331
+ node.properties.dataLine !== undefined &&
332
+ node.children.length === 1 &&
333
+ only?.type === "element") {
334
+ // The wrapper only exists to hold a line number beside a block that scrolls sideways.
335
+ node = only;
336
+ }
337
+ visit(node, "element", (element) => {
338
+ delete element.properties.dataLine;
339
+ delete element.properties.dataLineEnd;
340
+ delete element.properties.id;
341
+ delete element.properties.name;
342
+ });
343
+ if (node.tagName !== "li" || block.container.type !== "element") {
344
+ return node;
345
+ }
346
+ const list = block.container;
347
+ const start = list.tagName === "ol"
348
+ ? listStart(list) +
349
+ list.children
350
+ .filter((child) => child.type === "element" && child.tagName === "li")
351
+ .indexOf(block.node)
352
+ : undefined;
353
+ return h(list.tagName, {
354
+ className: list.properties.className,
355
+ start: start === 1 ? undefined : start,
356
+ }, [node]);
357
+ }
358
+ // Where an ordered list starts counting. `0` is a start like any other, so only a missing or
359
+ // unreadable value falls back to the default of 1.
360
+ function listStart(list) {
361
+ const start = Number(list.properties.start ?? 1);
362
+ return Number.isFinite(start) ? start : 1;
363
+ }
@@ -0,0 +1,15 @@
1
+ import type { ElementContent } from "hast";
2
+ import { type DocumentReview, type ReviewContext, type ReviewResult } from "./changes.js";
3
+ import { type Baseline, type ReviewMarksView } from "./review-state.js";
4
+ /** How the Changes page shows the reader's review: the marks, and what it compares with. */
5
+ export type ReviewView = {
6
+ marks: ReviewMarksView;
7
+ baseline: Baseline;
8
+ since: ReadonlyMap<string, DocumentReview>;
9
+ };
10
+ /**
11
+ * The Changes page's article, rendered on the server so it reads the same without script. With the
12
+ * review's context, the summary also says what the change does to findings and every session's
13
+ * budget, the documents it reaches follow the list, and each card shows its document's context.
14
+ */
15
+ export declare function renderChangesArticle(result: ReviewResult, worktree: boolean, context?: ReviewContext, reviewed?: ReviewView): ElementContent[];