@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.
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/assets/favicon.ico +0 -0
- package/assets/viewer.css +4184 -0
- package/dist/asset-routes.d.ts +4 -0
- package/dist/asset-routes.js +107 -0
- package/dist/blame.d.ts +8 -0
- package/dist/blame.js +54 -0
- package/dist/branch-changes.d.ts +8 -0
- package/dist/branch-changes.js +49 -0
- package/dist/candidate-lint-worker.d.ts +1 -0
- package/dist/candidate-lint-worker.js +67 -0
- package/dist/candidate-lint.d.ts +39 -0
- package/dist/candidate-lint.js +115 -0
- package/dist/change-banner.d.ts +38 -0
- package/dist/change-banner.js +108 -0
- package/dist/change-diff.d.ts +23 -0
- package/dist/change-diff.js +140 -0
- package/dist/change-marks.d.ts +32 -0
- package/dist/change-marks.js +363 -0
- package/dist/changes-article.d.ts +15 -0
- package/dist/changes-article.js +244 -0
- package/dist/changes-routes.d.ts +12 -0
- package/dist/changes-routes.js +271 -0
- package/dist/changes.d.ts +113 -0
- package/dist/changes.js +324 -0
- package/dist/cli-args.d.ts +21 -0
- package/dist/cli-args.js +93 -0
- package/dist/client/banner-height.d.ts +1 -0
- package/dist/client/banner-height.js +33 -0
- package/dist/client/change-marks.d.ts +1 -0
- package/dist/client/change-marks.js +56 -0
- package/dist/client/changes.d.ts +1 -0
- package/dist/client/changes.js +183 -0
- package/dist/client/chart-option.d.ts +3 -0
- package/dist/client/chart-option.js +76 -0
- package/dist/client/charts.d.ts +1 -0
- package/dist/client/charts.js +54 -0
- package/dist/client/clipboard.d.ts +1 -0
- package/dist/client/clipboard.js +43 -0
- package/dist/client/code-copy.d.ts +1 -0
- package/dist/client/code-copy.js +29 -0
- package/dist/client/column-resize.d.ts +1 -0
- package/dist/client/column-resize.js +120 -0
- package/dist/client/config-dom.d.ts +13 -0
- package/dist/client/config-dom.js +47 -0
- package/dist/client/config-draft.d.ts +73 -0
- package/dist/client/config-draft.js +218 -0
- package/dist/client/config-editor.d.ts +3 -0
- package/dist/client/config-editor.js +677 -0
- package/dist/client/config-fields.d.ts +35 -0
- package/dist/client/config-fields.js +339 -0
- package/dist/client/config-json.d.ts +18 -0
- package/dist/client/config-json.js +96 -0
- package/dist/client/config-preview.d.ts +9 -0
- package/dist/client/config-preview.js +91 -0
- package/dist/client/config-rules.d.ts +14 -0
- package/dist/client/config-rules.js +202 -0
- package/dist/client/config-types.d.ts +91 -0
- package/dist/client/config-types.js +4 -0
- package/dist/client/copy-path.d.ts +1 -0
- package/dist/client/copy-path.js +32 -0
- package/dist/client/dashboard-requests.d.ts +11 -0
- package/dist/client/dashboard-requests.js +28 -0
- package/dist/client/dashboard.d.ts +1 -0
- package/dist/client/dashboard.js +119 -0
- package/dist/client/diagnostics.d.ts +1 -0
- package/dist/client/diagnostics.js +164 -0
- package/dist/client/diagrams.d.ts +1 -0
- package/dist/client/diagrams.js +81 -0
- package/dist/client/diff-expand.d.ts +1 -0
- package/dist/client/diff-expand.js +42 -0
- package/dist/client/display-settings-model.d.ts +74 -0
- package/dist/client/display-settings-model.js +229 -0
- package/dist/client/display-settings.d.ts +1 -0
- package/dist/client/display-settings.js +411 -0
- package/dist/client/echarts.d.ts +30 -0
- package/dist/client/echarts.js +14 -0
- package/dist/client/graph-layout.d.ts +88 -0
- package/dist/client/graph-layout.js +198 -0
- package/dist/client/graph.d.ts +2 -0
- package/dist/client/graph.js +566 -0
- package/dist/client/history.d.ts +21 -0
- package/dist/client/history.js +86 -0
- package/dist/client/live-reload-model.d.ts +28 -0
- package/dist/client/live-reload-model.js +52 -0
- package/dist/client/live-reload.d.ts +9 -0
- package/dist/client/live-reload.js +138 -0
- package/dist/client/menu.d.ts +14 -0
- package/dist/client/menu.js +176 -0
- package/dist/client/navigation-feedback.d.ts +1 -0
- package/dist/client/navigation-feedback.js +141 -0
- package/dist/client/open-config.d.ts +1 -0
- package/dist/client/open-config.js +48 -0
- package/dist/client/outline-spy.d.ts +1 -0
- package/dist/client/outline-spy.js +60 -0
- package/dist/client/prompts.d.ts +7 -0
- package/dist/client/prompts.js +65 -0
- package/dist/client/review.d.ts +1 -0
- package/dist/client/review.js +379 -0
- package/dist/client/search.d.ts +1 -0
- package/dist/client/search.js +199 -0
- package/dist/client/segmented.d.ts +13 -0
- package/dist/client/segmented.js +70 -0
- package/dist/client/source-lines.d.ts +16 -0
- package/dist/client/source-lines.js +47 -0
- package/dist/client/theme-toggle.d.ts +10 -0
- package/dist/client/theme-toggle.js +100 -0
- package/dist/client/theme.d.ts +0 -0
- package/dist/client/theme.js +50 -0
- package/dist/client/tree-marks.d.ts +31 -0
- package/dist/client/tree-marks.js +102 -0
- package/dist/client/tree-state.d.ts +0 -0
- package/dist/client/tree-state.js +179 -0
- package/dist/client/viewer.d.ts +1 -0
- package/dist/client/viewer.js +53 -0
- package/dist/config-editor.d.ts +166 -0
- package/dist/config-editor.js +378 -0
- package/dist/config-routes.d.ts +18 -0
- package/dist/config-routes.js +213 -0
- package/dist/context-view.d.ts +41 -0
- package/dist/context-view.js +295 -0
- package/dist/corpus-routes.d.ts +31 -0
- package/dist/corpus-routes.js +258 -0
- package/dist/corpus.d.ts +134 -0
- package/dist/corpus.js +653 -0
- package/dist/dashboard-article.d.ts +14 -0
- package/dist/dashboard-article.js +290 -0
- package/dist/dashboard.d.ts +94 -0
- package/dist/dashboard.js +468 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.js +44 -0
- package/dist/document-routes.d.ts +39 -0
- package/dist/document-routes.js +396 -0
- package/dist/event-hub.d.ts +39 -0
- package/dist/event-hub.js +120 -0
- package/dist/findings-panel.d.ts +4 -0
- package/dist/findings-panel.js +74 -0
- package/dist/folder-page.d.ts +8 -0
- package/dist/folder-page.js +74 -0
- package/dist/graph-view.d.ts +66 -0
- package/dist/graph-view.js +236 -0
- package/dist/highlight.d.ts +24 -0
- package/dist/highlight.js +100 -0
- package/dist/http-send.d.ts +9 -0
- package/dist/http-send.js +53 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +72 -0
- package/dist/open-file.d.ts +36 -0
- package/dist/open-file.js +87 -0
- package/dist/page-tree.d.ts +3 -0
- package/dist/page-tree.js +105 -0
- package/dist/page.d.ts +73 -0
- package/dist/page.js +478 -0
- package/dist/prompts.d.ts +25 -0
- package/dist/prompts.js +82 -0
- package/dist/render-cache.d.ts +23 -0
- package/dist/render-cache.js +38 -0
- package/dist/render-document.d.ts +60 -0
- package/dist/render-document.js +440 -0
- package/dist/request-path.d.ts +17 -0
- package/dist/request-path.js +51 -0
- package/dist/review-context.d.ts +24 -0
- package/dist/review-context.js +109 -0
- package/dist/review-queries.d.ts +26 -0
- package/dist/review-queries.js +69 -0
- package/dist/review-state.d.ts +106 -0
- package/dist/review-state.js +251 -0
- package/dist/review-store.d.ts +145 -0
- package/dist/review-store.js +530 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.js +294 -0
- package/dist/search.d.ts +22 -0
- package/dist/search.js +164 -0
- package/dist/security.d.ts +95 -0
- package/dist/security.js +196 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +224 -0
- package/dist/speculation-rules.d.ts +4 -0
- package/dist/speculation-rules.js +49 -0
- package/dist/state-dir.d.ts +6 -0
- package/dist/state-dir.js +31 -0
- package/dist/tree-marks.d.ts +49 -0
- package/dist/tree-marks.js +108 -0
- package/dist/viewer-context.d.ts +37 -0
- package/dist/viewer-context.js +74 -0
- package/dist/watch.d.ts +41 -0
- package/dist/watch.js +135 -0
- package/package.json +57 -4
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { h } from "hastscript";
|
|
4
|
+
import { changedSections, documentSections, headChanges, introductionChanged, } from "@wastech-mdlint/core";
|
|
5
|
+
import { blameAnnotator } from "./blame.js";
|
|
6
|
+
import { historyDetails, renderChangeBanner, } from "./change-banner.js";
|
|
7
|
+
import { renderDiff, renderHunkTemplate } from "./change-diff.js";
|
|
8
|
+
import { documentNeighbors } from "./graph-view.js";
|
|
9
|
+
import { highlight, languageForPath, withCopyButton } from "./highlight.js";
|
|
10
|
+
import { sendHtml, sendJson } from "./http-send.js";
|
|
11
|
+
import { rawHtml } from "./page.js";
|
|
12
|
+
import { resolversFor } from "./render-cache.js";
|
|
13
|
+
import { documentHref, renderDocument, } from "./render-document.js";
|
|
14
|
+
import { markOf, sectionMarksOf } from "./review-state.js";
|
|
15
|
+
import { pageHeaders, rawFileHeaders } from "./security.js";
|
|
16
|
+
import { NO_CHANGES, } from "./viewer-context.js";
|
|
17
|
+
// A document's page in every view it has — rendered, its changes marked, its diff, blame — and the
|
|
18
|
+
// files it links to, which are served beside it.
|
|
19
|
+
// Enough of a document's history to see who has been changing it lately, without paging through it.
|
|
20
|
+
const HISTORY_COMMITS = 8;
|
|
21
|
+
// A source file larger than this is a generated or vendored file, not something a document links to
|
|
22
|
+
// for a reader; highlighting it would stall the page for nothing.
|
|
23
|
+
const MAX_SOURCE_BYTES = 1024 * 1024;
|
|
24
|
+
// Repository files served as-is. Everything else a document links to is shown as highlighted source,
|
|
25
|
+
// so no repository file is ever handed to the browser as HTML or script.
|
|
26
|
+
const IMAGE_TYPES = {
|
|
27
|
+
avif: "image/avif",
|
|
28
|
+
bmp: "image/bmp",
|
|
29
|
+
gif: "image/gif",
|
|
30
|
+
ico: "image/x-icon",
|
|
31
|
+
jpeg: "image/jpeg",
|
|
32
|
+
jpg: "image/jpeg",
|
|
33
|
+
png: "image/png",
|
|
34
|
+
svg: "image/svg+xml",
|
|
35
|
+
webp: "image/webp",
|
|
36
|
+
};
|
|
37
|
+
export class DocumentRoutes {
|
|
38
|
+
ctx;
|
|
39
|
+
// A document rendered with its changes marked, kept for as long as the review it was marked from:
|
|
40
|
+
// the tracker replaces every review after the watcher's next batch, which is also what brings a new
|
|
41
|
+
// text or a change in which files the links reach, so an entry never outlives what it was made of.
|
|
42
|
+
marked = new WeakMap();
|
|
43
|
+
constructor(ctx) {
|
|
44
|
+
this.ctx = ctx;
|
|
45
|
+
}
|
|
46
|
+
// With blame, nothing is kept: blame is asked for one page at a time, and the blame of a file moves
|
|
47
|
+
// with every commit, which nothing here would notice.
|
|
48
|
+
renderMarked(document, review, annotate) {
|
|
49
|
+
const entry = this.marked.get(review);
|
|
50
|
+
if (annotate === undefined && entry?.document === document) {
|
|
51
|
+
return entry.result;
|
|
52
|
+
}
|
|
53
|
+
const changes = headChanges(review.diff);
|
|
54
|
+
const result = renderDocument(document, resolversFor(this.ctx.corpus, document), {
|
|
55
|
+
changes,
|
|
56
|
+
basePath: review.oldPath ?? review.path,
|
|
57
|
+
baseText: review.baseText,
|
|
58
|
+
}, annotate).then((rendered) => ({
|
|
59
|
+
rendered,
|
|
60
|
+
changedSections: new Set(changedSections(document.headings, changes).map((heading) => heading.slug)),
|
|
61
|
+
introductionChanged: introductionChanged(document, changes),
|
|
62
|
+
}));
|
|
63
|
+
if (annotate === undefined) {
|
|
64
|
+
this.marked.set(review, { document, result });
|
|
65
|
+
result.catch(() => this.marked.delete(review));
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
// The document rendered without its changes marked: from the shared cache, or afresh with blame,
|
|
70
|
+
// which the cache does not keep.
|
|
71
|
+
renderPlain(document, annotate) {
|
|
72
|
+
return annotate === undefined
|
|
73
|
+
? this.ctx.renders.render(document)
|
|
74
|
+
: renderDocument(document, resolversFor(this.ctx.corpus, document), undefined, annotate);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The review of a document page's own changes, or `undefined` when it has none to show. Always the
|
|
78
|
+
* work-tree review: the page renders the file on disk, and only that review's line numbers are its
|
|
79
|
+
* lines. Its head text is compared with the text being rendered, so a page requested between a
|
|
80
|
+
* save reaching the corpus and the review being rebuilt is never marked from the other text.
|
|
81
|
+
*/
|
|
82
|
+
async reviewOf(document) {
|
|
83
|
+
if (this.ctx.changes === undefined) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const review = await this.ctx.changes.review(true);
|
|
87
|
+
if ("available" in review) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
return review.documents.find((entry) => entry.path === document.path && entry.headText === document.content);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* What the line above a changed document says about the reader's review of it, and the review
|
|
94
|
+
* since their last one when the page compares with that; `undefined` where marks are off or
|
|
95
|
+
* cannot be read, which the Changes page explains.
|
|
96
|
+
*/
|
|
97
|
+
async bannerReviewOf(review, baseline) {
|
|
98
|
+
const { reviewStore } = this.ctx;
|
|
99
|
+
if (review === undefined || reviewStore === undefined) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const read = await reviewStore.read();
|
|
103
|
+
if (!read.ok) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const mark = markOf(review, read.marks);
|
|
107
|
+
const sections = sectionMarksOf(review, read.marks);
|
|
108
|
+
// A document reviewed as it is compares with its own text, which is kept under its hash
|
|
109
|
+
// whichever path it was marked under: the page then shows that nothing changed since.
|
|
110
|
+
const version = mark.status === "reviewed" ? mark.hash : mark.since;
|
|
111
|
+
const since = baseline === "review" && version !== undefined
|
|
112
|
+
? await this.ctx.sinceReviews?.of(review, version)
|
|
113
|
+
: undefined;
|
|
114
|
+
return {
|
|
115
|
+
mark,
|
|
116
|
+
baseline,
|
|
117
|
+
sections,
|
|
118
|
+
...(since === undefined ? {} : { since }),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// The margin notes of one document, or `undefined` when git cannot blame it or the file on disk is
|
|
122
|
+
// no longer the text being rendered.
|
|
123
|
+
async blameOf(repository, relPath, content) {
|
|
124
|
+
const onDisk = await readFile(path.join(this.ctx.corpus.rootDir, ...relPath.split("/")), "utf8").catch(() => undefined);
|
|
125
|
+
if (onDisk !== content) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const lines = await repository.blame(relPath).catch(() => []);
|
|
129
|
+
return lines.length === 0
|
|
130
|
+
? undefined
|
|
131
|
+
: blameAnnotator(lines, this.ctx.showAbsolutePaths);
|
|
132
|
+
}
|
|
133
|
+
async sendDocument(request, response, asked) {
|
|
134
|
+
const { corpus, git } = this.ctx;
|
|
135
|
+
const { relPath } = asked;
|
|
136
|
+
const document = relPath === undefined ? undefined : corpus.documents.get(relPath);
|
|
137
|
+
if (relPath === undefined || document === undefined) {
|
|
138
|
+
await this.ctx.sendNotFound(request, response, "This path is not a document in the corpus. The viewer shows the Markdown files the wastech-mdlint config includes.");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Blame is asked for in the address and drawn on the server, like the other views of a page. The
|
|
142
|
+
// file on disk is what git blames, so it is used only while it is the text the corpus holds; a
|
|
143
|
+
// page asked for between a save and the corpus catching up shows no blame rather than the
|
|
144
|
+
// wrong lines'.
|
|
145
|
+
const annotate = asked.blame && git !== undefined
|
|
146
|
+
? await this.blameOf(git, relPath, document.content)
|
|
147
|
+
: undefined;
|
|
148
|
+
const branchReview = await this.reviewOf(document);
|
|
149
|
+
const shown = {
|
|
150
|
+
...asked,
|
|
151
|
+
relPath,
|
|
152
|
+
document,
|
|
153
|
+
annotate,
|
|
154
|
+
branchReview,
|
|
155
|
+
reviewed: await this.bannerReviewOf(branchReview, asked.baseline),
|
|
156
|
+
};
|
|
157
|
+
const { params, introChanged } = await this.documentBody(shown);
|
|
158
|
+
sendHtml(response, 200, await this.ctx.page(this.withReviewParts(params, shown, introChanged)), request.method === "HEAD");
|
|
159
|
+
}
|
|
160
|
+
// The page parts every view of a document shares: its path, its links and the blame switch.
|
|
161
|
+
commonParams(shown) {
|
|
162
|
+
const { corpus, git } = this.ctx;
|
|
163
|
+
const { relPath } = shown;
|
|
164
|
+
return {
|
|
165
|
+
displayPath: relPath,
|
|
166
|
+
absolutePath: this.ctx.absolutePathOf(relPath),
|
|
167
|
+
documentPath: relPath,
|
|
168
|
+
backlinks: corpus.backlinks(relPath),
|
|
169
|
+
neighborCount: documentNeighbors(corpus.graph, relPath).length,
|
|
170
|
+
...(git === undefined
|
|
171
|
+
? {}
|
|
172
|
+
: {
|
|
173
|
+
blameToggle: {
|
|
174
|
+
on: shown.annotate !== undefined,
|
|
175
|
+
href: documentPageHref(relPath, shown.view, shown.baseline, !shown.blame),
|
|
176
|
+
},
|
|
177
|
+
}),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
// The page in the view asked for: the source diff in place of the text, the text with its changes
|
|
181
|
+
// marked, or the plain text.
|
|
182
|
+
async documentBody(shown) {
|
|
183
|
+
const { document, view, annotate, branchReview, reviewed } = shown;
|
|
184
|
+
// What the page marks: the branch's changes, or the changes since the reader's last review when
|
|
185
|
+
// the page compares with that and the reviewed text is still kept.
|
|
186
|
+
const review = reviewed?.since ?? branchReview;
|
|
187
|
+
const shared = {
|
|
188
|
+
...this.commonParams(shown),
|
|
189
|
+
localGraphChanges: review !== undefined && view !== "off",
|
|
190
|
+
changeBanner: branchReview === undefined
|
|
191
|
+
? undefined
|
|
192
|
+
: renderChangeBanner(branchReview, view, reviewed),
|
|
193
|
+
};
|
|
194
|
+
if (review !== undefined && view === "source") {
|
|
195
|
+
// The diff takes the article's place. The outline is left out with the rendered text, since
|
|
196
|
+
// none of its headings is on this page to jump to.
|
|
197
|
+
const { title } = await this.renderPlain(document, annotate);
|
|
198
|
+
return {
|
|
199
|
+
introChanged: false,
|
|
200
|
+
params: {
|
|
201
|
+
...shared,
|
|
202
|
+
title,
|
|
203
|
+
article: [
|
|
204
|
+
h("div.change-source", renderDiff(review.diff, review.headText)),
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (review !== undefined &&
|
|
210
|
+
view === "rendered" &&
|
|
211
|
+
// On a document the branch added, every block would carry the same mark the line above it
|
|
212
|
+
// already gives the whole page.
|
|
213
|
+
review.status !== "A" &&
|
|
214
|
+
review.diff.hunks.length > 0) {
|
|
215
|
+
const result = await this.renderMarked(document, review, annotate);
|
|
216
|
+
const { rendered } = result;
|
|
217
|
+
return {
|
|
218
|
+
introChanged: result.introductionChanged,
|
|
219
|
+
params: {
|
|
220
|
+
...shared,
|
|
221
|
+
title: rendered.title,
|
|
222
|
+
article: [...rawHtml(rendered.html), renderHunkTemplate(review.diff)],
|
|
223
|
+
headings: document.headings,
|
|
224
|
+
changedSections: result.changedSections,
|
|
225
|
+
frontmatter: document.frontmatter,
|
|
226
|
+
frontmatterChange: rendered.frontmatterChange,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const { html, title } = await this.renderPlain(document, annotate);
|
|
231
|
+
return {
|
|
232
|
+
introChanged: false,
|
|
233
|
+
params: {
|
|
234
|
+
...shared,
|
|
235
|
+
title,
|
|
236
|
+
article: rawHtml(html),
|
|
237
|
+
headings: document.headings,
|
|
238
|
+
frontmatter: document.frontmatter,
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
// The parts of a document page that follow the reader's review: the outline's introduction entry,
|
|
243
|
+
// the history line on an unchanged document, and the section marks with the token that sends them.
|
|
244
|
+
withReviewParts(params, shown, introChanged) {
|
|
245
|
+
const { branchReview, reviewed } = shown;
|
|
246
|
+
const sectionMarks = reviewed === undefined || branchReview === undefined
|
|
247
|
+
? undefined
|
|
248
|
+
: { path: branchReview.path, marks: reviewed.sections };
|
|
249
|
+
const introduction = params.headings === undefined
|
|
250
|
+
? undefined
|
|
251
|
+
: introductionEntry(shown.document, introChanged, sectionMarks !== undefined);
|
|
252
|
+
return {
|
|
253
|
+
...params,
|
|
254
|
+
...(introduction === undefined ? {} : { introduction }),
|
|
255
|
+
...(this.ctx.git === undefined
|
|
256
|
+
? {}
|
|
257
|
+
: {
|
|
258
|
+
historyLine: h("div.document-history", historyDetails(shown.relPath)),
|
|
259
|
+
}),
|
|
260
|
+
// The "Viewed" checkbox above a changed document sends its mark with it, and so does each
|
|
261
|
+
// section's in the outline.
|
|
262
|
+
...(sectionMarks === undefined
|
|
263
|
+
? {}
|
|
264
|
+
: { changeToken: this.ctx.changeToken, sectionMarks }),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// The commits that last touched a document, for the line above its text: newest first, a few at
|
|
268
|
+
// most, the author's name only on a loopback binding, as a person's name is kept to this machine.
|
|
269
|
+
async sendHistory(response, params) {
|
|
270
|
+
const { corpus, git } = this.ctx;
|
|
271
|
+
// Only ever compared against the corpus's own paths, like the graph's parameters.
|
|
272
|
+
const relPath = params.get("path") ?? "";
|
|
273
|
+
if (git === undefined || !corpus.documents.has(relPath)) {
|
|
274
|
+
sendJson(response, {
|
|
275
|
+
error: git === undefined
|
|
276
|
+
? NO_CHANGES
|
|
277
|
+
: "This path is not a document in the corpus.",
|
|
278
|
+
}, 404);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const commits = await git
|
|
282
|
+
.fileHistory(relPath, HISTORY_COMMITS)
|
|
283
|
+
.catch(() => []);
|
|
284
|
+
sendJson(response, {
|
|
285
|
+
path: relPath,
|
|
286
|
+
commits: commits.map((commit) => ({
|
|
287
|
+
commit: commit.commit,
|
|
288
|
+
short: commit.short,
|
|
289
|
+
date: commit.date,
|
|
290
|
+
subject: commit.subject,
|
|
291
|
+
...(this.ctx.showAbsolutePaths ? { author: commit.author } : {}),
|
|
292
|
+
})),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
async sendLinkedFile(request, response, relPath) {
|
|
296
|
+
const { corpus } = this.ctx;
|
|
297
|
+
const absolutePath = relPath !== undefined && corpus.isLinkedFile(relPath)
|
|
298
|
+
? await corpus.resolveFile(relPath)
|
|
299
|
+
: undefined;
|
|
300
|
+
if (relPath === undefined || absolutePath === undefined) {
|
|
301
|
+
await this.ctx.sendNotFound(request, response, "The viewer serves only the repository files a document links to, and no document links here.");
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const extension = path.extname(relPath).slice(1).toLowerCase();
|
|
305
|
+
const imageType = IMAGE_TYPES[extension];
|
|
306
|
+
if (imageType !== undefined) {
|
|
307
|
+
const bytes = await readFile(absolutePath);
|
|
308
|
+
response.writeHead(200, {
|
|
309
|
+
...rawFileHeaders(),
|
|
310
|
+
"Content-Type": imageType,
|
|
311
|
+
});
|
|
312
|
+
response.end(request.method === "HEAD" ? undefined : bytes);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
sendHtml(response, 200, await this.ctx.page({
|
|
316
|
+
title: relPath,
|
|
317
|
+
article: await sourceArticle(relPath, absolutePath),
|
|
318
|
+
displayPath: relPath,
|
|
319
|
+
absolutePath: this.ctx.absolutePathOf(relPath),
|
|
320
|
+
}), request.method === "HEAD");
|
|
321
|
+
}
|
|
322
|
+
async redirectToIndex(response) {
|
|
323
|
+
const { corpus } = this.ctx;
|
|
324
|
+
const index = ["README.md", "readme.md", "index.md"].find((name) => corpus.documents.has(name));
|
|
325
|
+
const first = index ?? corpus.documents.keys().next().value;
|
|
326
|
+
if (first === undefined) {
|
|
327
|
+
sendHtml(response, 200, await this.ctx.page({
|
|
328
|
+
title: "No documents",
|
|
329
|
+
article: [
|
|
330
|
+
h("h1", "No documents"),
|
|
331
|
+
h("p", "The configured include patterns match no Markdown files in this directory."),
|
|
332
|
+
],
|
|
333
|
+
}));
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
response
|
|
337
|
+
.writeHead(302, { ...pageHeaders(), Location: documentHref(first) })
|
|
338
|
+
.end();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// A linked file that is not an image, as highlighted source — or a line saying why it is not shown.
|
|
342
|
+
async function sourceArticle(relPath, absolutePath) {
|
|
343
|
+
const { size } = await stat(absolutePath);
|
|
344
|
+
if (size > MAX_SOURCE_BYTES) {
|
|
345
|
+
return [h("h1", relPath), h("p", "This file is too large to display.")];
|
|
346
|
+
}
|
|
347
|
+
const bytes = await readFile(absolutePath);
|
|
348
|
+
// A NUL byte in the first block is the same heuristic git uses to call a file binary.
|
|
349
|
+
if (bytes.subarray(0, 8000).includes(0)) {
|
|
350
|
+
return [
|
|
351
|
+
h("h1", relPath),
|
|
352
|
+
h("p", "This is a binary file and cannot be displayed."),
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
const code = await highlight(bytes.toString("utf8"), languageForPath(relPath), {
|
|
356
|
+
lineAnchors: true,
|
|
357
|
+
});
|
|
358
|
+
return [
|
|
359
|
+
h("h1.source-title", relPath),
|
|
360
|
+
h("div.source-view", [withCopyButton(code)]),
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
// A document page's address with its views as asked: the changes view, the comparison, and blame.
|
|
364
|
+
function documentPageHref(relPath, view, baseline, blame) {
|
|
365
|
+
const params = new URLSearchParams();
|
|
366
|
+
if (view !== "rendered") {
|
|
367
|
+
params.set("diff", view);
|
|
368
|
+
}
|
|
369
|
+
if (baseline === "review") {
|
|
370
|
+
params.set("since", "review");
|
|
371
|
+
}
|
|
372
|
+
if (blame) {
|
|
373
|
+
params.set("blame", "1");
|
|
374
|
+
}
|
|
375
|
+
const query = params.toString();
|
|
376
|
+
return query === ""
|
|
377
|
+
? documentHref(relPath)
|
|
378
|
+
: `${documentHref(relPath)}?${query}`;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The outline entry for the text before a document's first section, or `undefined` for none. That
|
|
382
|
+
* text is a unit of review like any section, so where the page marks sections it needs an entry to be
|
|
383
|
+
* marked from, and where the branch changed it the entry is where the outline says so. Elsewhere the
|
|
384
|
+
* outline stays a list of headings: an entry for every title and opening paragraph would add a line
|
|
385
|
+
* to every page and tell the reader nothing. A document with no section has no outline to add it to.
|
|
386
|
+
*/
|
|
387
|
+
function introductionEntry(document, changed, marked) {
|
|
388
|
+
const [introduction, ...sections] = documentSections(document);
|
|
389
|
+
if (introduction === undefined ||
|
|
390
|
+
sections.length === 0 ||
|
|
391
|
+
introduction.text.trim() === "" ||
|
|
392
|
+
(!changed && !marked)) {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
return { changed };
|
|
396
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
/**
|
|
3
|
+
* The open pages' event streams: what tells a page that its file changed, that the findings are in,
|
|
4
|
+
* that the review moved or that a mark was saved elsewhere. One stream per open page, written to by
|
|
5
|
+
* every part of the server that changes what a page shows.
|
|
6
|
+
*
|
|
7
|
+
* Every event is numbered, and every page is served with the id of the last one sent before it
|
|
8
|
+
* (`sequence`). The page draws its tree's marks, the Changes count and its findings from the server's
|
|
9
|
+
* state at that moment and asks for nothing more, so an event sent between serving it and its stream
|
|
10
|
+
* connecting would otherwise be lost, and the page would keep showing the state before it. The page
|
|
11
|
+
* connects with that id, and the events it missed are sent to it first; when they are no longer
|
|
12
|
+
* kept — a burst longer than `KEPT_EVENTS`, or a server that restarted meanwhile — it is told so
|
|
13
|
+
* with `missed`, and reloads.
|
|
14
|
+
*
|
|
15
|
+
* An id is this run's tag and the event's number (`3f9c01ab.12`). A number alone cannot tell a
|
|
16
|
+
* restarted server from the one that served the page: the new one counts from zero again, and once
|
|
17
|
+
* it has sent as many events as the page saw — its startup findings alone can be one — the page's
|
|
18
|
+
* number reads as up to date, or as an earlier point of the new run, whose events would then be sent
|
|
19
|
+
* as if they followed the old ones. A page that closed its stream while hidden has no lost
|
|
20
|
+
* connection to tell it the server went away, so the tag is what does.
|
|
21
|
+
*/
|
|
22
|
+
export declare class EventHub {
|
|
23
|
+
private readonly clients;
|
|
24
|
+
private readonly keepAlive;
|
|
25
|
+
private latest;
|
|
26
|
+
private readonly run;
|
|
27
|
+
private readonly kept;
|
|
28
|
+
constructor();
|
|
29
|
+
/** How many pages are listening, so work only they would ask for can be started ahead of them. */
|
|
30
|
+
get size(): number;
|
|
31
|
+
/** The id of the last event sent, which a page is served with. */
|
|
32
|
+
get sequence(): string;
|
|
33
|
+
open(request: IncomingMessage, response: ServerResponse): void;
|
|
34
|
+
broadcast(name: string, data: unknown): void;
|
|
35
|
+
close(): void;
|
|
36
|
+
private id;
|
|
37
|
+
private missed;
|
|
38
|
+
private since;
|
|
39
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { assetHeaders } from "./security.js";
|
|
3
|
+
// How many of the latest events are kept to send again to a page that connects late. A page connects
|
|
4
|
+
// a few hundred milliseconds after it was served, and in that time a save sends three events at most
|
|
5
|
+
// (`change`, `diagnostics`, `git`); the room beyond that covers a burst of saves.
|
|
6
|
+
const KEPT_EVENTS = 64;
|
|
7
|
+
/**
|
|
8
|
+
* The open pages' event streams: what tells a page that its file changed, that the findings are in,
|
|
9
|
+
* that the review moved or that a mark was saved elsewhere. One stream per open page, written to by
|
|
10
|
+
* every part of the server that changes what a page shows.
|
|
11
|
+
*
|
|
12
|
+
* Every event is numbered, and every page is served with the id of the last one sent before it
|
|
13
|
+
* (`sequence`). The page draws its tree's marks, the Changes count and its findings from the server's
|
|
14
|
+
* state at that moment and asks for nothing more, so an event sent between serving it and its stream
|
|
15
|
+
* connecting would otherwise be lost, and the page would keep showing the state before it. The page
|
|
16
|
+
* connects with that id, and the events it missed are sent to it first; when they are no longer
|
|
17
|
+
* kept — a burst longer than `KEPT_EVENTS`, or a server that restarted meanwhile — it is told so
|
|
18
|
+
* with `missed`, and reloads.
|
|
19
|
+
*
|
|
20
|
+
* An id is this run's tag and the event's number (`3f9c01ab.12`). A number alone cannot tell a
|
|
21
|
+
* restarted server from the one that served the page: the new one counts from zero again, and once
|
|
22
|
+
* it has sent as many events as the page saw — its startup findings alone can be one — the page's
|
|
23
|
+
* number reads as up to date, or as an earlier point of the new run, whose events would then be sent
|
|
24
|
+
* as if they followed the old ones. A page that closed its stream while hidden has no lost
|
|
25
|
+
* connection to tell it the server went away, so the tag is what does.
|
|
26
|
+
*/
|
|
27
|
+
export class EventHub {
|
|
28
|
+
clients = new Set();
|
|
29
|
+
keepAlive;
|
|
30
|
+
latest = 0;
|
|
31
|
+
// Random rather than a start time, so two runs started within one clock tick still differ.
|
|
32
|
+
run = randomBytes(4).toString("hex");
|
|
33
|
+
kept = [];
|
|
34
|
+
constructor() {
|
|
35
|
+
// Idle connections are closed by some proxies and by browsers after a while; a comment line every
|
|
36
|
+
// so often keeps the stream open without the client seeing an event.
|
|
37
|
+
this.keepAlive = setInterval(() => {
|
|
38
|
+
for (const client of this.clients) {
|
|
39
|
+
client.write(": keep-alive\n\n");
|
|
40
|
+
}
|
|
41
|
+
}, 25_000);
|
|
42
|
+
this.keepAlive.unref();
|
|
43
|
+
}
|
|
44
|
+
/** How many pages are listening, so work only they would ask for can be started ahead of them. */
|
|
45
|
+
get size() {
|
|
46
|
+
return this.clients.size;
|
|
47
|
+
}
|
|
48
|
+
/** The id of the last event sent, which a page is served with. */
|
|
49
|
+
get sequence() {
|
|
50
|
+
return this.id(this.latest);
|
|
51
|
+
}
|
|
52
|
+
open(request, response) {
|
|
53
|
+
response.writeHead(200, {
|
|
54
|
+
...assetHeaders(),
|
|
55
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
56
|
+
"Cache-Control": "no-store",
|
|
57
|
+
Connection: "keep-alive",
|
|
58
|
+
});
|
|
59
|
+
// Reconnect quickly after a restart, so the page notices the new server within a second.
|
|
60
|
+
response.write("retry: 1000\n\n");
|
|
61
|
+
const seen = seenBy(request);
|
|
62
|
+
if (seen !== undefined && seen.run !== this.run) {
|
|
63
|
+
response.write(this.missed());
|
|
64
|
+
}
|
|
65
|
+
else if (seen !== undefined && seen.number !== this.latest) {
|
|
66
|
+
response.write(this.since(seen.number));
|
|
67
|
+
}
|
|
68
|
+
this.clients.add(response);
|
|
69
|
+
request.on("close", () => this.clients.delete(response));
|
|
70
|
+
}
|
|
71
|
+
broadcast(name, data) {
|
|
72
|
+
this.latest += 1;
|
|
73
|
+
// The id is what a browser sends back as `Last-Event-ID` when it reconnects on its own.
|
|
74
|
+
const message = `id: ${this.id(this.latest)}\nevent: ${name}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
75
|
+
this.kept.push({ id: this.latest, message });
|
|
76
|
+
if (this.kept.length > KEPT_EVENTS) {
|
|
77
|
+
this.kept.shift();
|
|
78
|
+
}
|
|
79
|
+
for (const client of this.clients) {
|
|
80
|
+
client.write(message);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
close() {
|
|
84
|
+
clearInterval(this.keepAlive);
|
|
85
|
+
for (const client of this.clients) {
|
|
86
|
+
client.end();
|
|
87
|
+
}
|
|
88
|
+
this.clients.clear();
|
|
89
|
+
}
|
|
90
|
+
id(number) {
|
|
91
|
+
return `${this.run}.${number}`;
|
|
92
|
+
}
|
|
93
|
+
missed() {
|
|
94
|
+
return `id: ${this.id(this.latest)}\nevent: missed\ndata: {}\n\n`;
|
|
95
|
+
}
|
|
96
|
+
// The events of this run after `seen`, or `missed` when they are not all kept.
|
|
97
|
+
since(seen) {
|
|
98
|
+
const first = this.kept[0]?.id ?? this.latest + 1;
|
|
99
|
+
if (seen > this.latest || seen + 1 < first) {
|
|
100
|
+
return this.missed();
|
|
101
|
+
}
|
|
102
|
+
return this.kept
|
|
103
|
+
.filter((event) => event.id > seen)
|
|
104
|
+
.map((event) => event.message)
|
|
105
|
+
.join("");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// The last event a connecting page has seen: the id it was served with, or the id of the last event
|
|
109
|
+
// it received when it reopens its stream or the browser reconnects it by itself. Anything else —
|
|
110
|
+
// malformed, or a bare number no run of this server sends — is no position to resume from.
|
|
111
|
+
function seenBy(request) {
|
|
112
|
+
const header = request.headers["last-event-id"];
|
|
113
|
+
const query = new URL(request.url ?? "/", "http://localhost").searchParams.get("seen");
|
|
114
|
+
const value = typeof header === "string" ? header : query;
|
|
115
|
+
const match = value === null ? null : /^([0-9a-f]{8})\.(\d{1,15})$/.exec(value);
|
|
116
|
+
if (match === null) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
return { run: match[1], number: Number(match[2]) };
|
|
120
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Element } from "hast";
|
|
2
|
+
import type { Finding } from "./diagnostics.js";
|
|
3
|
+
/** The panel for `relPath`'s findings, or nothing when it has none. */
|
|
4
|
+
export declare function renderFindingsPanel(relPath: string, findings: readonly Finding[]): Element | undefined;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { h } from "hastscript";
|
|
2
|
+
import { CONTENT_ID } from "./page.js";
|
|
3
|
+
// A document's findings, in a panel above its text: severity, rule, message, where on the page it is,
|
|
4
|
+
// and a prompt to fix it. Drawn into the page, so a page with findings opens with them in place
|
|
5
|
+
// rather than pushing its text down a moment after it is drawn; after a save the page script asks for
|
|
6
|
+
// the panel again (`/api/diagnostics/panel`) and puts this same markup in its place.
|
|
7
|
+
/** The panel for `relPath`'s findings, or nothing when it has none. */
|
|
8
|
+
export function renderFindingsPanel(relPath, findings) {
|
|
9
|
+
if (findings.length === 0) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const errors = findings.filter((finding) => finding.severity === "error").length;
|
|
13
|
+
const warnings = findings.length - errors;
|
|
14
|
+
const count = [
|
|
15
|
+
errors > 0 ? plural(errors, "error") : "",
|
|
16
|
+
warnings > 0 ? plural(warnings, "warning") : "",
|
|
17
|
+
]
|
|
18
|
+
.filter((part) => part !== "")
|
|
19
|
+
.join(", ");
|
|
20
|
+
return h(`details.diagnostics${errors > 0 ? ".has-errors" : ""}`, { open: true }, [
|
|
21
|
+
h("summary", [
|
|
22
|
+
// The count in a span of its own, which takes the row's free width and so puts the prompt
|
|
23
|
+
// button for all findings at the row's right end, level with the per-finding buttons under it.
|
|
24
|
+
h("span.diagnostics-title", `wastech-mdlint: ${count}`),
|
|
25
|
+
findings.length > 1 ? promptButton(relPath) : undefined,
|
|
26
|
+
]),
|
|
27
|
+
h("ul", findings.map((finding) => findingRow(relPath, finding))),
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
30
|
+
function findingRow(relPath, finding) {
|
|
31
|
+
return h(`li.diagnostic.diagnostic-${finding.severity}`, [
|
|
32
|
+
h("span.diagnostic-severity", finding.severity),
|
|
33
|
+
// Linked to the rule's description only when that is a web address: a custom rule's help link
|
|
34
|
+
// comes from the config, and anything else would be a link the page should not offer.
|
|
35
|
+
finding.helpUri !== undefined && /^https?:\/\//i.test(finding.helpUri)
|
|
36
|
+
? h("a.diagnostic-rule", {
|
|
37
|
+
href: finding.helpUri,
|
|
38
|
+
target: "_blank",
|
|
39
|
+
rel: ["noopener", "noreferrer"],
|
|
40
|
+
}, finding.ruleId)
|
|
41
|
+
: h("span.diagnostic-rule", finding.ruleId),
|
|
42
|
+
h("span.diagnostic-message", finding.message),
|
|
43
|
+
// Findings carry a source line, not an element; the nearest heading above that line is the
|
|
44
|
+
// closest place on the rendered page to send the reader, and above the first heading that is the
|
|
45
|
+
// top of the main column. Line 0 is how a rule reports a finding about the file as a whole, such
|
|
46
|
+
// as its size.
|
|
47
|
+
h("a.diagnostic-line", {
|
|
48
|
+
href: `#${finding.anchor ?? CONTENT_ID}`,
|
|
49
|
+
}, finding.line > 0 ? `line ${finding.line}` : "whole file"),
|
|
50
|
+
promptButton(relPath, finding),
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The button that copies a prompt for the document's findings: all of them, or one finding's, by its
|
|
55
|
+
* rule and line. Its menu is the page script's, so it is hidden without it.
|
|
56
|
+
*/
|
|
57
|
+
function promptButton(relPath, finding) {
|
|
58
|
+
return h("button.prompt-menu", {
|
|
59
|
+
type: "button",
|
|
60
|
+
hidden: true,
|
|
61
|
+
title: finding === undefined
|
|
62
|
+
? "Copy a prompt that answers every finding in this document"
|
|
63
|
+
: "Copy a prompt that answers this finding",
|
|
64
|
+
dataPath: relPath,
|
|
65
|
+
dataRule: finding?.ruleId,
|
|
66
|
+
// Not `data-line`: inside the article that attribute is a block's source line, and the
|
|
67
|
+
// stylesheet draws it in the right margin of whatever element carries it — a finding's line
|
|
68
|
+
// there showed up as a stray number beside the button.
|
|
69
|
+
dataFindingLine: finding === undefined ? undefined : String(finding.line),
|
|
70
|
+
}, finding === undefined ? "Prompt for all" : "Prompt");
|
|
71
|
+
}
|
|
72
|
+
function plural(count, noun) {
|
|
73
|
+
return `${count} ${noun}${count === 1 ? "" : "s"}`;
|
|
74
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ElementContent } from "hast";
|
|
2
|
+
import { type ParsedDocument } from "@wastech-mdlint/core";
|
|
3
|
+
/**
|
|
4
|
+
* The listing of `folder`: its subfolders that hold documents, then the documents directly in it.
|
|
5
|
+
* The caller checks that `folder` is one of the corpus's folders; an unknown name is a 404, not an
|
|
6
|
+
* empty listing.
|
|
7
|
+
*/
|
|
8
|
+
export declare function renderFolderArticle(folder: string, documents: ReadonlyMap<string, ParsedDocument>): ElementContent[];
|