@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,199 @@
1
+ // Turns the header's search form into a live list of matches. The form also works without this
2
+ // script — it submits to `/search`, which renders the same results as a page — so this only adds
3
+ // typing-as-you-go, keyboard selection and the Ctrl+K shortcut.
4
+ // Fewer characters than this match most of the corpus and say nothing useful.
5
+ const MIN_QUERY_LENGTH = 2;
6
+ export function initSearch() {
7
+ const form = document.querySelector("form.search");
8
+ const input = form?.querySelector('input[name="q"]');
9
+ if (form === null || input === null || input === undefined) {
10
+ return;
11
+ }
12
+ const box = new LiveSearch(input, resultList(form, input));
13
+ input.addEventListener("input", () => {
14
+ box.schedule();
15
+ });
16
+ input.addEventListener("keydown", (event) => {
17
+ box.onKey(event);
18
+ });
19
+ input.addEventListener("focus", () => {
20
+ box.reopen();
21
+ });
22
+ document.addEventListener("click", (event) => {
23
+ if (!form.contains(event.target)) {
24
+ box.close();
25
+ }
26
+ });
27
+ bindShortcuts(input);
28
+ }
29
+ function resultList(form, input) {
30
+ const list = document.createElement("ul");
31
+ list.className = "search-results";
32
+ // With a `.`, which no heading slug contains, so a document's heading "Search results" cannot take
33
+ // the id the input's `aria-controls` names; the hits below follow the same rule.
34
+ list.id = "viewer.search-results";
35
+ list.setAttribute("role", "listbox");
36
+ list.hidden = true;
37
+ form.append(list);
38
+ input.setAttribute("role", "combobox");
39
+ input.setAttribute("aria-controls", list.id);
40
+ input.setAttribute("aria-autocomplete", "list");
41
+ input.setAttribute("aria-expanded", "false");
42
+ return list;
43
+ }
44
+ // Ctrl+K (Cmd+K on macOS) from anywhere, and `/` when the reader is not already typing — the two
45
+ // shortcuts documentation sites have taught people to reach for.
46
+ function bindShortcuts(input) {
47
+ document.addEventListener("keydown", (event) => {
48
+ const typing = event.target instanceof HTMLElement &&
49
+ (event.target.isContentEditable ||
50
+ /^(INPUT|TEXTAREA|SELECT)$/.test(event.target.tagName));
51
+ if (((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "k") ||
52
+ (event.key === "/" && !typing)) {
53
+ event.preventDefault();
54
+ input.focus();
55
+ input.select();
56
+ }
57
+ });
58
+ }
59
+ // The state of one search box: the hits on show, which of them the keyboard has selected, and which
60
+ // request is the newest.
61
+ class LiveSearch {
62
+ input;
63
+ list;
64
+ hits = [];
65
+ active = -1;
66
+ timer;
67
+ // Responses can arrive out of order; only the newest query's answer is shown.
68
+ latestRequest = 0;
69
+ constructor(input, list) {
70
+ this.input = input;
71
+ this.list = list;
72
+ }
73
+ schedule() {
74
+ window.clearTimeout(this.timer);
75
+ this.timer = window.setTimeout(() => void this.run(), 120);
76
+ }
77
+ close() {
78
+ this.list.hidden = true;
79
+ this.input.setAttribute("aria-expanded", "false");
80
+ this.input.removeAttribute("aria-activedescendant");
81
+ this.active = -1;
82
+ }
83
+ reopen() {
84
+ if (this.hits.length > 0 &&
85
+ this.input.value.trim().length >= MIN_QUERY_LENGTH) {
86
+ this.list.hidden = false;
87
+ this.input.setAttribute("aria-expanded", "true");
88
+ }
89
+ }
90
+ onKey(event) {
91
+ const listed = !this.list.hidden && this.hits.length > 0;
92
+ if (event.key === "ArrowDown" && listed) {
93
+ event.preventDefault();
94
+ this.select((this.active + 1) % this.hits.length);
95
+ }
96
+ else if (event.key === "ArrowUp" && listed) {
97
+ event.preventDefault();
98
+ this.select(this.active <= 0 ? this.hits.length - 1 : this.active - 1);
99
+ }
100
+ else if (event.key === "Enter" && this.active >= 0 && !this.list.hidden) {
101
+ // With nothing selected, Enter submits the form and opens the full results page instead.
102
+ event.preventDefault();
103
+ location.href = this.hits[this.active].href;
104
+ }
105
+ else if (event.key === "Escape") {
106
+ this.close();
107
+ this.input.blur();
108
+ }
109
+ }
110
+ select(index) {
111
+ this.active = index;
112
+ for (const [position, item] of [...this.list.children].entries()) {
113
+ item.setAttribute("aria-selected", String(position === index));
114
+ }
115
+ if (index >= 0) {
116
+ this.input.setAttribute("aria-activedescendant", `viewer.search-hit-${index}`);
117
+ this.list.children[index]?.scrollIntoView({ block: "nearest" });
118
+ }
119
+ else {
120
+ this.input.removeAttribute("aria-activedescendant");
121
+ }
122
+ }
123
+ render(query) {
124
+ this.list.replaceChildren(...(this.hits.length === 0
125
+ ? [emptyItem(`No documents match “${query}”.`)]
126
+ : this.hits.map((hit, index) => hitItem(hit, index))));
127
+ this.list.hidden = false;
128
+ this.input.setAttribute("aria-expanded", "true");
129
+ this.select(-1);
130
+ }
131
+ async run() {
132
+ const query = this.input.value.trim();
133
+ if (query.length < MIN_QUERY_LENGTH) {
134
+ // Outdates any answer still on its way: it is for the longer query the reader has just
135
+ // shortened, and would reopen the list with results for text the box no longer holds.
136
+ this.latestRequest += 1;
137
+ this.hits = [];
138
+ this.close();
139
+ return;
140
+ }
141
+ const request = ++this.latestRequest;
142
+ try {
143
+ const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`);
144
+ const data = (await response.json());
145
+ if (request !== this.latestRequest) {
146
+ return;
147
+ }
148
+ this.hits = data.results;
149
+ this.render(query);
150
+ }
151
+ catch {
152
+ // The server went away (it is restarting, or was stopped); the form still submits normally.
153
+ }
154
+ }
155
+ }
156
+ function hitItem(hit, index) {
157
+ const item = document.createElement("li");
158
+ item.id = `viewer.search-hit-${index}`;
159
+ item.setAttribute("role", "option");
160
+ const link = document.createElement("a");
161
+ link.href = hit.href;
162
+ const title = document.createElement("span");
163
+ title.className = "search-hit-title";
164
+ title.append(...highlighted(hit.title, hit.terms));
165
+ const path = document.createElement("span");
166
+ path.className = "search-hit-path";
167
+ path.textContent = hit.path;
168
+ const snippet = document.createElement("span");
169
+ snippet.className = "search-hit-snippet";
170
+ snippet.append(...highlighted(hit.snippet, hit.terms));
171
+ link.append(title, path, snippet);
172
+ item.append(link);
173
+ return item;
174
+ }
175
+ function emptyItem(text) {
176
+ const item = document.createElement("li");
177
+ item.className = "search-empty";
178
+ item.textContent = text;
179
+ return item;
180
+ }
181
+ // Built from text nodes and <mark> elements, never from HTML strings: titles and snippets are
182
+ // repository text, as untrusted as the documents they come from.
183
+ function highlighted(text, terms) {
184
+ const escaped = terms
185
+ .filter((term) => term.length > 0)
186
+ .map((term) => term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
187
+ if (escaped.length === 0) {
188
+ return [document.createTextNode(text)];
189
+ }
190
+ const pattern = new RegExp(`(${escaped.join("|")})`, "giu");
191
+ return text.split(pattern).map((part, index) => {
192
+ if (index % 2 === 0) {
193
+ return document.createTextNode(part);
194
+ }
195
+ const mark = document.createElement("mark");
196
+ mark.textContent = part;
197
+ return mark;
198
+ });
199
+ }
@@ -0,0 +1,13 @@
1
+ /** -1 or 1 for an arrow key that moves a radio group's choice back or forward, 0 for any other key. */
2
+ export declare function arrowStep(event: KeyboardEvent): -1 | 0 | 1;
3
+ export type Segmented<T> = {
4
+ element: HTMLElement;
5
+ /** Show `value` as the chosen segment, or none when it is not one of the choices. */
6
+ render(value: T | undefined): void;
7
+ };
8
+ /**
9
+ * A segmented control named `name` over `options`, each a value and its caption. A click or an arrow
10
+ * key chooses, shows the choice at once and reports it to `onChoose`; `render` shows a value that
11
+ * changed elsewhere, such as a reset.
12
+ */
13
+ export declare function segmentedControl<T>(name: string, options: readonly (readonly [T, string])[], onChoose: (value: T) => void, className?: string): Segmented<T>;
@@ -0,0 +1,70 @@
1
+ // A one-of-few choice drawn as a segmented control: a row of buttons in a gray track, the chosen one
2
+ // lifted onto a thumb. Semantically a radio group, because that is what it is to a screen reader and
3
+ // to the keyboard — one tab stop on the checked segment, the arrow keys moving the choice — whereas a
4
+ // row of plain toggle buttons would read as several independent switches.
5
+ /** -1 or 1 for an arrow key that moves a radio group's choice back or forward, 0 for any other key. */
6
+ export function arrowStep(event) {
7
+ switch (event.key) {
8
+ case "ArrowRight":
9
+ case "ArrowDown":
10
+ return 1;
11
+ case "ArrowLeft":
12
+ case "ArrowUp":
13
+ return -1;
14
+ default:
15
+ return 0;
16
+ }
17
+ }
18
+ /**
19
+ * A segmented control named `name` over `options`, each a value and its caption. A click or an arrow
20
+ * key chooses, shows the choice at once and reports it to `onChoose`; `render` shows a value that
21
+ * changed elsewhere, such as a reset.
22
+ */
23
+ export function segmentedControl(name, options, onChoose, className = "segmented is-compact") {
24
+ const element = document.createElement("div");
25
+ element.className = className;
26
+ element.setAttribute("role", "radiogroup");
27
+ element.setAttribute("aria-label", name);
28
+ let current;
29
+ const buttons = options.map(([value, caption]) => {
30
+ const button = document.createElement("button");
31
+ button.type = "button";
32
+ button.className = "segment";
33
+ button.setAttribute("role", "radio");
34
+ button.textContent = caption;
35
+ button.addEventListener("click", () => {
36
+ choose(value);
37
+ });
38
+ return button;
39
+ });
40
+ element.append(...buttons);
41
+ const render = (value) => {
42
+ current = value;
43
+ const index = options.findIndex(([option]) => option === value);
44
+ buttons.forEach((button, at) => {
45
+ button.setAttribute("aria-checked", String(at === index));
46
+ // With nothing chosen the first segment is the group's tab stop, or the keyboard could not
47
+ // reach the group at all.
48
+ button.tabIndex = at === Math.max(index, 0) ? 0 : -1;
49
+ });
50
+ };
51
+ const choose = (value) => {
52
+ if (value === current) {
53
+ return;
54
+ }
55
+ render(value);
56
+ onChoose(value);
57
+ };
58
+ element.addEventListener("keydown", (event) => {
59
+ const step = arrowStep(event);
60
+ if (step === 0) {
61
+ return;
62
+ }
63
+ event.preventDefault();
64
+ const index = options.findIndex(([option]) => option === current);
65
+ const next = (Math.max(index, 0) + step + options.length) % options.length;
66
+ choose(options[next][0]);
67
+ buttons[next].focus();
68
+ });
69
+ return { element, render };
70
+ }
@@ -0,0 +1,16 @@
1
+ export declare function initSourceLines(): void;
2
+ /**
3
+ * The lines a `#L<n>` or `#L<n>-L<m>` fragment selects in a file of `lineCount` lines, first to last,
4
+ * or `undefined` when it selects none. Either end may be written first, and either may name a column
5
+ * (`#L5C3-L9C1`), which selects its whole line. The range is cut to the file, so a fragment naming
6
+ * line 999999999 — in a link or typed into the address — costs as many steps as the file has lines,
7
+ * not a billion.
8
+ *
9
+ * The grammar is the one core reads line fragments with (`parseLineFragment`), which a module served
10
+ * to the browser cannot import; a viewer test runs both through one table of fragments, so a link
11
+ * the review calls a line range is one this page marks, and the reverse.
12
+ */
13
+ export declare function lineRange(hash: string, lineCount: number): {
14
+ first: number;
15
+ last: number;
16
+ } | undefined;
@@ -0,0 +1,47 @@
1
+ // A document's link to a span of a file is written the GitHub way, `file.ts#L12-L20`. Single lines
2
+ // work without script because each line has an `id`; a range names no element, so it is resolved
3
+ // here: the lines are marked and the first one is scrolled into view.
4
+ export function initSourceLines() {
5
+ if (document.querySelector(".source-view") === null) {
6
+ return;
7
+ }
8
+ highlightLineRange();
9
+ window.addEventListener("hashchange", highlightLineRange);
10
+ }
11
+ /**
12
+ * The lines a `#L<n>` or `#L<n>-L<m>` fragment selects in a file of `lineCount` lines, first to last,
13
+ * or `undefined` when it selects none. Either end may be written first, and either may name a column
14
+ * (`#L5C3-L9C1`), which selects its whole line. The range is cut to the file, so a fragment naming
15
+ * line 999999999 — in a link or typed into the address — costs as many steps as the file has lines,
16
+ * not a billion.
17
+ *
18
+ * The grammar is the one core reads line fragments with (`parseLineFragment`), which a module served
19
+ * to the browser cannot import; a viewer test runs both through one table of fragments, so a link
20
+ * the review calls a line range is one this page marks, and the reverse.
21
+ */
22
+ export function lineRange(hash, lineCount) {
23
+ const match = /^#L([1-9]\d*)(?:C[1-9]\d*)?(?:-L([1-9]\d*)(?:C[1-9]\d*)?)?$/.exec(hash);
24
+ if (match === null) {
25
+ return undefined;
26
+ }
27
+ const start = Number(match[1]);
28
+ const end = match[2] === undefined ? start : Number(match[2]);
29
+ const first = Math.max(1, Math.min(start, end));
30
+ const last = Math.min(lineCount, Math.max(start, end));
31
+ return first <= last ? { first, last } : undefined;
32
+ }
33
+ function highlightLineRange() {
34
+ for (const line of document.querySelectorAll(".line.is-selected")) {
35
+ line.classList.remove("is-selected");
36
+ }
37
+ const range = lineRange(location.hash, document.querySelectorAll(".source-view .line").length);
38
+ if (range === undefined) {
39
+ return;
40
+ }
41
+ for (let number = range.first; number <= range.last; number += 1) {
42
+ document.getElementById(`L${number}`)?.classList.add("is-selected");
43
+ }
44
+ document
45
+ .getElementById(`L${range.first}`)
46
+ ?.scrollIntoView({ block: "center" });
47
+ }
@@ -0,0 +1,10 @@
1
+ export type Theme = "light" | "dark";
2
+ /** The theme the page is showing now, whichever way it was chosen. */
3
+ export declare function effectiveTheme(): Theme;
4
+ /**
5
+ * Call `listener` whenever the theme the page shows changes: from the switch, or from the operating
6
+ * system while the choice is "Auto". Parts of the page that cannot follow CSS alone — Mermaid bakes
7
+ * its colors into each SVG — redraw on it.
8
+ */
9
+ export declare function onThemeChange(listener: (theme: Theme) => void): void;
10
+ export declare function initThemeToggle(): void;
@@ -0,0 +1,100 @@
1
+ // The header's theme switch: a segmented control of three choices rather than a light/dark flip,
2
+ // because "follow the system" is a choice of its own: a reader who picked dark once should be able to
3
+ // hand control back to the operating system without clearing site data.
4
+ import { arrowStep } from "./segmented.js";
5
+ // Shared with `theme.ts`, which applies the stored choice before the page paints.
6
+ const STORAGE_KEY = "wastech-mdlint-viewer:theme";
7
+ const ORDER = ["system", "light", "dark"];
8
+ const CHANGE_EVENT = "wastech-mdlint-viewer:theme-change";
9
+ const systemDark = matchMedia("(prefers-color-scheme: dark)");
10
+ function currentChoice() {
11
+ const theme = document.documentElement.dataset.theme;
12
+ return theme === "light" || theme === "dark" ? theme : "system";
13
+ }
14
+ /** The theme the page is showing now, whichever way it was chosen. */
15
+ export function effectiveTheme() {
16
+ const choice = currentChoice();
17
+ if (choice !== "system") {
18
+ return choice;
19
+ }
20
+ return systemDark.matches ? "dark" : "light";
21
+ }
22
+ /**
23
+ * Call `listener` whenever the theme the page shows changes: from the switch, or from the operating
24
+ * system while the choice is "Auto". Parts of the page that cannot follow CSS alone — Mermaid bakes
25
+ * its colors into each SVG — redraw on it.
26
+ */
27
+ export function onThemeChange(listener) {
28
+ document.addEventListener(CHANGE_EVENT, () => {
29
+ listener(effectiveTheme());
30
+ });
31
+ systemDark.addEventListener("change", () => {
32
+ if (currentChoice() === "system") {
33
+ listener(effectiveTheme());
34
+ }
35
+ });
36
+ }
37
+ export function initThemeToggle() {
38
+ const group = document.querySelector(".theme-switch");
39
+ if (group === null) {
40
+ return;
41
+ }
42
+ // Rendered hidden by the server: without this script the control could do nothing.
43
+ group.hidden = false;
44
+ const segments = [
45
+ ...group.querySelectorAll("button[data-choice]"),
46
+ ];
47
+ // The chosen segment's look is the stylesheet's, from the choice on <html>, so it is right before
48
+ // this script runs. What is left here is what CSS cannot set: which radio is checked for assistive
49
+ // technology, and that only the checked one is a tab stop, as in any radio group.
50
+ const render = (choice) => {
51
+ for (const segment of segments) {
52
+ const checked = segment.dataset.choice === choice;
53
+ segment.setAttribute("aria-checked", String(checked));
54
+ segment.tabIndex = checked ? 0 : -1;
55
+ }
56
+ };
57
+ const choose = (choice, focus) => {
58
+ if (choice === "system") {
59
+ delete document.documentElement.dataset.theme;
60
+ }
61
+ else {
62
+ document.documentElement.dataset.theme = choice;
63
+ }
64
+ try {
65
+ if (choice === "system") {
66
+ localStorage.removeItem(STORAGE_KEY);
67
+ }
68
+ else {
69
+ localStorage.setItem(STORAGE_KEY, choice);
70
+ }
71
+ }
72
+ catch {
73
+ // Not remembered across pages, but applied to this one.
74
+ }
75
+ render(choice);
76
+ if (focus) {
77
+ segments.find((segment) => segment.dataset.choice === choice)?.focus();
78
+ }
79
+ document.dispatchEvent(new Event(CHANGE_EVENT));
80
+ };
81
+ render(currentChoice());
82
+ for (const segment of segments) {
83
+ segment.addEventListener("click", () => {
84
+ const choice = segment.dataset.choice;
85
+ if (choice !== currentChoice()) {
86
+ choose(choice, false);
87
+ }
88
+ });
89
+ }
90
+ // The arrow keys move the choice, the way they move it in a group of radio buttons.
91
+ group.addEventListener("keydown", (event) => {
92
+ const step = arrowStep(event);
93
+ if (step === 0) {
94
+ return;
95
+ }
96
+ event.preventDefault();
97
+ const index = ORDER.indexOf(currentChoice());
98
+ choose(ORDER[(index + step + ORDER.length) % ORDER.length], true);
99
+ });
100
+ }
File without changes
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ // Loaded in <head> as a classic, blocking script — like `tree-state.ts` after the tree, not a deferred
3
+ // module — because it has to run before the first paint. Applied any later, a page whose reader chose
4
+ // dark would flash light on every navigation, and a widened side column would snap back to its
5
+ // default width and then jump. It only reads what was stored; the header switch that writes the
6
+ // theme lives in `theme-toggle.ts`, the column edge handles that write the widths in
7
+ // `column-resize.ts` and the display settings panel in `display-settings.ts`, each with the same
8
+ // storage keys as here.
9
+ (() => {
10
+ // Says that script runs here. The header's script-driven controls are rendered `hidden`, since
11
+ // without script they could do nothing; the stylesheet lays them out under this mark from the very
12
+ // first paint instead of waiting for the deferred module that wires them, which on a long page can
13
+ // run after the bar is drawn and would then push the search box and the links aside on every page.
14
+ document.documentElement.dataset.script = "";
15
+ try {
16
+ const stored = localStorage.getItem("wastech-mdlint-viewer:theme");
17
+ if (stored === "light" || stored === "dark") {
18
+ document.documentElement.dataset.theme = stored;
19
+ }
20
+ for (const [key, variable] of [
21
+ ["wastech-mdlint-viewer:sidebar-width", "--sidebar-width"],
22
+ ["wastech-mdlint-viewer:rail-width", "--rail-width"],
23
+ ]) {
24
+ const width = Number(localStorage.getItem(key));
25
+ if (width > 0) {
26
+ // Capped by the window it opens in, which may be narrower than the one it was set in; at
27
+ // two fifths each, both columns at their cap still leave the article a fifth of it.
28
+ document.documentElement.style.setProperty(variable, `min(${width}px, 40vw)`);
29
+ }
30
+ }
31
+ // The reader's display settings, as the custom properties the panel in `display-settings.ts`
32
+ // computed from them and stored beside them. This script cannot import that module, so it takes
33
+ // the finished values and sets only properties named like the panel's own — `--reader-…` — which
34
+ // the stylesheet reads with its defaults as fallbacks. The key and the name pattern are
35
+ // `STORAGE_KEY` and `VARIABLE_NAME` in `display-settings-model.ts`, and a test holds them equal.
36
+ const display = JSON.parse(localStorage.getItem("wastech-mdlint-viewer:display") ?? "null");
37
+ const vars = display?.vars;
38
+ if (typeof vars === "object" && vars !== null) {
39
+ for (const [name, value] of Object.entries(vars)) {
40
+ if (/^--reader-[a-z-]+$/.test(name) && typeof value === "string") {
41
+ document.documentElement.style.setProperty(name, value);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ catch {
47
+ // Storage can be blocked, or hold something that is not JSON; the page then uses the defaults:
48
+ // the system theme, default widths and the stylesheet's own text settings.
49
+ }
50
+ })();
@@ -0,0 +1,31 @@
1
+ export type ReviewStatus = "unreviewed" | "reviewed" | "changed";
2
+ export type ChangeLetter = "A" | "M" | "D" | "R";
3
+ /** A `<span>` as a mark is drawn: its classes, its text and its other attributes. */
4
+ export type MarkSpec = {
5
+ className: string;
6
+ text: string;
7
+ attributes: Record<string, string>;
8
+ };
9
+ /** A file's count of findings, red when any is an error. */
10
+ export declare function findingsBadge(counts: {
11
+ errors: number;
12
+ warnings: number;
13
+ }): MarkSpec;
14
+ /** The letter of what the branch did to a file, naming where a renamed one came from. */
15
+ export declare function changeLetter(entry: {
16
+ status: ChangeLetter;
17
+ oldPath?: string;
18
+ }): MarkSpec;
19
+ /** The dot beside a changed file left to review: never reviewed, or changed since. */
20
+ export declare function reviewDot(status: ReviewStatus | null): MarkSpec | undefined;
21
+ /**
22
+ * The count on the header's Changes section: what is left against everything that changed, or only
23
+ * how many changed where marks cannot be read. Nothing when nothing changed.
24
+ */
25
+ export declare function changesCount(changed: number, review: {
26
+ left: number;
27
+ total: number;
28
+ } | null): MarkSpec | undefined;
29
+ export declare function markElement(spec: MarkSpec): HTMLSpanElement;
30
+ /** Whether the server drew these marks into the page as they are now, so none need asking for. */
31
+ export declare function drawnByServer(marks: "findings" | "changes"): boolean;
@@ -0,0 +1,102 @@
1
+ // The marks beside the tree's files and on the header's Changes section, as the page script redraws
2
+ // them when an event says they changed. The server draws the same marks into every page
3
+ // (`tree-marks.ts` beside the server's modules), and these builders produce exactly its markup — a
4
+ // test renders both and holds them equal — so a mark redrawn here looks like the one it replaces.
5
+ // They describe a mark rather than build it, so that test can read them without a page.
6
+ const STATUS_NAMES = {
7
+ A: "Added in this branch",
8
+ M: "Modified in this branch",
9
+ D: "Deleted in this branch",
10
+ R: "Renamed in this branch",
11
+ };
12
+ const REVIEW_NAMES = {
13
+ unreviewed: "Not reviewed yet",
14
+ changed: "Changed since your review",
15
+ };
16
+ /** A file's count of findings, red when any is an error. */
17
+ export function findingsBadge(counts) {
18
+ return {
19
+ className: counts.errors > 0 ? "tree-badge tree-badge-error" : "tree-badge",
20
+ text: String(counts.errors + counts.warnings),
21
+ attributes: {
22
+ title: [
23
+ counts.errors > 0 ? plural(counts.errors, "error") : "",
24
+ counts.warnings > 0 ? plural(counts.warnings, "warning") : "",
25
+ ]
26
+ .filter((part) => part !== "")
27
+ .join(", "),
28
+ },
29
+ };
30
+ }
31
+ /** The letter of what the branch did to a file, naming where a renamed one came from. */
32
+ export function changeLetter(entry) {
33
+ return {
34
+ className: `tree-change tree-change-${entry.status}`,
35
+ text: entry.status,
36
+ attributes: {
37
+ title: entry.oldPath === undefined
38
+ ? STATUS_NAMES[entry.status]
39
+ : `${STATUS_NAMES[entry.status]}, from ${entry.oldPath}`,
40
+ },
41
+ };
42
+ }
43
+ /** The dot beside a changed file left to review: never reviewed, or changed since. */
44
+ export function reviewDot(status) {
45
+ if (status === null || status === "reviewed") {
46
+ return undefined;
47
+ }
48
+ return {
49
+ className: `tree-review-dot tree-review-${status}`,
50
+ text: "",
51
+ attributes: {
52
+ role: "img",
53
+ "aria-label": REVIEW_NAMES[status],
54
+ title: REVIEW_NAMES[status],
55
+ },
56
+ };
57
+ }
58
+ /**
59
+ * The count on the header's Changes section: what is left against everything that changed, or only
60
+ * how many changed where marks cannot be read. Nothing when nothing changed.
61
+ */
62
+ export function changesCount(changed, review) {
63
+ if (changed === 0) {
64
+ return undefined;
65
+ }
66
+ if (review === null) {
67
+ return {
68
+ className: "segment-count",
69
+ text: String(changed),
70
+ attributes: { title: `${changed} changed ${documents(changed)}` },
71
+ };
72
+ }
73
+ return {
74
+ className: review.left === 0 ? "segment-count is-done" : "segment-count",
75
+ text: `${review.left}/${review.total}`,
76
+ attributes: {
77
+ title: `${review.left} of ${review.total} changed ${documents(review.total)} left to review`,
78
+ },
79
+ };
80
+ }
81
+ export function markElement(spec) {
82
+ const element = document.createElement("span");
83
+ element.className = spec.className;
84
+ if (spec.text !== "") {
85
+ element.textContent = spec.text;
86
+ }
87
+ for (const [name, value] of Object.entries(spec.attributes)) {
88
+ element.setAttribute(name, value);
89
+ }
90
+ return element;
91
+ }
92
+ /** Whether the server drew these marks into the page as they are now, so none need asking for. */
93
+ export function drawnByServer(marks) {
94
+ const marked = document.querySelector(".sidebar")?.dataset.marked ?? "";
95
+ return marked.split(" ").includes(marks);
96
+ }
97
+ function documents(count) {
98
+ return count === 1 ? "document" : "documents";
99
+ }
100
+ function plural(count, noun) {
101
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
102
+ }
File without changes