@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,378 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { h } from "hastscript";
|
|
5
|
+
import { compareFindings, compareStrings, ConfigError, editConfigText, generateConfigSchema, parseConfiguration, ruleRegistry, writeFileAtomic, } from "@wastech-mdlint/core";
|
|
6
|
+
import { highlight, withCopyButton } from "./highlight.js";
|
|
7
|
+
import { copyPathButton } from "./page.js";
|
|
8
|
+
// Enough to read through and act on; the counts above always cover all of them.
|
|
9
|
+
const LISTED_FINDINGS = 200;
|
|
10
|
+
// The package's schema, not a project-local one: it carries the same branch for every built-in rule
|
|
11
|
+
// and the generic custom-rule branch the form builds a custom rule from. Built once — it only
|
|
12
|
+
// changes with the installed package.
|
|
13
|
+
const CONFIG_SCHEMA = JSON.parse(generateConfigSchema());
|
|
14
|
+
export function hashText(text) {
|
|
15
|
+
return createHash("sha256").update(text, "utf8").digest("hex");
|
|
16
|
+
}
|
|
17
|
+
/** The config file the corpus was loaded from, read fresh from disk, or `undefined` when there is none. */
|
|
18
|
+
export async function readConfigFile(corpus) {
|
|
19
|
+
const absolutePath = corpus.configuration.configPath;
|
|
20
|
+
if (absolutePath === undefined) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const text = await readFile(absolutePath, "utf8").catch(() => undefined);
|
|
24
|
+
if (text === undefined) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
absolutePath,
|
|
29
|
+
displayPath: path
|
|
30
|
+
.relative(corpus.rootDir, absolutePath)
|
|
31
|
+
.split(path.sep)
|
|
32
|
+
.join("/"),
|
|
33
|
+
text,
|
|
34
|
+
hash: hashText(text),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Why saving is off, or `undefined` when it is on. Three conditions, each its own reason: the viewer
|
|
39
|
+
* was not started with the flag that allows writing; there is no config file (creating one is
|
|
40
|
+
* `init`'s job, which also wires the schema); or the file is outside the repository, the only tree
|
|
41
|
+
* the viewer is allowed to change.
|
|
42
|
+
*/
|
|
43
|
+
export async function writeBlockedReason(corpus, allowWrites) {
|
|
44
|
+
if (!allowWrites) {
|
|
45
|
+
return "Saving is off. Start the viewer with --allow-config-writes to save from this page; until then, copy or download the changed file.";
|
|
46
|
+
}
|
|
47
|
+
const file = await readConfigFile(corpus);
|
|
48
|
+
if (file === undefined) {
|
|
49
|
+
return "There is no config file to save to. Create one with wastech-mdlint init, then reload this page.";
|
|
50
|
+
}
|
|
51
|
+
if (!(await corpus.contains(file.absolutePath))) {
|
|
52
|
+
return "The config file is outside the repository, and the viewer writes only inside it.";
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const EDIT_OPS = new Set(["set", "remove", "insert"]);
|
|
57
|
+
/** Check the shape of a change request body; what the edits mean is for core to judge. */
|
|
58
|
+
export function parseChangeRequest(body) {
|
|
59
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) {
|
|
60
|
+
return { ok: false, message: "The request body must be a JSON object." };
|
|
61
|
+
}
|
|
62
|
+
const { base, text, edits, ...rest } = body;
|
|
63
|
+
const unknownKey = Object.keys(rest)[0];
|
|
64
|
+
if (unknownKey !== undefined) {
|
|
65
|
+
return { ok: false, message: `Unknown field "${unknownKey}".` };
|
|
66
|
+
}
|
|
67
|
+
if (typeof base !== "string" || !/^[0-9a-f]{64}$/.test(base)) {
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
message: '"base" must name the version of the file the change was made to.',
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (text !== undefined && typeof text !== "string") {
|
|
74
|
+
return { ok: false, message: '"text" must be a string.' };
|
|
75
|
+
}
|
|
76
|
+
const problem = edits === undefined ? undefined : editsProblem(edits);
|
|
77
|
+
if (problem !== undefined) {
|
|
78
|
+
return { ok: false, message: problem };
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
ok: true,
|
|
82
|
+
request: {
|
|
83
|
+
base,
|
|
84
|
+
text,
|
|
85
|
+
edits: edits,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// Why a request's `edits` is not a list of well-formed edits, or `undefined` when it is.
|
|
90
|
+
function editsProblem(edits) {
|
|
91
|
+
if (!Array.isArray(edits)) {
|
|
92
|
+
return '"edits" must be a list.';
|
|
93
|
+
}
|
|
94
|
+
for (const [index, edit] of edits.entries()) {
|
|
95
|
+
const { op, path: editPath } = (edit ?? {});
|
|
96
|
+
if (typeof op !== "string" ||
|
|
97
|
+
!EDIT_OPS.has(op) ||
|
|
98
|
+
!Array.isArray(editPath) ||
|
|
99
|
+
!editPath.every((segment) => typeof segment === "string" || typeof segment === "number")) {
|
|
100
|
+
return `Edit ${index + 1} must have an "op" of set, remove or insert and a "path" of names and indexes.`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
/** The candidate text a request describes, or core's reason it cannot be built. */
|
|
106
|
+
export function candidateText(file, request) {
|
|
107
|
+
const text = request.text ?? file.text;
|
|
108
|
+
// Text with no edits on top is taken as written, even when it does not parse: the reader is
|
|
109
|
+
// typing, and core's own parse error, with its offset, is the useful answer.
|
|
110
|
+
if (request.edits === undefined || request.edits.length === 0) {
|
|
111
|
+
return { ok: true, text };
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
return { ok: true, text: editConfigText(text, request.edits) };
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error instanceof ConfigError) {
|
|
118
|
+
return { ok: false, errors: messageLines(error) };
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** The candidate as core loads it, or core's diagnostics, one per line. */
|
|
124
|
+
export function validateCandidate(corpus, file, text) {
|
|
125
|
+
try {
|
|
126
|
+
return {
|
|
127
|
+
ok: true,
|
|
128
|
+
loaded: parseConfiguration({
|
|
129
|
+
text,
|
|
130
|
+
cwd: corpus.rootDir,
|
|
131
|
+
configPath: file.absolutePath,
|
|
132
|
+
}),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (error instanceof ConfigError) {
|
|
137
|
+
return { ok: false, errors: messageLines(error) };
|
|
138
|
+
}
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function messageLines(error) {
|
|
143
|
+
return error.message.split("\n").filter((line) => line.trim() !== "");
|
|
144
|
+
}
|
|
145
|
+
/** The canonical id of each rule entry, `null` for a custom rule or a name the registry does not know. */
|
|
146
|
+
export function entryRuleIds(config) {
|
|
147
|
+
return (config.rules ?? []).map((entry) => entry.rule !== "custom" && ruleRegistry.has(entry.rule)
|
|
148
|
+
? ruleRegistry.resolveId(entry.rule)
|
|
149
|
+
: null);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* What the candidate would report, compared with what the current config reports now.
|
|
153
|
+
*
|
|
154
|
+
* Findings are matched by core's `compareFindings` — on file, rule, line, severity and message, as a
|
|
155
|
+
* multiset — the comparison the review of a branch uses too, so a config edit and a branch never
|
|
156
|
+
* disagree about what counts as the same finding.
|
|
157
|
+
*/
|
|
158
|
+
export function previewCandidate(params) {
|
|
159
|
+
const { documentCount } = params.linted;
|
|
160
|
+
const result = { messages: params.linted.messages };
|
|
161
|
+
const delta = compareFindings(params.current, result.messages);
|
|
162
|
+
const added = previewFindings(delta.added);
|
|
163
|
+
const removed = previewFindings(delta.removed);
|
|
164
|
+
const byRule = new Map();
|
|
165
|
+
for (const [messages, side] of [
|
|
166
|
+
[params.current, "before"],
|
|
167
|
+
[result.messages, "after"],
|
|
168
|
+
]) {
|
|
169
|
+
for (const message of messages) {
|
|
170
|
+
const entry = byRule.get(message.ruleId) ?? { before: 0, after: 0 };
|
|
171
|
+
entry[side] += 1;
|
|
172
|
+
byRule.set(message.ruleId, entry);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
status: "ok",
|
|
177
|
+
text: params.text,
|
|
178
|
+
hash: hashText(params.text),
|
|
179
|
+
config: params.candidate.config,
|
|
180
|
+
ruleIds: entryRuleIds(params.candidate.config),
|
|
181
|
+
documents: {
|
|
182
|
+
before: params.corpus.documents.size,
|
|
183
|
+
after: documentCount,
|
|
184
|
+
},
|
|
185
|
+
findings: {
|
|
186
|
+
before: severityCounts(params.current),
|
|
187
|
+
after: severityCounts(result.messages),
|
|
188
|
+
},
|
|
189
|
+
byRule: [...byRule]
|
|
190
|
+
.sort(([left], [right]) => compareStrings(left, right))
|
|
191
|
+
.map(([ruleId, counts]) => ({ ruleId, ...counts })),
|
|
192
|
+
added: added.slice(0, LISTED_FINDINGS),
|
|
193
|
+
removed: removed.slice(0, LISTED_FINDINGS),
|
|
194
|
+
addedCount: added.length,
|
|
195
|
+
removedCount: removed.length,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
// The findings as the preview lists them, in a stable order.
|
|
199
|
+
function previewFindings(messages) {
|
|
200
|
+
const result = messages.map((message) => ({
|
|
201
|
+
filePath: message.filePath,
|
|
202
|
+
line: message.line,
|
|
203
|
+
ruleId: message.ruleId,
|
|
204
|
+
severity: message.severity,
|
|
205
|
+
message: message.message,
|
|
206
|
+
}));
|
|
207
|
+
return result.sort((left, right) => compareStrings(left.filePath, right.filePath) ||
|
|
208
|
+
left.line - right.line ||
|
|
209
|
+
compareStrings(left.ruleId, right.ruleId) ||
|
|
210
|
+
compareStrings(left.message, right.message));
|
|
211
|
+
}
|
|
212
|
+
function severityCounts(messages) {
|
|
213
|
+
const errors = messages.filter((message) => message.severity === "error").length;
|
|
214
|
+
return { errors, warnings: messages.length - errors };
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Write a validated candidate over the config, atomically, then read it back.
|
|
218
|
+
*
|
|
219
|
+
* `writeFileAtomic` replaces the file in one rename, so a failed write leaves the old file whole. The
|
|
220
|
+
* read-back catches what it cannot: another program writing the file in the same moment. Different
|
|
221
|
+
* text on disk after the rename can only have been written after it, so it is the newest version
|
|
222
|
+
* of the file and stays exactly as it is. Putting the version this page started from back over it
|
|
223
|
+
* would throw away that program's change to undo the reader's, and leave neither; the reader is told
|
|
224
|
+
* instead, and reloads to edit what is there now.
|
|
225
|
+
*
|
|
226
|
+
* `readBack` reads the file after the write; a test replaces it to stand in for the other program.
|
|
227
|
+
*/
|
|
228
|
+
export async function saveCandidate(params) {
|
|
229
|
+
const { file, text } = params;
|
|
230
|
+
const readBack = params.readBack ?? ((absolutePath) => readFile(absolutePath, "utf8"));
|
|
231
|
+
try {
|
|
232
|
+
await writeFileAtomic(file.absolutePath, text);
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
return {
|
|
236
|
+
status: "failed",
|
|
237
|
+
message: `The config could not be written (${error.code ?? "write error"}); the file on disk is unchanged.`,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const written = await readBack(file.absolutePath).catch(() => undefined);
|
|
241
|
+
if (written === text) {
|
|
242
|
+
return { status: "saved", hash: hashText(text) };
|
|
243
|
+
}
|
|
244
|
+
if (written === undefined) {
|
|
245
|
+
return {
|
|
246
|
+
status: "failed",
|
|
247
|
+
message: "The config was written but could not be read back, so whether it holds this change is unknown. Check the file before the next run.",
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
status: "conflict",
|
|
252
|
+
message: "Another program changed the config while it was being saved, and its version is the one on disk now. Reload the page to see it, then make the change again.",
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/** Everything the page's script needs to build the editor. */
|
|
256
|
+
export async function configApi(corpus, allowWrites) {
|
|
257
|
+
const file = await readConfigFile(corpus);
|
|
258
|
+
const validated = file === undefined ? undefined : validateCandidate(corpus, file, file.text);
|
|
259
|
+
return {
|
|
260
|
+
file: file === undefined
|
|
261
|
+
? null
|
|
262
|
+
: { path: file.displayPath, text: file.text, hash: file.hash },
|
|
263
|
+
config: validated?.ok === true ? validated.loaded.config : null,
|
|
264
|
+
ruleIds: validated?.ok === true ? entryRuleIds(validated.loaded.config) : [],
|
|
265
|
+
errors: validated?.ok === false ? validated.errors : [],
|
|
266
|
+
writeBlocked: (await writeBlockedReason(corpus, allowWrites)) ?? null,
|
|
267
|
+
schema: CONFIG_SCHEMA,
|
|
268
|
+
rules: registryRules(),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function registryRules() {
|
|
272
|
+
return ruleRegistry
|
|
273
|
+
.getAllMetadata()
|
|
274
|
+
.map((rule) => ({
|
|
275
|
+
id: rule.id,
|
|
276
|
+
alias: rule.alias,
|
|
277
|
+
category: rule.category,
|
|
278
|
+
description: rule.description,
|
|
279
|
+
defaultSeverity: rule.defaultSeverity,
|
|
280
|
+
fixable: rule.fixable,
|
|
281
|
+
docsUrl: rule.docsUrl,
|
|
282
|
+
}))
|
|
283
|
+
.sort((left, right) => compareStrings(left.id, right.id));
|
|
284
|
+
}
|
|
285
|
+
/** The id of the data the editor is built from, which the page's script reads by the same name. */
|
|
286
|
+
export const CONFIG_DATA_ID = "viewer.config-data";
|
|
287
|
+
// JSON to place inside a `<script>` element, which ends at the first `</script` whatever the JSON
|
|
288
|
+
// means: every `<` is written as its escape, which reads back as the same text and can neither end
|
|
289
|
+
// the element nor open a comment in it. A config file's own text is part of the data, and may hold
|
|
290
|
+
// either.
|
|
291
|
+
function scriptJson(value) {
|
|
292
|
+
return JSON.stringify(value).replaceAll("<", "\\u003c");
|
|
293
|
+
}
|
|
294
|
+
/** What a read-only config page says in place of the editor. */
|
|
295
|
+
export const CONFIG_READ_ONLY = "This page is read-only: the viewer is reachable from other machines (--host), so it previews, saves and opens nothing. Copy the file below to change it elsewhere, or start the viewer on a loopback address to edit it here.";
|
|
296
|
+
/**
|
|
297
|
+
* The page without its script: the file as it is, highlighted, and the registry's rules with the
|
|
298
|
+
* ones the config runs marked. Editing needs the script, which builds its form from the schema.
|
|
299
|
+
*/
|
|
300
|
+
export async function renderConfigArticle(corpus, options) {
|
|
301
|
+
const file = await readConfigFile(corpus);
|
|
302
|
+
const blocked = options.readOnly
|
|
303
|
+
? CONFIG_READ_ONLY
|
|
304
|
+
: await writeBlockedReason(corpus, options.allowWrites);
|
|
305
|
+
const nodes = [
|
|
306
|
+
h("h1", "Linter config"),
|
|
307
|
+
file === undefined
|
|
308
|
+
? h("p", "This repository has no wastech-mdlint config, so the linter runs with no rules. Create one with wastech-mdlint init, then reload this page to edit it.")
|
|
309
|
+
: h("p.config-file", [
|
|
310
|
+
"Config file: ",
|
|
311
|
+
h("code", file.displayPath),
|
|
312
|
+
" ",
|
|
313
|
+
copyPathButton(file.displayPath, options.showAbsolutePath ? file.absolutePath : undefined),
|
|
314
|
+
]),
|
|
315
|
+
// Hidden until the script that sends them has run. Left out entirely where opening is off, since
|
|
316
|
+
// the server would refuse them.
|
|
317
|
+
file === undefined || !options.canOpen
|
|
318
|
+
? undefined
|
|
319
|
+
: h("p.config-open", { hidden: true }, [
|
|
320
|
+
h("button.config-open-button", { type: "button", dataOpen: "reveal" }, "Show in folder"),
|
|
321
|
+
h("button.config-open-button", { type: "button", dataOpen: "open" }, "Open in default app"),
|
|
322
|
+
h("span.config-open-status", { role: "status" }),
|
|
323
|
+
]),
|
|
324
|
+
file === undefined
|
|
325
|
+
? undefined
|
|
326
|
+
: h("p.config-save-state", blocked ?? "Saving is on: a saved change is written to this file."),
|
|
327
|
+
file === undefined || options.readOnly
|
|
328
|
+
? undefined
|
|
329
|
+
: h("div.config-editor", { hidden: true }, [
|
|
330
|
+
h("p", "The editor is drawn by the page's script."),
|
|
331
|
+
// Carried in the page rather than asked for as it opens: asked for, it arrived a frame or
|
|
332
|
+
// two after the page was drawn, and the form then pushed the file and everything below it
|
|
333
|
+
// down the page. Read and replaced by the editor as it is built.
|
|
334
|
+
h("script", { type: "application/json", id: CONFIG_DATA_ID }, scriptJson(options.editor)),
|
|
335
|
+
]),
|
|
336
|
+
file === undefined
|
|
337
|
+
? undefined
|
|
338
|
+
: h("section.config-current", { ariaLabel: "Current file" }, [
|
|
339
|
+
h("h2", "Current file"),
|
|
340
|
+
withCopyButton(await highlight(file.text, "jsonc")),
|
|
341
|
+
]),
|
|
342
|
+
renderRegistry(corpus),
|
|
343
|
+
];
|
|
344
|
+
return nodes.filter((node) => node !== undefined);
|
|
345
|
+
}
|
|
346
|
+
// Every rule the registry knows, with the severity this config runs it at, or a dash where it does
|
|
347
|
+
// not run it.
|
|
348
|
+
function renderRegistry(corpus) {
|
|
349
|
+
const configured = new Map(corpus.configuration.rules.map(({ rule, severity }) => [
|
|
350
|
+
rule.id,
|
|
351
|
+
severity ?? rule.defaultSeverity,
|
|
352
|
+
]));
|
|
353
|
+
return h("section.config-registry", { ariaLabel: "Rules" }, [
|
|
354
|
+
h("h2", "Rules"),
|
|
355
|
+
h("div.dashboard-table-wrap", [
|
|
356
|
+
h("table.config-rules", [
|
|
357
|
+
h("thead", h("tr", [
|
|
358
|
+
h("th", { scope: "col" }, "Rule"),
|
|
359
|
+
h("th", { scope: "col" }, "Description"),
|
|
360
|
+
h("th", { scope: "col" }, "Default"),
|
|
361
|
+
h("th", { scope: "col" }, "In this config"),
|
|
362
|
+
])),
|
|
363
|
+
h("tbody", registryRules().map((rule) => h("tr", [
|
|
364
|
+
h("td", rule.docsUrl === undefined
|
|
365
|
+
? h("code", rule.id)
|
|
366
|
+
: h("a", {
|
|
367
|
+
href: rule.docsUrl,
|
|
368
|
+
rel: "noopener noreferrer",
|
|
369
|
+
target: "_blank",
|
|
370
|
+
}, h("code", rule.id))),
|
|
371
|
+
h("td", rule.description),
|
|
372
|
+
h("td", rule.defaultSeverity),
|
|
373
|
+
h("td", configured.get(rule.id) ?? "—"),
|
|
374
|
+
]))),
|
|
375
|
+
]),
|
|
376
|
+
]),
|
|
377
|
+
]);
|
|
378
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { ViewerContext } from "./viewer-context.js";
|
|
3
|
+
export declare class ConfigRoutes {
|
|
4
|
+
private readonly ctx;
|
|
5
|
+
private configWrites;
|
|
6
|
+
private readonly candidates;
|
|
7
|
+
constructor(ctx: ViewerContext);
|
|
8
|
+
sendConfigPage(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
9
|
+
sendConfigData(response: ServerResponse): Promise<void>;
|
|
10
|
+
changeConfig(response: ServerResponse, target: string, body: unknown): Promise<void>;
|
|
11
|
+
openConfig(response: ServerResponse, body: unknown): Promise<void>;
|
|
12
|
+
private changeBase;
|
|
13
|
+
private previewChange;
|
|
14
|
+
/** Stops the preview's lint thread, when the viewer closes. */
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
private currentFindings;
|
|
17
|
+
private saveChange;
|
|
18
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { CandidateLinter } from "./candidate-lint.js";
|
|
2
|
+
import { candidateText, configApi, parseChangeRequest, previewCandidate, readConfigFile, renderConfigArticle, saveCandidate, validateCandidate, writeBlockedReason, } from "./config-editor.js";
|
|
3
|
+
import { sendHtml, sendJson } from "./http-send.js";
|
|
4
|
+
import { openCommand } from "./open-file.js";
|
|
5
|
+
// The config page and the three things it asks of the server: a preview of what a changed config
|
|
6
|
+
// would report, a save, and showing the file in the system's own program.
|
|
7
|
+
export class ConfigRoutes {
|
|
8
|
+
ctx;
|
|
9
|
+
// Saves run one at a time: between checking that the file is the version the page edited and
|
|
10
|
+
// replacing it, a second save must not slip in.
|
|
11
|
+
configWrites = Promise.resolve();
|
|
12
|
+
candidates = new CandidateLinter();
|
|
13
|
+
constructor(ctx) {
|
|
14
|
+
this.ctx = ctx;
|
|
15
|
+
}
|
|
16
|
+
async sendConfigPage(request, response) {
|
|
17
|
+
sendHtml(response, 200, await this.ctx.page({
|
|
18
|
+
title: "Linter config",
|
|
19
|
+
section: "config",
|
|
20
|
+
article: await renderConfigArticle(this.ctx.corpus, {
|
|
21
|
+
allowWrites: this.ctx.allowConfigWrites,
|
|
22
|
+
showAbsolutePath: this.ctx.showAbsolutePaths,
|
|
23
|
+
canOpen: this.ctx.openRefused === undefined,
|
|
24
|
+
editor: await configApi(this.ctx.corpus, this.ctx.allowConfigWrites),
|
|
25
|
+
// Without a token the server refuses every preview, save and open, so the page offers
|
|
26
|
+
// none: the editor's script starts only where the page carries one.
|
|
27
|
+
readOnly: this.ctx.changeToken === undefined,
|
|
28
|
+
}),
|
|
29
|
+
changeToken: this.ctx.changeToken,
|
|
30
|
+
scriptFirst: true,
|
|
31
|
+
}), request.method === "HEAD");
|
|
32
|
+
}
|
|
33
|
+
async sendConfigData(response) {
|
|
34
|
+
sendJson(response, await configApi(this.ctx.corpus, this.ctx.allowConfigWrites));
|
|
35
|
+
}
|
|
36
|
+
// A preview or a save, once its body has passed the checks every change request passes.
|
|
37
|
+
async changeConfig(response, target, body) {
|
|
38
|
+
const parsed = parseChangeRequest(body);
|
|
39
|
+
if (!parsed.ok) {
|
|
40
|
+
sendJson(response, { status: "refused", error: parsed.message }, 400);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (target === "/api/config/save") {
|
|
44
|
+
const run = this.configWrites.then(() => this.saveChange(response, parsed.request));
|
|
45
|
+
this.configWrites = run.catch(() => undefined);
|
|
46
|
+
await run;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
await this.previewChange(response, parsed.request);
|
|
50
|
+
}
|
|
51
|
+
// Shows the config file in the file manager or opens it in its default application, on the
|
|
52
|
+
// machine running the viewer. The file is always the one the corpus was loaded from; the request
|
|
53
|
+
// only picks which of the two, so it can name no other file and pass no argument of its own.
|
|
54
|
+
async openConfig(response, body) {
|
|
55
|
+
const action = body?.action;
|
|
56
|
+
if (action !== "reveal" && action !== "open") {
|
|
57
|
+
sendJson(response, {
|
|
58
|
+
status: "refused",
|
|
59
|
+
error: 'The action must be "reveal" or "open".',
|
|
60
|
+
}, 400);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (this.ctx.openRefused !== undefined) {
|
|
64
|
+
sendJson(response, { status: "refused", error: this.ctx.openRefused }, 403);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const file = await readConfigFile(this.ctx.corpus);
|
|
68
|
+
if (file === undefined) {
|
|
69
|
+
sendJson(response, {
|
|
70
|
+
status: "refused",
|
|
71
|
+
error: "This repository has no config file to open.",
|
|
72
|
+
}, 409);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const command = openCommand(process.platform, action, file.absolutePath);
|
|
76
|
+
if (command === undefined) {
|
|
77
|
+
sendJson(response, {
|
|
78
|
+
status: "refused",
|
|
79
|
+
error: "The config file's path cannot be passed to the system safely.",
|
|
80
|
+
}, 409);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
await this.ctx.launch(command);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
sendJson(response, {
|
|
88
|
+
status: "failed",
|
|
89
|
+
error: `Could not start ${command.command}: ${error.message}`,
|
|
90
|
+
}, 500);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
sendJson(response, { status: "opened", action });
|
|
94
|
+
}
|
|
95
|
+
// The file a change applies to, or `undefined` after answering why there is none to apply it to.
|
|
96
|
+
async changeBase(response, base) {
|
|
97
|
+
const file = await readConfigFile(this.ctx.corpus);
|
|
98
|
+
if (file === undefined) {
|
|
99
|
+
sendJson(response, {
|
|
100
|
+
status: "refused",
|
|
101
|
+
error: "This repository has no config file to change.",
|
|
102
|
+
}, 409);
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
if (file.hash !== base) {
|
|
106
|
+
sendJson(response, {
|
|
107
|
+
status: "conflict",
|
|
108
|
+
error: "The config changed on disk since this page read it. Reload the page to edit the new version.",
|
|
109
|
+
}, 409);
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return file;
|
|
113
|
+
}
|
|
114
|
+
async previewChange(response, change) {
|
|
115
|
+
const file = await this.changeBase(response, change.base);
|
|
116
|
+
if (file === undefined) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const candidate = candidateText(file, change);
|
|
120
|
+
if (!candidate.ok) {
|
|
121
|
+
sendJson(response, { status: "invalid", errors: candidate.errors }, 422);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const validated = validateCandidate(this.ctx.corpus, file, candidate.text);
|
|
125
|
+
if (!validated.ok) {
|
|
126
|
+
sendJson(response, { status: "invalid", errors: validated.errors, text: candidate.text }, 422);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const { corpus } = this.ctx;
|
|
130
|
+
// A response closed before it was answered is a request the page gave up on.
|
|
131
|
+
const abandoned = new AbortController();
|
|
132
|
+
response.once("close", () => {
|
|
133
|
+
if (!response.writableFinished) {
|
|
134
|
+
abandoned.abort();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
const linted = await this.candidates.lint({
|
|
138
|
+
rootDir: corpus.rootDir,
|
|
139
|
+
configPath: file.absolutePath,
|
|
140
|
+
text: candidate.text,
|
|
141
|
+
sameScope: corpus.sharesScope(validated.loaded),
|
|
142
|
+
documents: corpus.documents,
|
|
143
|
+
}, abandoned.signal);
|
|
144
|
+
if (!linted.ok && linted.reason === "abandoned") {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (!linted.ok) {
|
|
148
|
+
sendJson(response, { status: "failed", error: linted.message }, 503);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
sendJson(response, previewCandidate({
|
|
152
|
+
corpus,
|
|
153
|
+
current: this.currentFindings(),
|
|
154
|
+
candidate: validated.loaded,
|
|
155
|
+
text: candidate.text,
|
|
156
|
+
linted,
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
/** Stops the preview's lint thread, when the viewer closes. */
|
|
160
|
+
close() {
|
|
161
|
+
return this.candidates.close();
|
|
162
|
+
}
|
|
163
|
+
// What the config reports now: the background lint's result when it is in, else a lint run here.
|
|
164
|
+
currentFindings() {
|
|
165
|
+
const byFile = this.ctx.diagnostics.messagesByFile;
|
|
166
|
+
return byFile === undefined
|
|
167
|
+
? this.ctx.corpus.lint().messages
|
|
168
|
+
: [...byFile.values()].flat();
|
|
169
|
+
}
|
|
170
|
+
async saveChange(response, change) {
|
|
171
|
+
const blocked = await writeBlockedReason(this.ctx.corpus, this.ctx.allowConfigWrites);
|
|
172
|
+
if (blocked !== undefined) {
|
|
173
|
+
sendJson(response, { status: "refused", error: blocked }, 403);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (change.text === undefined || change.edits !== undefined) {
|
|
177
|
+
// A save writes exactly the text the reader previewed, never a result computed afresh here.
|
|
178
|
+
sendJson(response, {
|
|
179
|
+
status: "refused",
|
|
180
|
+
error: "A save carries the previewed text and no edits.",
|
|
181
|
+
}, 400);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const file = await this.changeBase(response, change.base);
|
|
185
|
+
if (file === undefined) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const validated = validateCandidate(this.ctx.corpus, file, change.text);
|
|
189
|
+
if (!validated.ok) {
|
|
190
|
+
sendJson(response, { status: "invalid", errors: validated.errors }, 422);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const outcome = await saveCandidate({ file, text: change.text });
|
|
194
|
+
if (outcome.status === "failed") {
|
|
195
|
+
sendJson(response, { status: "failed", error: outcome.message }, 500);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (outcome.status === "conflict") {
|
|
199
|
+
sendJson(response, { status: "conflict", error: outcome.message }, 409);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// The watcher sees the config change and reloads the corpus as it does for any edit; without
|
|
203
|
+
// one nothing else would, and every page would go on showing the old config's findings.
|
|
204
|
+
if (!this.ctx.watch) {
|
|
205
|
+
await this.ctx.corpus.reload();
|
|
206
|
+
this.ctx.renders.clear();
|
|
207
|
+
this.ctx.changes?.invalidate();
|
|
208
|
+
this.ctx.events.broadcast("change", { paths: [], structural: true });
|
|
209
|
+
this.ctx.relint();
|
|
210
|
+
}
|
|
211
|
+
sendJson(response, outcome);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Element } from "hast";
|
|
2
|
+
import type { ContextDelta, DocumentContextDelta, EagerContextChange, FindingsDelta, LintMessage } from "@wastech-mdlint/core";
|
|
3
|
+
import type { ReviewContext } from "./changes.js";
|
|
4
|
+
export type FindingCounts = {
|
|
5
|
+
errors: number;
|
|
6
|
+
warnings: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function countFindings(messages: readonly LintMessage[]): FindingCounts;
|
|
9
|
+
/**
|
|
10
|
+
* What the change does to findings, in words: "+1 error, +2 warnings, −3 findings", or nothing when
|
|
11
|
+
* it adds and removes none. The same wording on the summary and on every row, so a reader learns it
|
|
12
|
+
* once — and a test reads the page's numbers back from it.
|
|
13
|
+
*/
|
|
14
|
+
export declare function findingsText(delta: FindingsDelta): string | undefined;
|
|
15
|
+
/** The summary's lines about the context: findings, every session's budget, and what is approximate. */
|
|
16
|
+
export declare function renderContextSummary(context: ReviewContext): Element[];
|
|
17
|
+
/**
|
|
18
|
+
* The approximations behind the numbers, said where the numbers are. Links to files other than
|
|
19
|
+
* documents are checked against the files on disk on both sides, because a lint needs a real
|
|
20
|
+
* directory to look in; and a config whose scope changed is still read through the current scope.
|
|
21
|
+
*/
|
|
22
|
+
export declare function contextNotes(context: ReviewContext): string[];
|
|
23
|
+
/** "CLAUDE.md eager context 8921 → 10.7k tokens (+1856, budget 10k)". */
|
|
24
|
+
export declare function eagerText(change: EagerContextChange): string;
|
|
25
|
+
/** A changed document's context on its card: its size, its findings and its links, each when moved. */
|
|
26
|
+
export declare function renderDocumentContext(delta: DocumentContextDelta): Element | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The unchanged documents the change reaches: those with findings they did not have, those linking
|
|
29
|
+
* into code lines that moved under them, and — folded, since a much-linked document would flood the
|
|
30
|
+
* page — those linking to a changed document.
|
|
31
|
+
*/
|
|
32
|
+
export declare function renderAffected(delta: ContextDelta): Element | undefined;
|
|
33
|
+
/** On the config's card: what the config change alone does to findings over the head's documents. */
|
|
34
|
+
export declare function renderConfigOnly(delta: FindingsDelta | undefined): Element;
|
|
35
|
+
/** A finding as JSON carries it: its place and its words, without the rule's internal data. */
|
|
36
|
+
export declare function findingPayload(message: LintMessage): unknown;
|
|
37
|
+
export declare function findingsPayload(delta: FindingsDelta): unknown;
|
|
38
|
+
/** One changed document's context, as JSON. */
|
|
39
|
+
export declare function documentContextPayload(delta: DocumentContextDelta): unknown;
|
|
40
|
+
/** The whole review's context, as JSON, beside what `/api/changes` already lists. */
|
|
41
|
+
export declare function contextPayload(context: ReviewContext): unknown;
|