@wastech-mdlint/viewer 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -2
  3. package/assets/favicon.ico +0 -0
  4. package/assets/viewer.css +4184 -0
  5. package/dist/asset-routes.d.ts +4 -0
  6. package/dist/asset-routes.js +107 -0
  7. package/dist/blame.d.ts +8 -0
  8. package/dist/blame.js +54 -0
  9. package/dist/branch-changes.d.ts +8 -0
  10. package/dist/branch-changes.js +49 -0
  11. package/dist/candidate-lint-worker.d.ts +1 -0
  12. package/dist/candidate-lint-worker.js +67 -0
  13. package/dist/candidate-lint.d.ts +39 -0
  14. package/dist/candidate-lint.js +115 -0
  15. package/dist/change-banner.d.ts +38 -0
  16. package/dist/change-banner.js +108 -0
  17. package/dist/change-diff.d.ts +23 -0
  18. package/dist/change-diff.js +140 -0
  19. package/dist/change-marks.d.ts +32 -0
  20. package/dist/change-marks.js +363 -0
  21. package/dist/changes-article.d.ts +15 -0
  22. package/dist/changes-article.js +244 -0
  23. package/dist/changes-routes.d.ts +12 -0
  24. package/dist/changes-routes.js +271 -0
  25. package/dist/changes.d.ts +113 -0
  26. package/dist/changes.js +324 -0
  27. package/dist/cli-args.d.ts +21 -0
  28. package/dist/cli-args.js +93 -0
  29. package/dist/client/banner-height.d.ts +1 -0
  30. package/dist/client/banner-height.js +33 -0
  31. package/dist/client/change-marks.d.ts +1 -0
  32. package/dist/client/change-marks.js +56 -0
  33. package/dist/client/changes.d.ts +1 -0
  34. package/dist/client/changes.js +183 -0
  35. package/dist/client/chart-option.d.ts +3 -0
  36. package/dist/client/chart-option.js +76 -0
  37. package/dist/client/charts.d.ts +1 -0
  38. package/dist/client/charts.js +54 -0
  39. package/dist/client/clipboard.d.ts +1 -0
  40. package/dist/client/clipboard.js +43 -0
  41. package/dist/client/code-copy.d.ts +1 -0
  42. package/dist/client/code-copy.js +29 -0
  43. package/dist/client/column-resize.d.ts +1 -0
  44. package/dist/client/column-resize.js +120 -0
  45. package/dist/client/config-dom.d.ts +13 -0
  46. package/dist/client/config-dom.js +47 -0
  47. package/dist/client/config-draft.d.ts +73 -0
  48. package/dist/client/config-draft.js +218 -0
  49. package/dist/client/config-editor.d.ts +3 -0
  50. package/dist/client/config-editor.js +677 -0
  51. package/dist/client/config-fields.d.ts +35 -0
  52. package/dist/client/config-fields.js +339 -0
  53. package/dist/client/config-json.d.ts +18 -0
  54. package/dist/client/config-json.js +96 -0
  55. package/dist/client/config-preview.d.ts +9 -0
  56. package/dist/client/config-preview.js +91 -0
  57. package/dist/client/config-rules.d.ts +14 -0
  58. package/dist/client/config-rules.js +202 -0
  59. package/dist/client/config-types.d.ts +91 -0
  60. package/dist/client/config-types.js +4 -0
  61. package/dist/client/copy-path.d.ts +1 -0
  62. package/dist/client/copy-path.js +32 -0
  63. package/dist/client/dashboard-requests.d.ts +11 -0
  64. package/dist/client/dashboard-requests.js +28 -0
  65. package/dist/client/dashboard.d.ts +1 -0
  66. package/dist/client/dashboard.js +119 -0
  67. package/dist/client/diagnostics.d.ts +1 -0
  68. package/dist/client/diagnostics.js +164 -0
  69. package/dist/client/diagrams.d.ts +1 -0
  70. package/dist/client/diagrams.js +81 -0
  71. package/dist/client/diff-expand.d.ts +1 -0
  72. package/dist/client/diff-expand.js +42 -0
  73. package/dist/client/display-settings-model.d.ts +74 -0
  74. package/dist/client/display-settings-model.js +229 -0
  75. package/dist/client/display-settings.d.ts +1 -0
  76. package/dist/client/display-settings.js +411 -0
  77. package/dist/client/echarts.d.ts +30 -0
  78. package/dist/client/echarts.js +14 -0
  79. package/dist/client/graph-layout.d.ts +88 -0
  80. package/dist/client/graph-layout.js +198 -0
  81. package/dist/client/graph.d.ts +2 -0
  82. package/dist/client/graph.js +566 -0
  83. package/dist/client/history.d.ts +21 -0
  84. package/dist/client/history.js +86 -0
  85. package/dist/client/live-reload-model.d.ts +28 -0
  86. package/dist/client/live-reload-model.js +52 -0
  87. package/dist/client/live-reload.d.ts +9 -0
  88. package/dist/client/live-reload.js +138 -0
  89. package/dist/client/menu.d.ts +14 -0
  90. package/dist/client/menu.js +176 -0
  91. package/dist/client/navigation-feedback.d.ts +1 -0
  92. package/dist/client/navigation-feedback.js +141 -0
  93. package/dist/client/open-config.d.ts +1 -0
  94. package/dist/client/open-config.js +48 -0
  95. package/dist/client/outline-spy.d.ts +1 -0
  96. package/dist/client/outline-spy.js +60 -0
  97. package/dist/client/prompts.d.ts +7 -0
  98. package/dist/client/prompts.js +65 -0
  99. package/dist/client/review.d.ts +1 -0
  100. package/dist/client/review.js +379 -0
  101. package/dist/client/search.d.ts +1 -0
  102. package/dist/client/search.js +199 -0
  103. package/dist/client/segmented.d.ts +13 -0
  104. package/dist/client/segmented.js +70 -0
  105. package/dist/client/source-lines.d.ts +16 -0
  106. package/dist/client/source-lines.js +47 -0
  107. package/dist/client/theme-toggle.d.ts +10 -0
  108. package/dist/client/theme-toggle.js +100 -0
  109. package/dist/client/theme.d.ts +0 -0
  110. package/dist/client/theme.js +50 -0
  111. package/dist/client/tree-marks.d.ts +31 -0
  112. package/dist/client/tree-marks.js +102 -0
  113. package/dist/client/tree-state.d.ts +0 -0
  114. package/dist/client/tree-state.js +179 -0
  115. package/dist/client/viewer.d.ts +1 -0
  116. package/dist/client/viewer.js +53 -0
  117. package/dist/config-editor.d.ts +166 -0
  118. package/dist/config-editor.js +378 -0
  119. package/dist/config-routes.d.ts +18 -0
  120. package/dist/config-routes.js +213 -0
  121. package/dist/context-view.d.ts +41 -0
  122. package/dist/context-view.js +295 -0
  123. package/dist/corpus-routes.d.ts +31 -0
  124. package/dist/corpus-routes.js +258 -0
  125. package/dist/corpus.d.ts +134 -0
  126. package/dist/corpus.js +653 -0
  127. package/dist/dashboard-article.d.ts +14 -0
  128. package/dist/dashboard-article.js +290 -0
  129. package/dist/dashboard.d.ts +94 -0
  130. package/dist/dashboard.js +468 -0
  131. package/dist/diagnostics.d.ts +23 -0
  132. package/dist/diagnostics.js +44 -0
  133. package/dist/document-routes.d.ts +39 -0
  134. package/dist/document-routes.js +396 -0
  135. package/dist/event-hub.d.ts +39 -0
  136. package/dist/event-hub.js +120 -0
  137. package/dist/findings-panel.d.ts +4 -0
  138. package/dist/findings-panel.js +74 -0
  139. package/dist/folder-page.d.ts +8 -0
  140. package/dist/folder-page.js +74 -0
  141. package/dist/graph-view.d.ts +66 -0
  142. package/dist/graph-view.js +236 -0
  143. package/dist/highlight.d.ts +24 -0
  144. package/dist/highlight.js +100 -0
  145. package/dist/http-send.d.ts +9 -0
  146. package/dist/http-send.js +53 -0
  147. package/dist/index.d.ts +2 -0
  148. package/dist/index.js +72 -0
  149. package/dist/open-file.d.ts +36 -0
  150. package/dist/open-file.js +87 -0
  151. package/dist/page-tree.d.ts +3 -0
  152. package/dist/page-tree.js +105 -0
  153. package/dist/page.d.ts +73 -0
  154. package/dist/page.js +478 -0
  155. package/dist/prompts.d.ts +25 -0
  156. package/dist/prompts.js +82 -0
  157. package/dist/render-cache.d.ts +23 -0
  158. package/dist/render-cache.js +38 -0
  159. package/dist/render-document.d.ts +60 -0
  160. package/dist/render-document.js +440 -0
  161. package/dist/request-path.d.ts +17 -0
  162. package/dist/request-path.js +51 -0
  163. package/dist/review-context.d.ts +24 -0
  164. package/dist/review-context.js +109 -0
  165. package/dist/review-queries.d.ts +26 -0
  166. package/dist/review-queries.js +69 -0
  167. package/dist/review-state.d.ts +106 -0
  168. package/dist/review-state.js +251 -0
  169. package/dist/review-store.d.ts +145 -0
  170. package/dist/review-store.js +530 -0
  171. package/dist/router.d.ts +28 -0
  172. package/dist/router.js +294 -0
  173. package/dist/search.d.ts +22 -0
  174. package/dist/search.js +164 -0
  175. package/dist/security.d.ts +95 -0
  176. package/dist/security.js +196 -0
  177. package/dist/server.d.ts +21 -0
  178. package/dist/server.js +224 -0
  179. package/dist/speculation-rules.d.ts +4 -0
  180. package/dist/speculation-rules.js +49 -0
  181. package/dist/state-dir.d.ts +6 -0
  182. package/dist/state-dir.js +31 -0
  183. package/dist/tree-marks.d.ts +49 -0
  184. package/dist/tree-marks.js +108 -0
  185. package/dist/viewer-context.d.ts +37 -0
  186. package/dist/viewer-context.js +74 -0
  187. package/dist/watch.d.ts +41 -0
  188. package/dist/watch.js +135 -0
  189. package/package.json +57 -4
