@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
package/dist/page.js ADDED
@@ -0,0 +1,478 @@
1
+ import { toHtml } from "hast-util-to-html";
2
+ import { h, s } from "hastscript";
3
+ import { renderTree } from "./page-tree.js";
4
+ import { documentHref } from "./render-document.js";
5
+ import { changesCount } from "./tree-marks.js";
6
+ /**
7
+ * The id of the page's main column, the skip link's target and where a finding about the whole file
8
+ * jumps to.
9
+ *
10
+ * Every id the viewer gives its own elements contains a `.`: author headings get core's slugs as ids,
11
+ * verbatim so the anchors REF-002 accepts exist on the page, and a slug never contains a `.`. An id a
12
+ * heading could also have — `content` for a heading "Content" — would send that heading's outline
13
+ * entry to the first element with it, the viewer's own.
14
+ */
15
+ export const CONTENT_ID = "viewer.content";
16
+ // The id of an empty element at the very end of the page, which the page is not drawn before (see
17
+ // `renderHead`). An element of its own rather than the layout around the tree and the article, since
18
+ // a browser stops waiting for an element as soon as it has read its start tag.
19
+ const END_ID = "viewer.end";
20
+ /**
21
+ * The id a long page gives the first element below its first screen, so the page is drawn once
22
+ * everything above it has been read rather than all of it (`firstScreen`). The Changes page and the
23
+ * dashboard are several hundred kilobytes, most of it far below the first screen, and waiting for all
24
+ * of it held them back by a fifth of a second; what arrives after the first frame is out of sight.
25
+ */
26
+ export const FIRST_SCREEN_ID = "viewer.first-screen";
27
+ /** An empty element to place where a long page's first screen ends, when nothing there can carry the id. */
28
+ export function firstScreenMarker() {
29
+ return h("div", { id: FIRST_SCREEN_ID, hidden: true });
30
+ }
31
+ // The theme switch's three choices, one segment each. The stylesheet marks the chosen one from the
32
+ // choice `theme.js` applied before the first paint, so the control is right from the first frame
33
+ // rather than once the module script has run. Each segment is an icon, drawn as SVG paths rather than
34
+ // the ◐ ☀ ☾ characters, which sit at a different height in every font the reader can pick; its label
35
+ // is the segment's name and tooltip.
36
+ const THEME_CHOICES = [
37
+ {
38
+ choice: "system",
39
+ label: "Auto",
40
+ // A circle, half filled: the system decides.
41
+ icon: [
42
+ s("circle", { cx: 8, cy: 8, r: 5.75 }),
43
+ s("path", {
44
+ d: "M8 2.25a5.75 5.75 0 0 1 0 11.5Z",
45
+ fill: "currentColor",
46
+ stroke: "none",
47
+ }),
48
+ ],
49
+ },
50
+ {
51
+ choice: "light",
52
+ label: "Light",
53
+ // A sun: a disc and eight rays.
54
+ icon: [
55
+ s("circle", { cx: 8, cy: 8, r: 3 }),
56
+ s("path", {
57
+ d: "M8 1.5v1.5M8 13v1.5M1.5 8H3M13 8h1.5M3.4 3.4l1.06 1.06M11.54 11.54l1.06 1.06M3.4 12.6l1.06-1.06M11.54 4.46l1.06-1.06",
58
+ }),
59
+ ],
60
+ },
61
+ {
62
+ choice: "dark",
63
+ label: "Dark",
64
+ // A crescent moon.
65
+ icon: [
66
+ s("path", {
67
+ d: "M13.5 9.6A5.75 5.75 0 1 1 6.4 2.5a4.6 4.6 0 0 0 7.1 7.1Z",
68
+ }),
69
+ ],
70
+ },
71
+ ];
72
+ export function renderPage(params) {
73
+ const tree = {
74
+ type: "root",
75
+ children: [
76
+ { type: "doctype" },
77
+ h("html", { lang: "en" }, [
78
+ renderHead(params),
79
+ h("body", [
80
+ h("a.skip-link", { href: `#${CONTENT_ID}` }, "Skip to content"),
81
+ renderTopbar(params),
82
+ h("div.layout", [
83
+ renderSidebar(params),
84
+ h("main.content", { id: CONTENT_ID }, [
85
+ h(`article.markdown-body${params.blameToggle?.on === true ? ".has-blame" : ""}`, [
86
+ params.findingsPanel,
87
+ params.changeBanner,
88
+ params.historyLine,
89
+ renderFrontmatter(params.frontmatter ?? null, params.frontmatterChange),
90
+ ...params.article,
91
+ ]),
92
+ ]),
93
+ renderRail(params),
94
+ ]),
95
+ h("div", { id: END_ID, hidden: true }),
96
+ ]),
97
+ ]),
98
+ ],
99
+ };
100
+ return toHtml(tree, { allowDangerousHtml: true });
101
+ }
102
+ function renderHead(params) {
103
+ return h("head", [
104
+ h("meta", { charSet: "utf-8" }),
105
+ h("meta", {
106
+ name: "viewport",
107
+ content: "width=device-width, initial-scale=1",
108
+ }),
109
+ h("meta", { name: "color-scheme", content: "light dark" }),
110
+ params.documentPath === undefined
111
+ ? undefined
112
+ : h("meta", { name: "wastech-doc", content: params.documentPath }),
113
+ params.changeToken === undefined
114
+ ? undefined
115
+ : h("meta", {
116
+ name: "wastech-change-token",
117
+ content: params.changeToken,
118
+ }),
119
+ params.eventSequence === undefined
120
+ ? undefined
121
+ : h("meta", {
122
+ name: "wastech-events",
123
+ content: params.eventSequence,
124
+ }),
125
+ h("title", `${params.title} · ${params.siteName}`),
126
+ h("link", { rel: "icon", href: "/favicon.ico" }),
127
+ // Classic and blocking, ahead of the stylesheet, so a stored theme is applied before the
128
+ // first paint; every other script is a deferred module.
129
+ h("script", { src: "/assets/theme.js" }),
130
+ h("link", { rel: "stylesheet", href: "/assets/viewer.css" }),
131
+ h("script", {
132
+ type: "module",
133
+ src: "/assets/viewer.js",
134
+ blocking: params.scriptFirst === true ? "render" : undefined,
135
+ }),
136
+ // Nothing of the page is drawn until all of it has been read, or on a long page all of its first
137
+ // screen. The blocking script after the tree stops the parser while it is fetched, and the
138
+ // browser drew what it had by then — the header and the tree over an empty article — one frame
139
+ // before the rest, so a navigation blinked through an empty page. Browsers that do not know this
140
+ // link draw as before.
141
+ h("link", {
142
+ rel: "expect",
143
+ href: `#${params.firstScreen === true ? FIRST_SCREEN_ID : END_ID}`,
144
+ blocking: "render",
145
+ }),
146
+ ]);
147
+ }
148
+ function renderTopbar(params) {
149
+ return h("header.topbar", [
150
+ h("a.site-name", { href: "/" }, params.siteName),
151
+ params.displayPath === undefined
152
+ ? undefined
153
+ : h("span.current-path", params.displayPath),
154
+ params.displayPath === undefined
155
+ ? undefined
156
+ : copyPathButton(params.displayPath, params.absolutePath),
157
+ params.blameToggle === undefined
158
+ ? undefined
159
+ : h("a.blame-toggle", {
160
+ href: params.blameToggle.href,
161
+ ariaPressed: params.blameToggle.on ? "true" : "false",
162
+ title: params.blameToggle.on
163
+ ? "Hide who last changed each block"
164
+ : "Show who last changed each block, in the right margin",
165
+ }, "Blame"),
166
+ // A plain GET form, so search works as a results page even before (or without) the
167
+ // script that turns it into a live list.
168
+ h("form.search", { action: "/search", method: "get", role: "search" }, [
169
+ h("input", {
170
+ type: "search",
171
+ name: "q",
172
+ value: params.query,
173
+ placeholder: "Search documents",
174
+ ariaLabel: "Search documents",
175
+ autoComplete: "off",
176
+ spellCheck: false,
177
+ }),
178
+ h("kbd.search-shortcut", { ariaHidden: "true" }, "Ctrl K"),
179
+ ]),
180
+ renderSectionNav(params),
181
+ renderThemeSwitch(),
182
+ // Opens the display settings panel the page script builds; laid out like the theme
183
+ // switch, and for the same reason.
184
+ h("button.settings-toggle", {
185
+ type: "button",
186
+ hidden: true,
187
+ title: "Display settings",
188
+ ariaLabel: "Display settings",
189
+ }, "Aa"),
190
+ ]);
191
+ }
192
+ // The three sections as one segmented control of plain links: the page's own section is the chosen
193
+ // segment, and the links work without script.
194
+ function renderSectionNav(params) {
195
+ return h("nav.section-nav.segmented", { ariaLabel: "Sections" }, [
196
+ // From a document, the graph opens with that document and its neighbors lit, which is
197
+ // how a reader gets from a page to its place among the others.
198
+ sectionLink(params, "graph", params.documentPath === undefined
199
+ ? "/graph"
200
+ : `/graph?${new URLSearchParams({ focus: params.documentPath }).toString()}`, "Graph"),
201
+ sectionLink(params, "dashboard", "/dashboard", "Dashboard"),
202
+ params.showChanges === true
203
+ ? sectionLink(params, "changes", "/changes", [
204
+ "Changes",
205
+ params.marks?.changes === undefined
206
+ ? undefined
207
+ : changesCount(params.marks.changes),
208
+ ])
209
+ : undefined,
210
+ sectionLink(params, "config", "/config", "Config"),
211
+ ]);
212
+ }
213
+ // Hidden without script, which it needs to do anything. With script, the stylesheet lays it out
214
+ // from the first paint (see `theme.ts`), before the module that wires it has run.
215
+ function renderThemeSwitch() {
216
+ return h("div.theme-switch.segmented", { role: "radiogroup", ariaLabel: "Theme", hidden: true }, THEME_CHOICES.map(({ choice, icon, label }) => h("button.segment", {
217
+ type: "button",
218
+ role: "radio",
219
+ dataChoice: choice,
220
+ title: label,
221
+ ariaLabel: label,
222
+ }, s("svg", {
223
+ viewBox: "0 0 16 16",
224
+ width: 16,
225
+ height: 16,
226
+ fill: "none",
227
+ stroke: "currentColor",
228
+ strokeWidth: 1.5,
229
+ strokeLinecap: "round",
230
+ ariaHidden: "true",
231
+ }, [...icon]))));
232
+ }
233
+ function renderSidebar(params) {
234
+ const current = params.marks?.current ?? [];
235
+ return h("div.sidebar-frame", [
236
+ h("nav.sidebar", {
237
+ ariaLabel: "Documents",
238
+ // Which marks in the tree are current, so the page script does not ask for them again and
239
+ // the tree-state script may narrow the tree to them before the first paint.
240
+ dataMarked: current.length === 0 ? undefined : current.join(" "),
241
+ }, [
242
+ // On a phone the tree folds behind this row, so the article starts under the header
243
+ // rather than below a screenful of file names; the tree-state script closes it there
244
+ // before the first paint. On a wider screen the row is not drawn and the tree stays
245
+ // open, and without script it is open everywhere, as a list that cannot be opened
246
+ // would be no list at all.
247
+ h("details.sidebar-disclosure", { open: true }, [
248
+ h("summary.sidebar-summary", "Documents"),
249
+ ...renderTreeFilters(params),
250
+ renderTree(params.documentPaths, params.documentPath, params.marks),
251
+ ]),
252
+ ]),
253
+ // Classic and blocking, right after the tree, so the folders the reader had open and
254
+ // the tree's scroll position are back before the tree is first painted.
255
+ h("script", { src: "/assets/tree-state.js" }),
256
+ columnResizer("sidebar", "Resize the document tree"),
257
+ ]);
258
+ }
259
+ // The switches that narrow the tree, each with the line it shows when nothing is left. Hidden until
260
+ // the script that knows the findings and the changes has run, like the theme switch.
261
+ function renderTreeFilters(params) {
262
+ return [
263
+ treeSwitch("findings", "Only files with findings"),
264
+ h("p.tree-filter-empty.tree-filter-findings-empty", { hidden: true }, "No file has findings."),
265
+ params.showChanges === true
266
+ ? treeSwitch("changes", "Only changed files")
267
+ : undefined,
268
+ params.showReview === true
269
+ ? treeSwitch("unreviewed", "Only files left to review")
270
+ : undefined,
271
+ params.showChanges === true
272
+ ? h("p.tree-filter-empty.tree-filter-changes-empty", { hidden: true }, "No document changed.")
273
+ : undefined,
274
+ ];
275
+ }
276
+ function treeSwitch(name, label) {
277
+ return h(`label.tree-filter.tree-filter-${name}.switch-row`, { hidden: true }, [h("span", label), h("input.switch", { type: "checkbox", role: "switch" })]);
278
+ }
279
+ /**
280
+ * The button that copies a file's path, relative or absolute, through a menu the page script adds.
281
+ * Hidden without script, which the copying needs. `absolutePath` is left off unless the viewer is
282
+ * bound to a loopback address; the menu then says why that choice is missing.
283
+ */
284
+ export function copyPathButton(relativePath, absolutePath) {
285
+ return h("button.copy-path", {
286
+ type: "button",
287
+ hidden: true,
288
+ title: "Copy the path",
289
+ ariaLabel: `Copy the path of ${relativePath}`,
290
+ dataPath: relativePath,
291
+ dataAbsolutePath: absolutePath,
292
+ },
293
+ // Two overlapping sheets, the usual mark for copying, drawn in the text color.
294
+ s("svg", {
295
+ viewBox: "0 0 16 16",
296
+ width: 14,
297
+ height: 14,
298
+ fill: "none",
299
+ stroke: "currentColor",
300
+ strokeWidth: 1.5,
301
+ ariaHidden: "true",
302
+ }, [
303
+ s("rect", { x: 5.5, y: 5.5, width: 8, height: 8, rx: 1.5 }),
304
+ s("path", {
305
+ d: "M10.5 3.5v-1a1 1 0 0 0-1-1h-6a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h1",
306
+ }),
307
+ ]));
308
+ }
309
+ function sectionLink(params, section, href, content) {
310
+ return h("a.segment", {
311
+ href,
312
+ ariaCurrent: params.section === section ? "page" : undefined,
313
+ }, content);
314
+ }
315
+ /** Article content for an already-sanitized HTML string. */
316
+ export function rawHtml(html) {
317
+ return [{ type: "raw", value: html }];
318
+ }
319
+ function renderRail(params) {
320
+ const backlinks = params.backlinks ?? [];
321
+ // The document title is the page's own heading; the outline starts below it and stops at h3,
322
+ // where entries become too fine-grained to scan in a side column.
323
+ const outline = (params.headings ?? []).filter((heading) => heading.depth === 2 || heading.depth === 3);
324
+ const neighborCount = params.documentPath === undefined ? 0 : (params.neighborCount ?? 0);
325
+ if (outline.length === 0 && backlinks.length === 0 && neighborCount === 0) {
326
+ return undefined;
327
+ }
328
+ // Framed like the tree, so its width handle spans the full height wherever the rail is scrolled to.
329
+ return h("div.rail-frame", [
330
+ h("aside.rail", [
331
+ neighborCount === 0 || params.documentPath === undefined
332
+ ? undefined
333
+ : h("section.local-graph", { ariaLabel: "Linked documents" }, [
334
+ h("h2", `Links (${neighborCount})`),
335
+ // Drawn by the page script, which shows the box once it has something to put in it.
336
+ h("div.local-graph-canvas", {
337
+ dataPath: params.documentPath,
338
+ dataChanges: params.localGraphChanges === true ? "1" : undefined,
339
+ hidden: true,
340
+ }),
341
+ h("p.local-graph-hint", { hidden: true }, "Ctrl or ⌘ + scroll to zoom, drag to move"),
342
+ h("a.local-graph-open", {
343
+ href: `/graph?${new URLSearchParams({ focus: params.documentPath }).toString()}`,
344
+ }, "Open in the full graph"),
345
+ ]),
346
+ outline.length === 0
347
+ ? undefined
348
+ : h("nav.outline", { ariaLabel: "On this page" }, [
349
+ h("h2", "On this page"),
350
+ h("ul", [
351
+ params.introduction === undefined
352
+ ? undefined
353
+ : outlineEntry(params, {
354
+ // Its section key is `""`, as core's `documentSections` names it. It jumps to the
355
+ // top of the main column, where the text it stands for begins.
356
+ key: "",
357
+ depth: 2,
358
+ text: "Introduction",
359
+ href: `#${CONTENT_ID}`,
360
+ changed: params.introduction.changed,
361
+ }),
362
+ ...outline.map((heading) => outlineEntry(params, {
363
+ key: heading.slug,
364
+ depth: heading.depth,
365
+ text: heading.text,
366
+ href: `#${heading.slug}`,
367
+ changed: params.changedSections?.has(heading.slug) === true,
368
+ })),
369
+ ]),
370
+ ]),
371
+ backlinks.length === 0
372
+ ? undefined
373
+ : h("section.backlinks", { ariaLabel: "Linked from" }, [
374
+ h("h2", `Linked from (${backlinks.length})`),
375
+ h("ul", backlinks.map((source) => h("li", [h("a", { href: documentHref(source) }, source)]))),
376
+ ]),
377
+ ]),
378
+ columnResizer("rail", "Resize the side column"),
379
+ ]);
380
+ }
381
+ /**
382
+ * One entry of the outline: a link to its section, the branch's change mark, and the button that
383
+ * marks the section viewed where the page marks sections.
384
+ */
385
+ function outlineEntry(params, entry) {
386
+ const mark = params.sectionMarks?.marks.get(entry.key);
387
+ return h(`li.depth-${entry.depth}`, { dataReview: mark?.status }, [
388
+ h("a", { href: entry.href }, [
389
+ entry.text,
390
+ entry.changed
391
+ ? h("span.outline-change", {
392
+ role: "img",
393
+ ariaLabel: "changed in this branch",
394
+ title: "Changed in this branch",
395
+ })
396
+ : undefined,
397
+ ]),
398
+ mark === undefined || params.sectionMarks === undefined
399
+ ? undefined
400
+ : sectionMarkButton(params.sectionMarks.path, entry.text, mark),
401
+ ]);
402
+ }
403
+ const SECTION_STATUS = {
404
+ reviewed: "Viewed",
405
+ changed: "Changed since you viewed it",
406
+ unreviewed: "Not viewed yet",
407
+ };
408
+ /**
409
+ * The button that marks one section viewed, beside its outline entry: pressed while the section's
410
+ * own text is one the reader reviewed. Hidden until the script that sends the mark has run; without
411
+ * it, the entry's `data-review` still shows where the section stands.
412
+ */
413
+ function sectionMarkButton(relPath, headingText, mark) {
414
+ return h("button.section-mark", {
415
+ type: "button",
416
+ hidden: true,
417
+ ariaPressed: mark.status === "reviewed" ? "true" : "false",
418
+ ariaLabel: `Mark the section "${headingText}" as viewed`,
419
+ title: SECTION_STATUS[mark.status],
420
+ dataPath: relPath,
421
+ dataSection: mark.key,
422
+ dataHash: mark.hash,
423
+ }, "✓");
424
+ }
425
+ // A column's width handle, on its edge toward the article. Hidden until the script that drags it has
426
+ // run, like the theme switch, but rendered as the focusable separator it becomes.
427
+ function columnResizer(column, label) {
428
+ return h("div.column-resizer", {
429
+ dataColumn: column,
430
+ role: "separator",
431
+ ariaOrientation: "vertical",
432
+ ariaLabel: label,
433
+ tabIndex: 0,
434
+ hidden: true,
435
+ });
436
+ }
437
+ function renderFrontmatter(frontmatter, change) {
438
+ if (frontmatter === null) {
439
+ return undefined;
440
+ }
441
+ // Marked like any block of the article, so the card shows a change the same way and the page
442
+ // script finds its hunks the same way.
443
+ const properties = {
444
+ ariaLabel: "Front matter",
445
+ dataChange: change?.kind,
446
+ dataChangeHunks: change?.hunks.join(" "),
447
+ };
448
+ const { parsed } = frontmatter;
449
+ if (!parsed.ok) {
450
+ // Shown rather than hidden: a block the linter cannot parse is the block an author needs to see.
451
+ return h("section.frontmatter.frontmatter-invalid", properties, [
452
+ h("p", `Front matter is not valid YAML: ${parsed.error.message}`),
453
+ h("pre", frontmatter.raw),
454
+ ]);
455
+ }
456
+ const value = parsed.value;
457
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
458
+ return h("section.frontmatter", properties, [h("pre", frontmatter.raw)]);
459
+ }
460
+ return h("section.frontmatter", properties, [
461
+ h("dl", Object.entries(value).flatMap(([key, entry]) => [
462
+ h("dt", key),
463
+ h("dd", formatFrontmatterValue(entry)),
464
+ ])),
465
+ ]);
466
+ }
467
+ function formatFrontmatterValue(value) {
468
+ if (value === null) {
469
+ return "—";
470
+ }
471
+ if (typeof value === "string") {
472
+ return value;
473
+ }
474
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
475
+ return value.join(", ");
476
+ }
477
+ return JSON.stringify(value);
478
+ }
@@ -0,0 +1,25 @@
1
+ import { type LintMessage, type PromptStrategy } from "@wastech-mdlint/core";
2
+ import type { Corpus } from "./corpus.js";
3
+ export type PromptChoice = {
4
+ strategy: PromptStrategy;
5
+ label: string;
6
+ text: string;
7
+ };
8
+ export type PromptsAnswer = {
9
+ status: 200;
10
+ body: {
11
+ prompts: PromptChoice[];
12
+ };
13
+ } | {
14
+ status: 404 | 409;
15
+ body: {
16
+ error: string;
17
+ };
18
+ };
19
+ export declare function findingPrompts(params: {
20
+ corpus: Corpus;
21
+ findings: ReadonlyMap<string, readonly LintMessage[]> | undefined;
22
+ filePath: string;
23
+ ruleId?: string;
24
+ line?: number;
25
+ }): Promise<PromptsAnswer>;
@@ -0,0 +1,82 @@
1
+ import { existsSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
4
+ import { buildFindingPrompt, cliRunner, detectPackageManager, findingPromptStrategies, PROMPT_STRATEGY_LABELS, } from "@wastech-mdlint/core";
5
+ import { entryRuleIds } from "./config-editor.js";
6
+ // The CLI, the viewer and core are released together under one version, so the viewer's own is the
7
+ // version of the CLI release a prompt pins when the repository has none installed.
8
+ const VIEWER_VERSION = createRequire(import.meta.url)("../package.json").version;
9
+ /**
10
+ * The command a prompt tells an agent to run the CLI with, as `init`'s agent rule does: the
11
+ * repository's installed copy through its package manager, or else the pinned release fetched on
12
+ * demand — a bare `npx wastech-mdlint` with nothing installed would fetch an unrelated package.
13
+ */
14
+ async function promptRunner(rootDir) {
15
+ return cliRunner({
16
+ cliInstalled: existsSync(path.join(rootDir, "node_modules", "@wastech-mdlint", "cli", "package.json")),
17
+ packageManager: await detectPackageManager(rootDir),
18
+ cliVersion: VIEWER_VERSION,
19
+ });
20
+ }
21
+ export async function findingPrompts(params) {
22
+ const { corpus } = params;
23
+ if (!corpus.documents.has(params.filePath)) {
24
+ return {
25
+ status: 404,
26
+ body: { error: "This path is not a document in the corpus." },
27
+ };
28
+ }
29
+ if (params.findings === undefined) {
30
+ return {
31
+ status: 409,
32
+ body: { error: "The linter has not finished its first run yet." },
33
+ };
34
+ }
35
+ const all = params.findings.get(params.filePath) ?? [];
36
+ // A rule can report more than one finding on a line — SIZE-001 reports each metric over budget on
37
+ // line 0 — and they are answered together, as the panel shows them together.
38
+ const findings = params.ruleId === undefined
39
+ ? all
40
+ : all.filter((finding) => finding.ruleId === params.ruleId && finding.line === params.line);
41
+ if (findings.length === 0) {
42
+ return {
43
+ status: 409,
44
+ body: {
45
+ error: "These findings are no longer reported; the page shows the latest ones once it refreshes.",
46
+ },
47
+ };
48
+ }
49
+ const config = corpus.configuration;
50
+ const ruleIds = entryRuleIds(config.config);
51
+ const ruleEntries = (config.config.rules ?? []).flatMap((entry, index) => {
52
+ const ruleId = ruleIds[index];
53
+ // An entry switched off reports nothing, so no finding's budget can come from it.
54
+ return ruleId === null || ruleId === undefined || entry.severity === "off"
55
+ ? []
56
+ : [{ ruleId, options: "options" in entry ? entry.options : undefined }];
57
+ });
58
+ const configPath = config.configPath === undefined
59
+ ? undefined
60
+ : path
61
+ .relative(corpus.rootDir, config.configPath)
62
+ .split(path.sep)
63
+ .join("/");
64
+ const runner = await promptRunner(corpus.rootDir);
65
+ return {
66
+ status: 200,
67
+ body: {
68
+ prompts: findingPromptStrategies(findings).map((strategy) => ({
69
+ strategy,
70
+ label: PROMPT_STRATEGY_LABELS[strategy],
71
+ text: buildFindingPrompt({
72
+ filePath: params.filePath,
73
+ findings,
74
+ strategy,
75
+ configPath,
76
+ runner,
77
+ ruleEntries,
78
+ }),
79
+ })),
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,23 @@
1
+ import type { ParsedDocument } from "@wastech-mdlint/core";
2
+ import type { Corpus } from "./corpus.js";
3
+ import { type RenderedDocument } from "./render-document.js";
4
+ /** How a document's links and embeds resolve: against the corpus as it is now. */
5
+ export declare function resolversFor(corpus: Corpus, document: ParsedDocument): {
6
+ resolve: (fromPath: string, target: string, isImage: boolean) => import("./corpus.js").ResolvedTarget;
7
+ resolveEmbedded: (target: string, isImage: boolean) => Promise<import("./corpus.js").ResolvedTarget>;
8
+ };
9
+ /**
10
+ * Documents rendered as plain pages. Keyed by the document object, which the corpus replaces whenever
11
+ * the file changes, so an edit misses the cache without any explicit invalidation. Link targets also
12
+ * depend on which other files exist, so a change to the set of documents starts a fresh cache
13
+ * (`clear`), and a document the corpus reports changed without replacing it — a file it links to
14
+ * appeared or went — is dropped from it (`forget`).
15
+ */
16
+ export declare class RenderCache {
17
+ private readonly corpus;
18
+ private rendered;
19
+ constructor(corpus: Corpus);
20
+ render(document: ParsedDocument): Promise<RenderedDocument>;
21
+ forget(document: ParsedDocument): void;
22
+ clear(): void;
23
+ }
@@ -0,0 +1,38 @@
1
+ import { renderDocument } from "./render-document.js";
2
+ /** How a document's links and embeds resolve: against the corpus as it is now. */
3
+ export function resolversFor(corpus, document) {
4
+ return {
5
+ resolve: (fromPath, target, isImage) => corpus.resolve(fromPath, target, isImage),
6
+ resolveEmbedded: (target, isImage) => corpus.resolveEmbedded(document, target, isImage),
7
+ };
8
+ }
9
+ /**
10
+ * Documents rendered as plain pages. Keyed by the document object, which the corpus replaces whenever
11
+ * the file changes, so an edit misses the cache without any explicit invalidation. Link targets also
12
+ * depend on which other files exist, so a change to the set of documents starts a fresh cache
13
+ * (`clear`), and a document the corpus reports changed without replacing it — a file it links to
14
+ * appeared or went — is dropped from it (`forget`).
15
+ */
16
+ export class RenderCache {
17
+ corpus;
18
+ rendered = new WeakMap();
19
+ constructor(corpus) {
20
+ this.corpus = corpus;
21
+ }
22
+ render(document) {
23
+ let pending = this.rendered.get(document);
24
+ if (pending === undefined) {
25
+ pending = renderDocument(document, resolversFor(this.corpus, document));
26
+ this.rendered.set(document, pending);
27
+ // A failed render is not cached, so fixing the file (or a transient read error) recovers.
28
+ pending.catch(() => this.rendered.delete(document));
29
+ }
30
+ return pending;
31
+ }
32
+ forget(document) {
33
+ this.rendered.delete(document);
34
+ }
35
+ clear() {
36
+ this.rendered = new WeakMap();
37
+ }
38
+ }