@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,324 @@
1
+ import { branchContext, openGitRepository, reviewBranch, } from "@wastech-mdlint/core";
2
+ import { contextPayload, documentContextPayload, findingsPayload, } from "./context-view.js";
3
+ /**
4
+ * Keeps the review between requests, for as long as the viewer runs.
5
+ *
6
+ * A review is stale when the work tree changed — which the corpus watcher reports, and the server
7
+ * passes on through `invalidate` — or when `HEAD` or the base moved without the work tree changing,
8
+ * as a commit, a `reset --soft` or a fetch does. Where the repository is watched, the watcher on
9
+ * git's `HEAD` and refs reports the second too, through `gitMoved`, and a kept review is answered
10
+ * without asking git: that one short process was most of the time a document page took to answer,
11
+ * and every navigation paid it although nothing had moved. Without a watcher nothing would report
12
+ * it, so git is asked on every request instead.
13
+ *
14
+ * Requests that arrive while a review is being computed wait for that one rather than starting
15
+ * their own: after a save, the reloading document page, the open pages' scripts and every other tab
16
+ * all ask at once, and each would otherwise run the whole review again.
17
+ */
18
+ export class ChangeTracker {
19
+ corpus;
20
+ repository;
21
+ options;
22
+ cache = new Map();
23
+ pending = new Map();
24
+ // Bumped by `invalidate`, so a review that was already running when the work tree changed is
25
+ // neither cached nor joined: it describes the files as they were before.
26
+ generation = 0;
27
+ contexts = new WeakMap();
28
+ warming = Promise.resolve();
29
+ // Whether git's `HEAD` and refs are watched now: from the start, until a watch is lost.
30
+ watched;
31
+ constructor(corpus, repository, options) {
32
+ this.corpus = corpus;
33
+ this.repository = repository;
34
+ this.options = options;
35
+ this.watched = options.watched;
36
+ }
37
+ /**
38
+ * A tracker for the repository holding the corpus, or `undefined` outside any git work tree or
39
+ * without git — where there is no branch to review and the viewer shows no Changes section. A
40
+ * repository git refuses to read (owned by another user, or a git too old) still gets a tracker, so
41
+ * the section can say why it is empty.
42
+ */
43
+ static async open(corpus, options) {
44
+ const repository = await openGitRepository(corpus.rootDir, {
45
+ env: options.env,
46
+ });
47
+ if (!repository.available &&
48
+ (repository.reason === "not-a-repository" ||
49
+ repository.reason === "no-git-binary")) {
50
+ return undefined;
51
+ }
52
+ return new ChangeTracker(corpus, repository, {
53
+ ...(options.base === undefined ? {} : { base: options.base }),
54
+ watched: options.watched === true,
55
+ });
56
+ }
57
+ /** The repository, when git can read it: where review marks are kept is decided from it. */
58
+ get git() {
59
+ return this.repository.available ? this.repository : undefined;
60
+ }
61
+ /** The review kept for a mode, without asking git whether it is still current. */
62
+ cached(worktree) {
63
+ return this.cache.get(worktree)?.result;
64
+ }
65
+ /**
66
+ * Compute the work-tree review and what it does to the context in the background, ahead of the
67
+ * first page that needs them. The context — the base linted beside the head — is what the Changes
68
+ * page, the dashboard's Branch column and a changed document's small graph wait for, a second or so
69
+ * on a corpus of six hundred documents. Every page used to ask for the changes as it opened, which
70
+ * computed it on the way; pages now draw the branch's marks from the review alone, so without this
71
+ * the first of those pages waited for all of it.
72
+ */
73
+ warm() {
74
+ const run = this.review(true)
75
+ .then((review) => "available" in review ? undefined : this.context(review))
76
+ .then(() => undefined, () => undefined);
77
+ this.warming = Promise.all([this.warming, run]).then(() => undefined);
78
+ }
79
+ /**
80
+ * Resolves once every warm-up started so far has finished, so the viewer can close without git
81
+ * still running in the repository: on Windows a directory a process is working in cannot be
82
+ * removed, and a test's temporary repository could not be cleaned up.
83
+ */
84
+ idle() {
85
+ return this.warming;
86
+ }
87
+ /**
88
+ * Called when the watcher on git's `HEAD` and refs saw something move. Each kept review is checked
89
+ * against git and dropped when it no longer matches, and the work-tree one is computed again where
90
+ * it was; true when that one is now a different review, which the open pages are then told. A ref
91
+ * of some other branch moving changes nothing they show, and answers false.
92
+ */
93
+ async gitMoved() {
94
+ const before = this.cached(true);
95
+ const repository = this.repository;
96
+ if (repository.available) {
97
+ for (const [worktree, kept] of [...this.cache]) {
98
+ const key = await repository.revisionKey(kept.result.set.base);
99
+ if (key !== kept.key && this.cache.get(worktree) === kept) {
100
+ this.cache.delete(worktree);
101
+ }
102
+ }
103
+ }
104
+ const after = await this.review(true).catch(() => undefined);
105
+ return after !== undefined && after !== before;
106
+ }
107
+ /**
108
+ * A watch on git's `HEAD` or refs ended or never started, so a move there would go unreported:
109
+ * from now on a kept review is checked against git on every request again.
110
+ */
111
+ watchLost() {
112
+ this.watched = false;
113
+ }
114
+ /** Forget every review; the next request computes a fresh one. */
115
+ invalidate() {
116
+ this.generation += 1;
117
+ this.cache.clear();
118
+ this.pending.clear();
119
+ }
120
+ async review(worktree) {
121
+ const repository = this.repository;
122
+ if (!repository.available) {
123
+ return repository;
124
+ }
125
+ const cached = this.cache.get(worktree);
126
+ if (cached !== undefined &&
127
+ (this.watched ||
128
+ (await repository.revisionKey(cached.result.set.base)) === cached.key)) {
129
+ return cached.result;
130
+ }
131
+ const running = this.pending.get(worktree);
132
+ if (running !== undefined) {
133
+ return running;
134
+ }
135
+ const generation = this.generation;
136
+ const computing = this.compute(repository, worktree)
137
+ .then((result) => {
138
+ // A problem such as a missing base is not kept, and is asked again every time: a fetch or
139
+ // a new branch may have solved it, and nothing on disk the watcher sees would say so.
140
+ if (!("available" in result) && generation === this.generation) {
141
+ this.cache.set(worktree, {
142
+ key: `${result.set.head}:${result.set.base.commit}`,
143
+ result,
144
+ });
145
+ }
146
+ return result;
147
+ })
148
+ .finally(() => {
149
+ if (this.pending.get(worktree) === computing) {
150
+ this.pending.delete(worktree);
151
+ }
152
+ });
153
+ this.pending.set(worktree, computing);
154
+ return computing;
155
+ }
156
+ /**
157
+ * What `review`'s change does to the context, computed on first request and kept with the review,
158
+ * so it lasts exactly as long as the review does. Apart from the review because only the Changes
159
+ * page and its JSON need it: a document's page, which waits for the review, never waits for a lint
160
+ * of two corpora as well. `undefined` when the work-tree review it is built on cannot be made.
161
+ */
162
+ context(review) {
163
+ let pending = this.contexts.get(review);
164
+ if (pending === undefined) {
165
+ pending = this.computeContext(review);
166
+ this.contexts.set(review, pending);
167
+ // A failure is not kept, so the next request tries again rather than showing nothing forever.
168
+ pending.catch(() => this.contexts.delete(review));
169
+ }
170
+ return pending;
171
+ }
172
+ async computeContext(review) {
173
+ const repository = this.repository;
174
+ const worktree = review.set.worktree ? review : await this.review(true);
175
+ if (!repository.available ||
176
+ "available" in worktree ||
177
+ worktree.set.mergeBase !== review.set.mergeBase) {
178
+ // The two reviews disagree about the merge-base only while one of them is being replaced;
179
+ // the next request finds both new.
180
+ this.contexts.delete(review);
181
+ return undefined;
182
+ }
183
+ return branchContext({
184
+ repository,
185
+ rootDir: this.corpus.rootDir,
186
+ review,
187
+ worktree,
188
+ disk: worktree.snapshot,
189
+ ordering: review.snapshot,
190
+ });
191
+ }
192
+ async compute(repository, worktree) {
193
+ // One snapshot of the corpus for the whole computation, so the documents, the graph and the
194
+ // config all describe the same moment even if an update lands meanwhile.
195
+ const corpus = this.corpus;
196
+ const snapshot = {
197
+ documents: corpus.documents,
198
+ graph: corpus.graph,
199
+ configuration: corpus.configuration,
200
+ membership: corpus.membership(),
201
+ };
202
+ const review = await reviewBranch(repository, {
203
+ ...(this.options.base === undefined ? {} : { base: this.options.base }),
204
+ worktree,
205
+ corpus: snapshot,
206
+ });
207
+ return "available" in review ? review : { ...review, snapshot };
208
+ }
209
+ }
210
+ /**
211
+ * `/api/changes`: what the page lists, as data. Diffs are left to `/api/changes/diff`. With the
212
+ * review's context, every document carries its findings, size and links, and the list is in the
213
+ * page's order; without it — it could not be computed — those are `null`. With review marks, every
214
+ * document carries its status and the hash a mark must name, and `review` counts what is left; both
215
+ * are `null` where marks are off, and `review` says why when they could not be read. No time from the
216
+ * marks appears here, so the payload depends only on the files and the marks.
217
+ */
218
+ export function changesPayload(result, context, marks) {
219
+ if ("available" in result) {
220
+ return {
221
+ status: "unavailable",
222
+ reason: result.reason,
223
+ message: result.message,
224
+ hint: result.hint,
225
+ };
226
+ }
227
+ const { set } = result;
228
+ return {
229
+ status: "ready",
230
+ base: set.base,
231
+ mergeBase: set.mergeBase,
232
+ head: set.head,
233
+ worktree: set.worktree,
234
+ added: totalOf(result, "added"),
235
+ removed: totalOf(result, "removed"),
236
+ config: result.config === undefined
237
+ ? null
238
+ : {
239
+ path: result.config.path,
240
+ status: result.config.status,
241
+ added: result.config.diff.added,
242
+ removed: result.config.diff.removed,
243
+ findings: context?.delta.configOnly === undefined
244
+ ? null
245
+ : findingsPayload(context.delta.configOnly),
246
+ },
247
+ documents: ordered(result, context).map((review) => {
248
+ const delta = documentContext(context, review.path);
249
+ const mark = marks?.ok === true ? marks.documents.get(review.path) : undefined;
250
+ return {
251
+ path: review.path,
252
+ ...(review.oldPath === undefined ? {} : { oldPath: review.oldPath }),
253
+ status: review.status,
254
+ eager: review.eager,
255
+ reach: review.reach,
256
+ added: review.diff.added,
257
+ removed: review.diff.removed,
258
+ // The id of its card on the Changes page, so a page script links to it without deriving
259
+ // the id a second way.
260
+ anchor: changeCardId(review.path),
261
+ context: delta === undefined ? null : documentContextPayload(delta),
262
+ review: mark === undefined
263
+ ? null
264
+ : { status: mark.status, hash: mark.hash, sections: mark.sections },
265
+ };
266
+ }),
267
+ outsideCorpus: set.outsideCorpus,
268
+ context: context === undefined ? null : contextPayload(context),
269
+ review: reviewPayload(result, marks),
270
+ };
271
+ }
272
+ function reviewPayload(review, marks) {
273
+ if (marks === undefined) {
274
+ return null;
275
+ }
276
+ return marks.ok
277
+ ? { state: "ready", left: marks.left, total: review.documents.length }
278
+ : { state: "unavailable", message: marks.message };
279
+ }
280
+ // The review's documents in the order to read them: with its context, the order that also moves
281
+ // documents given new errors up; without it, the review's own.
282
+ export function ordered(review, context) {
283
+ if (context === undefined) {
284
+ return review.documents;
285
+ }
286
+ const byPath = new Map(review.documents.map((entry) => [entry.path, entry]));
287
+ return context.order.flatMap((relPath) => {
288
+ const entry = byPath.get(relPath);
289
+ return entry === undefined ? [] : [entry];
290
+ });
291
+ }
292
+ export function documentContext(context, relPath) {
293
+ return context?.delta.documents.find((entry) => entry.path === relPath);
294
+ }
295
+ /**
296
+ * The diff of one path in the review — a document or the config — or `undefined` when the review
297
+ * holds no such path. The path is only ever compared with the review's own, never read from disk.
298
+ */
299
+ export function diffPayload(review, relPath) {
300
+ const document = review.documents.find((entry) => entry.path === relPath);
301
+ if (document !== undefined) {
302
+ return {
303
+ path: document.path,
304
+ ...(document.oldPath === undefined ? {} : { oldPath: document.oldPath }),
305
+ status: document.status,
306
+ diff: document.diff,
307
+ };
308
+ }
309
+ if (review.config?.path === relPath) {
310
+ return {
311
+ path: review.config.path,
312
+ status: review.config.status,
313
+ diff: review.config.diff,
314
+ };
315
+ }
316
+ return undefined;
317
+ }
318
+ export function totalOf(review, side) {
319
+ return review.documents.reduce((sum, document) => sum + document.diff[side], 0);
320
+ }
321
+ /** The id of a path's card on the Changes page; it holds a `.`, as every id the viewer makes does. */
322
+ export function changeCardId(relPath) {
323
+ return `change.${encodeURIComponent(relPath)}`;
324
+ }
@@ -0,0 +1,21 @@
1
+ export declare const DEFAULT_PORT = 4180;
2
+ export declare const USAGE = "Usage: wastech-mdlint-viewer [path] [options]\n\nServe the repository's Markdown as web pages on this machine.\n\nArguments:\n path Repository directory (default: the current directory)\n\nOptions:\n -p, --port <n> Port to listen on (default: 4180, or the next free one)\n --host <host> Address to bind (default: 127.0.0.1, this machine only)\n -c, --config <f> wastech-mdlint config file (default: found in the repository)\n --base <ref> Branch, tag or commit the Changes section compares with\n (default: the branch origin/HEAD names, then main, then master)\n --no-watch Do not reload pages when files change\n --allow-config-writes\n Let the config page save changes to the config file\n --state-dir <dir>\n Where to keep review marks (default: the user's state\n directory, under wastech-mdlint/viewer)\n -h, --help Show this help\n";
3
+ export type CliArgs = {
4
+ kind: "help";
5
+ } | {
6
+ kind: "error";
7
+ message: string;
8
+ } | {
9
+ kind: "run";
10
+ cwd: string;
11
+ configPath?: string;
12
+ base?: string;
13
+ host: string;
14
+ port: number;
15
+ portExplicit: boolean;
16
+ watch: boolean;
17
+ allowConfigWrites: boolean;
18
+ stateDir?: string;
19
+ };
20
+ /** Parse the command line into what to do. Pure, so every outcome is testable without a server. */
21
+ export declare function parseCliArgs(argv: readonly string[]): CliArgs;
@@ -0,0 +1,93 @@
1
+ import { parseArgs } from "node:util";
2
+ import { DEFAULT_HOST, isLoopbackHost } from "./security.js";
3
+ export const DEFAULT_PORT = 4180;
4
+ export const USAGE = `Usage: wastech-mdlint-viewer [path] [options]
5
+
6
+ Serve the repository's Markdown as web pages on this machine.
7
+
8
+ Arguments:
9
+ path Repository directory (default: the current directory)
10
+
11
+ Options:
12
+ -p, --port <n> Port to listen on (default: ${DEFAULT_PORT}, or the next free one)
13
+ --host <host> Address to bind (default: ${DEFAULT_HOST}, this machine only)
14
+ -c, --config <f> wastech-mdlint config file (default: found in the repository)
15
+ --base <ref> Branch, tag or commit the Changes section compares with
16
+ (default: the branch origin/HEAD names, then main, then master)
17
+ --no-watch Do not reload pages when files change
18
+ --allow-config-writes
19
+ Let the config page save changes to the config file
20
+ --state-dir <dir>
21
+ Where to keep review marks (default: the user's state
22
+ directory, under wastech-mdlint/viewer)
23
+ -h, --help Show this help
24
+ `;
25
+ /** Parse the command line into what to do. Pure, so every outcome is testable without a server. */
26
+ export function parseCliArgs(argv) {
27
+ let parsed;
28
+ try {
29
+ parsed = parse(argv);
30
+ }
31
+ catch (error) {
32
+ return { kind: "error", message: error.message };
33
+ }
34
+ const { values, positionals } = parsed;
35
+ if (values.help === true) {
36
+ return { kind: "help" };
37
+ }
38
+ if (positionals.length > 1) {
39
+ return {
40
+ kind: "error",
41
+ message: `Expected one path, got ${positionals.length}.`,
42
+ };
43
+ }
44
+ let port = DEFAULT_PORT;
45
+ if (values.port !== undefined) {
46
+ // `Number("")` is 0 and `Number(" 8 ")` is 8, so the text is checked as digits before converting.
47
+ port = /^\d+$/.test(values.port) ? Number(values.port) : Number.NaN;
48
+ if (!Number.isInteger(port) || port < 0 || port > 65535) {
49
+ return { kind: "error", message: `Invalid port: ${values.port}` };
50
+ }
51
+ }
52
+ const host = values.host ?? DEFAULT_HOST;
53
+ const allowConfigWrites = values["allow-config-writes"] === true;
54
+ // Anyone who can open the config page can save from it. On this machine's loopback address that
55
+ // is the user; bound to a network address it would be anyone who can reach the port.
56
+ if (allowConfigWrites && !isLoopbackHost(host)) {
57
+ return {
58
+ kind: "error",
59
+ message: `--allow-config-writes works only on a loopback address, not with --host ${host}: anyone who can reach the viewer could rewrite the config.`,
60
+ };
61
+ }
62
+ return {
63
+ kind: "run",
64
+ cwd: positionals[0] ?? ".",
65
+ configPath: values.config,
66
+ base: values.base,
67
+ host,
68
+ port,
69
+ portExplicit: values.port !== undefined,
70
+ watch: values["no-watch"] !== true,
71
+ allowConfigWrites,
72
+ ...(values["state-dir"] === undefined
73
+ ? {}
74
+ : { stateDir: values["state-dir"] }),
75
+ };
76
+ }
77
+ function parse(argv) {
78
+ return parseArgs({
79
+ args: [...argv],
80
+ allowPositionals: true,
81
+ strict: true,
82
+ options: {
83
+ port: { type: "string", short: "p" },
84
+ host: { type: "string" },
85
+ config: { type: "string", short: "c" },
86
+ base: { type: "string" },
87
+ "no-watch": { type: "boolean" },
88
+ "allow-config-writes": { type: "boolean" },
89
+ "state-dir": { type: "string" },
90
+ help: { type: "boolean", short: "h" },
91
+ },
92
+ });
93
+ }
@@ -0,0 +1 @@
1
+ export declare function initBannerHeight(): void;
@@ -0,0 +1,33 @@
1
+ // The line above a changed document stays pinned under the header while the text scrolls, so a jump
2
+ // to a heading has to land below both — and so does the outline's idea of which section is being
3
+ // read, which it takes from the same scroll padding. The stylesheet assumes the line stands on one
4
+ // row, its height at any width wide enough to pin it; at a narrower one its controls wrap onto a
5
+ // second row and a heading would land under it. This keeps the stylesheet's figure to the line's
6
+ // real height as it changes: wrapping as the window narrows, a baseline switch appearing once a
7
+ // review is marked. Without script the one-row figure still holds wherever the line fits on a row.
8
+ export function initBannerHeight() {
9
+ const banner = document.querySelector(".markdown-body > .changes-banner");
10
+ if (banner === null) {
11
+ return;
12
+ }
13
+ const root = document.documentElement;
14
+ let first = true;
15
+ new ResizeObserver(() => {
16
+ root.style.setProperty("--banner-height", `${banner.offsetHeight}px`);
17
+ if (first) {
18
+ first = false;
19
+ // A page opened at a heading was scrolled to it before this script ran, by the one-row
20
+ // figure; on a wrapped line that left the heading under it, so it is scrolled to again.
21
+ const target = targetOfHash();
22
+ if (target !== null &&
23
+ target.getBoundingClientRect().top <
24
+ banner.getBoundingClientRect().bottom) {
25
+ target.scrollIntoView();
26
+ }
27
+ }
28
+ }).observe(banner);
29
+ }
30
+ function targetOfHash() {
31
+ const id = decodeURIComponent(location.hash.slice(1));
32
+ return id === "" ? null : document.getElementById(id);
33
+ }
@@ -0,0 +1 @@
1
+ export declare function initChangeMarks(): void;
@@ -0,0 +1,56 @@
1
+ // A changed block's bar, made a button: a click shows the block's hunks from the source diff right
2
+ // below it, and a second click hides them again. The marks themselves are on the page from the server;
3
+ // so are the hunks, as one inert template rendered by the code that renders the Changes page, so the
4
+ // diff shown here is that diff and never one drawn a second way.
5
+ export function initChangeMarks() {
6
+ const template = document.querySelector("template.change-hunks");
7
+ if (template === null) {
8
+ return;
9
+ }
10
+ let opened = 0;
11
+ for (const block of document.querySelectorAll(".markdown-body [data-change-hunks]")) {
12
+ // A closed <details> renders nothing but its summary, so a button inside it would be invisible
13
+ // until it opened; the bar there stays a mark only, and the Source view shows its change.
14
+ if (block.tagName === "DETAILS") {
15
+ continue;
16
+ }
17
+ const hunks = (block.dataset.changeHunks ?? "").split(" ").filter(Boolean);
18
+ const button = document.createElement("button");
19
+ button.type = "button";
20
+ button.className = "change-bar";
21
+ const what = block.dataset.change === "added" ? "added" : "changed";
22
+ button.title = `Show what ${what} here`;
23
+ button.setAttribute("aria-label", `Show what ${what} here`);
24
+ button.setAttribute("aria-expanded", "false");
25
+ let shown;
26
+ button.addEventListener("click", () => {
27
+ if (shown !== undefined) {
28
+ shown.remove();
29
+ shown = undefined;
30
+ button.setAttribute("aria-expanded", "false");
31
+ return;
32
+ }
33
+ shown = document.createElement("div");
34
+ shown.className = "change-hunk-view";
35
+ // Contains a `.`, as every id the viewer makes does, so no heading slug can be the same id.
36
+ shown.id = `viewer.hunk-view.${(opened += 1)}`;
37
+ for (const index of hunks) {
38
+ const hunk = template.content.querySelector(`.change-hunk[data-hunk="${index}"]`);
39
+ if (hunk !== null) {
40
+ shown.append(hunk.cloneNode(true));
41
+ }
42
+ }
43
+ button.setAttribute("aria-controls", shown.id);
44
+ button.setAttribute("aria-expanded", "true");
45
+ // A list item holds only flow content, so its hunks go inside it, after its text; any other
46
+ // block is followed by them.
47
+ if (block.tagName === "LI") {
48
+ block.append(shown);
49
+ }
50
+ else {
51
+ block.after(shown);
52
+ }
53
+ });
54
+ block.prepend(button);
55
+ }
56
+ }
@@ -0,0 +1 @@
1
+ export declare function initChanges(): void;