@@ -0,0 +1,108 @@
1
+ import { h } from "hastscript";
2
+ import { markReview } from "./review-state.js";
3
+ const STATUS_NAMES = {
4
+ A: "Added in this branch",
5
+ M: "Modified in this branch",
6
+ D: "Deleted in this branch",
7
+ R: "Renamed in this branch",
8
+ };
9
+ const REVIEW_NAMES = {
10
+ unreviewed: "Not reviewed yet",
11
+ changed: "Changed since your review",
12
+ };
13
+ /**
14
+ * The marks as the server holds them now, without asking git: the review is the one kept from the
15
+ * last request that needed it. A document's page has waited for a current one before it gets here;
16
+ * any other page takes what is kept rather than wait for git, and when nothing is kept, its script
17
+ * asks for the changes as the page opens.
18
+ */
19
+ export async function treeMarksOf(state) {
20
+ const findings = state.diagnostics.ready
21
+ ? state.diagnostics.summary()
22
+ : undefined;
23
+ const changes = state.review === undefined
24
+ ? undefined
25
+ : await changeMarksOf(state.review, state.reviewStore);
26
+ const current = [];
27
+ if (findings !== undefined) {
28
+ current.push("findings");
29
+ }
30
+ if (changes !== undefined && state.watch) {
31
+ current.push("changes");
32
+ }
33
+ return { findings, changes, current };
34
+ }
35
+ async function changeMarksOf(review, reviewStore) {
36
+ const marks = reviewStore === undefined
37
+ ? undefined
38
+ : markReview(review, await reviewStore.read());
39
+ const files = new Map();
40
+ for (const entry of review.documents) {
41
+ const mark = marks?.ok === true ? marks.documents.get(entry.path) : undefined;
42
+ files.set(entry.path, {
43
+ status: entry.status,
44
+ ...(entry.oldPath === undefined ? {} : { oldPath: entry.oldPath }),
45
+ review: mark?.status ?? null,
46
+ });
47
+ }
48
+ return {
49
+ files,
50
+ changed: review.documents.length,
51
+ review: marks?.ok === true
52
+ ? { left: marks.left, total: review.documents.length }
53
+ : null,
54
+ };
55
+ }
56
+ /** A file's count of findings, red when any is an error. */
57
+ export function findingsBadge(counts) {
58
+ return h(`span.tree-badge${counts.errors > 0 ? ".tree-badge-error" : ""}`, {
59
+ title: [
60
+ counts.errors > 0 ? plural(counts.errors, "error") : "",
61
+ counts.warnings > 0 ? plural(counts.warnings, "warning") : "",
62
+ ]
63
+ .filter((part) => part !== "")
64
+ .join(", "),
65
+ }, String(counts.errors + counts.warnings));
66
+ }
67
+ /** The letter of what the branch did to a file, naming where a renamed one came from. */
68
+ export function changeLetter(file) {
69
+ return h(`span.tree-change.tree-change-${file.status}`, {
70
+ title: file.oldPath === undefined
71
+ ? STATUS_NAMES[file.status]
72
+ : `${STATUS_NAMES[file.status]}, from ${file.oldPath}`,
73
+ }, file.status);
74
+ }
75
+ /** The dot beside a changed file left to review: never reviewed, or changed since. */
76
+ export function reviewDot(status) {
77
+ if (status === null || status === "reviewed") {
78
+ return undefined;
79
+ }
80
+ return h(`span.tree-review-dot.tree-review-${status}`, {
81
+ role: "img",
82
+ ariaLabel: REVIEW_NAMES[status],
83
+ title: REVIEW_NAMES[status],
84
+ });
85
+ }
86
+ /**
87
+ * The count on the header's Changes section: what is left against everything that changed, the queue
88
+ * a reviewer works down, or only how many changed where marks cannot be read. Nothing when nothing
89
+ * changed.
90
+ */
91
+ export function changesCount(marks) {
92
+ if (marks.changed === 0) {
93
+ return undefined;
94
+ }
95
+ const { review } = marks;
96
+ if (review === null) {
97
+ return h("span.segment-count", { title: `${marks.changed} changed ${documents(marks.changed)}` }, String(marks.changed));
98
+ }
99
+ return h(`span.segment-count${review.left === 0 ? ".is-done" : ""}`, {
100
+ title: `${review.left} of ${review.total} changed ${documents(review.total)} left to review`,
101
+ }, `${review.left}/${review.total}`);
102
+ }
103
+ function documents(count) {
104
+ return count === 1 ? "document" : "documents";
105
+ }
106
+ function plural(count, noun) {
107
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
108
+ }
@@ -0,0 +1,37 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import type { GitRepository } from "@wastech-mdlint/core";
3
+ import type { ChangeTracker } from "./changes.js";
4
+ import type { Corpus } from "./corpus.js";
5
+ import type { Diagnostics } from "./diagnostics.js";
6
+ import type { EventHub } from "./event-hub.js";
7
+ import type { Launcher } from "./open-file.js";
8
+ import { type PageParams } from "./page.js";
9
+ import type { RenderCache } from "./render-cache.js";
10
+ import type { SinceReview } from "./review-state.js";
11
+ import type { ReviewStore } from "./review-store.js";
12
+ import type { DocumentSearch } from "./search.js";
13
+ export declare const NO_CHANGES = "Changes compares the documents with a base branch, and this directory is not in a git work tree, or git is not on PATH.";
14
+ /** A page as a route describes it; the parts every page shares are filled in by `page`. */
15
+ export type PageBody = Omit<PageParams, "siteName" | "documentPaths" | "showChanges" | "marks" | "eventSequence" | "findingsPanel">;
16
+ export type ViewerContext = {
17
+ corpus: Corpus;
18
+ changes: ChangeTracker | undefined;
19
+ git: GitRepository | undefined;
20
+ reviewStore: ReviewStore | undefined;
21
+ sinceReviews: SinceReview | undefined;
22
+ search: DocumentSearch;
23
+ diagnostics: Diagnostics;
24
+ renders: RenderCache;
25
+ events: EventHub;
26
+ allowConfigWrites: boolean;
27
+ showAbsolutePaths: boolean;
28
+ watch: boolean;
29
+ launch: Launcher;
30
+ openRefused: string | undefined;
31
+ changeToken: string | undefined;
32
+ page(params: PageBody): Promise<string>;
33
+ sendNotFound(request: IncomingMessage, response: ServerResponse, message: string): Promise<void>;
34
+ absolutePathOf(relPath: string): string | undefined;
35
+ relint(): void;
36
+ };
37
+ export declare function viewerContext(state: Omit<ViewerContext, "page" | "sendNotFound" | "absolutePathOf" | "relint">): ViewerContext;
@@ -0,0 +1,74 @@
1
+ import path from "node:path";
2
+ import { h } from "hastscript";
3
+ import { renderFindingsPanel } from "./findings-panel.js";
4
+ import { sendHtml } from "./http-send.js";
5
+ import { renderPage } from "./page.js";
6
+ import { treeMarksOf } from "./tree-marks.js";
7
+ // What every route of one running viewer shares: the corpus and what is known about it, the git
8
+ // review, the switches the command line set, and the few actions that touch all of it. The server
9
+ // builds it once and hands the same object to each group of routes, so a route reads state it is
10
+ // given rather than state it can reach, and no route keeps a second copy of the corpus or the review.
11
+ export const NO_CHANGES = "Changes compares the documents with a base branch, and this directory is not in a git work tree, or git is not on PATH.";
12
+ // A document page's findings panel, read with the tree's counts so the two never disagree. Nothing
13
+ // until the first lint is in: the page then says so by leaving findings out of the tree's current
14
+ // marks, and its script asks for both once the lint finishes.
15
+ function findingsPanelOf(state, relPath) {
16
+ const document = relPath === undefined ? undefined : state.corpus.documents.get(relPath);
17
+ return document === undefined || !state.diagnostics.ready
18
+ ? undefined
19
+ : renderFindingsPanel(document.path, state.diagnostics.findings(document));
20
+ }
21
+ export function viewerContext(state) {
22
+ const siteName = path.basename(state.corpus.rootDir) || "repository";
23
+ const page = async (params) => {
24
+ // Read before the marks, so an event sent while they are read is one the page is sent again
25
+ // rather than one it claims to include. The panel is read in the same turn as the tree's counts,
26
+ // which `treeMarksOf` takes before it waits for anything, so no lint can land between the two.
27
+ const eventSequence = state.events.sequence;
28
+ const findingsPanel = findingsPanelOf(state, params.documentPath);
29
+ const marks = await treeMarksOf({
30
+ diagnostics: state.diagnostics,
31
+ review: state.changes?.cached(true),
32
+ reviewStore: state.reviewStore,
33
+ watch: state.watch,
34
+ });
35
+ return renderPage({
36
+ ...params,
37
+ siteName,
38
+ documentPaths: [...state.corpus.documents.keys()],
39
+ showChanges: state.changes !== undefined,
40
+ showReview: state.reviewStore !== undefined,
41
+ marks,
42
+ eventSequence,
43
+ findingsPanel,
44
+ });
45
+ };
46
+ return {
47
+ ...state,
48
+ page,
49
+ async sendNotFound(request, response, message) {
50
+ sendHtml(response, 404, await page({
51
+ title: "Not found",
52
+ article: [h("h1", "Not found"), h("p", message)],
53
+ }), request.method === "HEAD");
54
+ },
55
+ absolutePathOf(relPath) {
56
+ return state.showAbsolutePaths
57
+ ? path.join(state.corpus.rootDir, ...relPath.split("/"))
58
+ : undefined;
59
+ },
60
+ // Runs on the event loop: `lintContext` is synchronous and takes a few hundred milliseconds on a
61
+ // corpus of hundreds of documents, which is cheaper than handing the corpus to a worker and short
62
+ // enough that the requests it holds up do not notice.
63
+ relint() {
64
+ try {
65
+ state.diagnostics.update(state.corpus.lint());
66
+ state.events.broadcast("diagnostics", {});
67
+ }
68
+ catch (error) {
69
+ // The previous results stay; the terminal says why they did not refresh.
70
+ process.stderr.write(`wastech-mdlint-viewer: linting failed: ${error.message}\n`);
71
+ }
72
+ },
73
+ };
74
+ }
@@ -0,0 +1,41 @@
1
+ export declare function isRelevantChange(relPath: string): boolean;
2
+ /**
3
+ * Whether a file git changed under its directory can move what a review compares: this work tree's
4
+ * `HEAD` (a commit, a checkout, a reset), a ref (a branch moved, a fetch), or the packed refs a `gc`
5
+ * or a fetch rewrites. Lock files are git's staging copies, renamed over the real file when done, and
6
+ * the rename is what reports the change.
7
+ */
8
+ export declare function isRefChange(relPath: string): boolean;
9
+ /**
10
+ * Watch where git records what `HEAD` and the branches point at, and call `onMove` once per burst of
11
+ * changes. Apart from the corpus watcher, which never looks inside `.git`: a commit, a `reset --soft`
12
+ * or a fetch moves what a review compares without changing a single file of the work tree, and an
13
+ * open Changes page should show that without waiting for the reader to reload it.
14
+ *
15
+ * `gitDir` holds this work tree's own `HEAD`; `commonDir` the refs every worktree of the clone shares.
16
+ * They are the same directory in the main work tree. Only the top of each is watched, plus `refs/`
17
+ * recursively, so the objects a commit or a fetch writes by the thousand never reach the callback.
18
+ *
19
+ * `onLost` is called when one of those watches could not start or has ended, after which a move
20
+ * there would go unreported: the review is kept without asking git only while this watch holds.
21
+ */
22
+ export declare function watchGitRefs(dirs: {
23
+ gitDir: string;
24
+ commonDir: string;
25
+ }, handlers: {
26
+ onMove: () => void;
27
+ onLost: () => void;
28
+ }, debounceMs?: number): () => void;
29
+ /**
30
+ * Watch the repository and report changed repo-relative POSIX paths in batches.
31
+ *
32
+ * Batched because one save is several events: editors write a temp file and rename it over the
33
+ * original, and some platforms report both a `rename` and a `change`. Reading the file on the first
34
+ * event can catch it half-written, or not there at all for an instant.
35
+ *
36
+ * Node's recursive `fs.watch` is used directly. On Windows and macOS it is one native subscription
37
+ * for the whole tree; on Linux Node emulates it with one inotify watch per directory, which on a
38
+ * repository with a large `node_modules` can exhaust the per-user watch limit — the known cost of
39
+ * keeping this dependency-free.
40
+ */
41
+ export declare function watchRepository(rootDir: string, onChange: (paths: string[]) => void, debounceMs?: number): () => void;
package/dist/watch.js ADDED
@@ -0,0 +1,135 @@
1
+ import { realpathSync, watch } from "node:fs";
2
+ import path from "node:path";
3
+ // Which paths reach the corpus. Almost any can change what the viewer shows, and none can be judged
4
+ // by its name alone: a config passed with `--config` may be called anything; an image a document
5
+ // embeds is no Markdown file; and a folder moved, renamed or deleted arrives as one event for the
6
+ // folder, with nothing for the documents inside it. So the corpus decides, against core's scope.
7
+ // Only git's own database and installed dependencies are dropped here, unseen: neither is ever part
8
+ // of a corpus, and both churn by the thousand on an install or a checkout.
9
+ export function isRelevantChange(relPath) {
10
+ const segments = relPath.split("/");
11
+ return !segments.includes("node_modules") && !segments.includes(".git");
12
+ }
13
+ /**
14
+ * Whether a file git changed under its directory can move what a review compares: this work tree's
15
+ * `HEAD` (a commit, a checkout, a reset), a ref (a branch moved, a fetch), or the packed refs a `gc`
16
+ * or a fetch rewrites. Lock files are git's staging copies, renamed over the real file when done, and
17
+ * the rename is what reports the change.
18
+ */
19
+ export function isRefChange(relPath) {
20
+ if (relPath.endsWith(".lock")) {
21
+ return false;
22
+ }
23
+ return (relPath === "HEAD" ||
24
+ relPath === "packed-refs" ||
25
+ relPath === "refs" ||
26
+ relPath.startsWith("refs/"));
27
+ }
28
+ /**
29
+ * Watch where git records what `HEAD` and the branches point at, and call `onMove` once per burst of
30
+ * changes. Apart from the corpus watcher, which never looks inside `.git`: a commit, a `reset --soft`
31
+ * or a fetch moves what a review compares without changing a single file of the work tree, and an
32
+ * open Changes page should show that without waiting for the reader to reload it.
33
+ *
34
+ * `gitDir` holds this work tree's own `HEAD`; `commonDir` the refs every worktree of the clone shares.
35
+ * They are the same directory in the main work tree. Only the top of each is watched, plus `refs/`
36
+ * recursively, so the objects a commit or a fetch writes by the thousand never reach the callback.
37
+ *
38
+ * `onLost` is called when one of those watches could not start or has ended, after which a move
39
+ * there would go unreported: the review is kept without asking git only while this watch holds.
40
+ */
41
+ export function watchGitRefs(dirs, handlers, debounceMs = 150) {
42
+ let timer;
43
+ const watchers = [];
44
+ const schedule = () => {
45
+ clearTimeout(timer);
46
+ timer = setTimeout(handlers.onMove, debounceMs);
47
+ };
48
+ const watchDir = (dir, options, toRelPath) => {
49
+ try {
50
+ // The native realpath, for the reason `watchRepository` gives: libuv aborts the process when an
51
+ // event's long path does not match a watched 8.3 short name.
52
+ const watched = watch(realpathSync.native(dir), options, (_event, filename) => {
53
+ if (filename !== null && isRefChange(toRelPath(filename))) {
54
+ schedule();
55
+ }
56
+ });
57
+ watched.on("error", () => {
58
+ // A ref folder git removed and recreated (a `pack-refs`) ends its watch. The review is then
59
+ // checked against git on every request again, so a page is at worst stale until the next one.
60
+ watched.close();
61
+ handlers.onLost();
62
+ });
63
+ watchers.push(watched);
64
+ }
65
+ catch {
66
+ // A folder that cannot be watched — gone, or not readable — reports nothing, so the review is
67
+ // checked against git on every request.
68
+ handlers.onLost();
69
+ }
70
+ };
71
+ const posix = (filename) => filename.split(path.sep).join("/");
72
+ watchDir(dirs.gitDir, { recursive: false }, posix);
73
+ if (dirs.commonDir !== dirs.gitDir) {
74
+ watchDir(dirs.commonDir, { recursive: false }, posix);
75
+ }
76
+ watchDir(path.join(dirs.commonDir, "refs"), { recursive: true }, (filename) => `refs/${posix(filename)}`);
77
+ return () => {
78
+ clearTimeout(timer);
79
+ for (const watched of watchers) {
80
+ watched.close();
81
+ }
82
+ };
83
+ }
84
+ /**
85
+ * Watch the repository and report changed repo-relative POSIX paths in batches.
86
+ *
87
+ * Batched because one save is several events: editors write a temp file and rename it over the
88
+ * original, and some platforms report both a `rename` and a `change`. Reading the file on the first
89
+ * event can catch it half-written, or not there at all for an instant.
90
+ *
91
+ * Node's recursive `fs.watch` is used directly. On Windows and macOS it is one native subscription
92
+ * for the whole tree; on Linux Node emulates it with one inotify watch per directory, which on a
93
+ * repository with a large `node_modules` can exhaust the per-user watch limit — the known cost of
94
+ * keeping this dependency-free.
95
+ */
96
+ export function watchRepository(rootDir, onChange, debounceMs = 120) {
97
+ const pending = new Set();
98
+ let timer;
99
+ let watcher;
100
+ try {
101
+ // The native realpath, because on Windows it also expands 8.3 short names (`C:\Users\VLADIM~1`,
102
+ // which is what `os.tmpdir()` and some shells hand out). libuv compares each event's long path
103
+ // against the watched directory's path as given and aborts the whole process on a mismatch — a
104
+ // C assertion no JavaScript handler can catch.
105
+ const watchedDir = realpathSync.native(rootDir);
106
+ watcher = watch(watchedDir, { recursive: true }, (_event, filename) => {
107
+ // Some platforms omit the name for an event; there is nothing to map it to.
108
+ if (filename === null) {
109
+ return;
110
+ }
111
+ const relPath = filename.split(path.sep).join("/");
112
+ if (!isRelevantChange(relPath)) {
113
+ return;
114
+ }
115
+ pending.add(relPath);
116
+ clearTimeout(timer);
117
+ timer = setTimeout(() => {
118
+ const batch = [...pending];
119
+ pending.clear();
120
+ onChange(batch);
121
+ }, debounceMs);
122
+ });
123
+ }
124
+ catch (error) {
125
+ process.stderr.write(`wastech-mdlint-viewer: watching for changes is unavailable (${error.message}); pages will not reload on their own.\n`);
126
+ return () => { };
127
+ }
128
+ watcher.on("error", (error) => {
129
+ process.stderr.write(`wastech-mdlint-viewer: watching for changes stopped (${error.message}); pages will not reload on their own.\n`);
130
+ });
131
+ return () => {
132
+ clearTimeout(timer);
133
+ watcher.close();
134
+ };
135
+ }
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@wastech-mdlint/viewer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0",
4
+ "description": "Local web viewer for a repository's Markdown: the corpus wastech-mdlint lints, rendered with diagrams, charts, highlighted code, backlinks and a link graph, reloading as files change.",
5
+ "keywords": [
6
+ "markdown",
7
+ "viewer",
8
+ "preview",
9
+ "documentation",
10
+ "docs",
11
+ "mermaid",
12
+ "live-reload",
13
+ "echarts",
14
+ "graph"
15
+ ],
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/VladimirMakarevich/wastech-mdlint.git",
20
+ "directory": "packages/viewer"
21
+ },
22
+ "type": "module",
23
+ "bin": {
24
+ "wastech-mdlint-viewer": "./dist/index.js"
25
+ },
26
+ "engines": {
27
+ "node": ">=24.17.0"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "assets",
32
+ "LICENSE",
33
+ "README.md"
34
+ ],
35
+ "scripts": {
36
+ "build": "tsc -b",
37
+ "test": "vitest run"
38
+ },
39
+ "dependencies": {
40
+ "@types/hast": "^3.0.5",
41
+ "@wastech-mdlint/core": "0.4.0",
42
+ "echarts": "^6.1.0",
43
+ "hast-util-raw": "^9.2.1",
44
+ "hast-util-sanitize": "^5.0.2",
45
+ "hast-util-to-html": "^9.0.7",
46
+ "hastscript": "^9.0.3",
47
+ "mdast-util-to-hast": "^13.2.2",
48
+ "mermaid": "^12.3.0",
49
+ "micromark-util-sanitize-uri": "^2.0.1",
50
+ "minisearch": "^7.2.0",
51
+ "shiki": "^4.6.0",
52
+ "unist-util-visit": "^5.2.0",
53
+ "vfile": "^6.0.3"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true
58
+ }
59
+ }