@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,60 @@
|
|
|
1
|
+
import { type HeadChanges, type ParsedDocument } from "@wastech-mdlint/core";
|
|
2
|
+
import { type BlockChange } from "./change-marks.js";
|
|
3
|
+
import { type ResolvedTarget } from "./corpus.js";
|
|
4
|
+
export type RenderedDocument = {
|
|
5
|
+
html: string;
|
|
6
|
+
title: string;
|
|
7
|
+
frontmatterChange?: BlockChange;
|
|
8
|
+
};
|
|
9
|
+
/** What the branch did to a document, for marking it on its rendered page. */
|
|
10
|
+
export type DocumentChange = {
|
|
11
|
+
changes: HeadChanges;
|
|
12
|
+
basePath: string;
|
|
13
|
+
baseText: string;
|
|
14
|
+
};
|
|
15
|
+
export type TargetResolver = (fromPath: string, target: string, isImage: boolean) => ResolvedTarget;
|
|
16
|
+
export type TargetResolvers = {
|
|
17
|
+
resolve: TargetResolver;
|
|
18
|
+
resolveEmbedded: (target: string, isImage: boolean) => Promise<ResolvedTarget>;
|
|
19
|
+
};
|
|
20
|
+
/** The URL of a document page. */
|
|
21
|
+
export declare function documentHref(relPath: string): string;
|
|
22
|
+
/** The URL of the listing of a corpus folder, `""` being the repository's root. */
|
|
23
|
+
export declare function folderHref(relPath: string): string;
|
|
24
|
+
/** The URL of a non-Markdown repository file a document links to. */
|
|
25
|
+
export declare function fileHref(relPath: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Render one corpus document to sanitized HTML.
|
|
28
|
+
*
|
|
29
|
+
* The tree comes from core's `parseDocumentWithTree`, the same parse that produced the facts the
|
|
30
|
+
* linter checks, so heading ids are core's slugs taken verbatim rather than recomputed: an anchor
|
|
31
|
+
* REF-002 accepts is an anchor that exists on this page.
|
|
32
|
+
*
|
|
33
|
+
* Order matters. Author content — Markdown and the raw HTML inside it — is sanitized first, as one
|
|
34
|
+
* tree. Only then are code blocks replaced with Shiki's output and the Mermaid and chart
|
|
35
|
+
* placeholders, because that markup is the viewer's own: Shiki colors tokens with `style` attributes
|
|
36
|
+
* the sanitizer would (rightly) strip from an author, and it escapes the code text itself.
|
|
37
|
+
*
|
|
38
|
+
* With `change`, the blocks the branch changed are marked and the ones it removed are put back,
|
|
39
|
+
* folded, where they stood. The base side goes through the same pipeline, so a removed block is
|
|
40
|
+
* sanitized like any other, and its links resolve against the current corpus: a link that only
|
|
41
|
+
* worked at the base shows as missing, which is what a reviewer needs to learn about it.
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* What the right margin says about the source lines from `start` to `end`, beside the number of the
|
|
45
|
+
* first: a short `text` and a longer `title` a pointer shows. `undefined` adds nothing.
|
|
46
|
+
*/
|
|
47
|
+
export type LineAnnotator = (start: number, end: number) => {
|
|
48
|
+
text: string;
|
|
49
|
+
title: string;
|
|
50
|
+
} | undefined;
|
|
51
|
+
export declare function renderDocument(document: ParsedDocument, resolvers: TargetResolvers, change?: DocumentChange, annotate?: LineAnnotator): Promise<RenderedDocument>;
|
|
52
|
+
/**
|
|
53
|
+
* The last heading at or above a 1-based source line, or `undefined` above the first heading.
|
|
54
|
+
*
|
|
55
|
+
* Search hits and lint findings carry a source line, while the rendered page only has anchors at
|
|
56
|
+
* headings, so this is the closest place on the page to send a reader for that line.
|
|
57
|
+
*/
|
|
58
|
+
export declare function headingAbove(document: ParsedDocument, line: number): ParsedDocument["headings"][number] | undefined;
|
|
59
|
+
/** A document's first top-level heading, or its file name when it has none. */
|
|
60
|
+
export declare function documentTitle(document: ParsedDocument): string;
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { raw } from "hast-util-raw";
|
|
2
|
+
import { defaultSchema, sanitize } from "hast-util-sanitize";
|
|
3
|
+
import { toHtml } from "hast-util-to-html";
|
|
4
|
+
import { h } from "hastscript";
|
|
5
|
+
import { toHast } from "mdast-util-to-hast";
|
|
6
|
+
import { normalizeUri } from "micromark-util-sanitize-uri";
|
|
7
|
+
import { visit } from "unist-util-visit";
|
|
8
|
+
import { VFile } from "vfile";
|
|
9
|
+
import { normalizeText, parseDocumentWithTree, } from "@wastech-mdlint/core";
|
|
10
|
+
import { markChanges, } from "./change-marks.js";
|
|
11
|
+
import { isExternalTarget } from "./corpus.js";
|
|
12
|
+
import { highlight, languageForFence, withCopyButton } from "./highlight.js";
|
|
13
|
+
import { encodeRepoPath } from "./request-path.js";
|
|
14
|
+
// GitHub's own allowlist, with two changes, both about DOM clobbering: an element whose `id` or
|
|
15
|
+
// `name` makes it a property of `window` or `document`, shadowing what a script reads there.
|
|
16
|
+
//
|
|
17
|
+
// `clobberPrefix` is emptied because the prefix would rename every heading and HTML anchor
|
|
18
|
+
// (`id="setup"` → `id="user-content-setup"`), and then the anchors REF-002 validates — heading slugs
|
|
19
|
+
// and `<a id>`/`<a name>` targets alike — would not exist on the page. An `id` only shadows a global
|
|
20
|
+
// read through `window.<name>`, which the viewer's scripts never do.
|
|
21
|
+
//
|
|
22
|
+
// `name` is therefore allowed on `<a>` alone, where it is an anchor and makes nothing a property of
|
|
23
|
+
// `document`. On an `<img>`, the one other tag GitHub's list lets an author write that `document`
|
|
24
|
+
// exposes by name, it would override the document's own members: `<img name="querySelector">` turns
|
|
25
|
+
// `document.querySelector` into that image, and every script on the page fails at its first call.
|
|
26
|
+
const SANITIZE_SCHEMA = {
|
|
27
|
+
...defaultSchema,
|
|
28
|
+
clobberPrefix: "",
|
|
29
|
+
attributes: {
|
|
30
|
+
...defaultSchema.attributes,
|
|
31
|
+
"*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "name"),
|
|
32
|
+
a: [...(defaultSchema.attributes?.a ?? []), "name"],
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
// Set on every link and image the Markdown writes, whose target is rewritten on the Markdown tree, so
|
|
36
|
+
// that after `raw` an `<a>` or `<img>` without it is one the author wrote as raw HTML. The sanitizer
|
|
37
|
+
// strips it like any `data-*` attribute it does not list, so it never reaches the page; an author who
|
|
38
|
+
// writes it on raw HTML only stops their own link from being rewritten.
|
|
39
|
+
const FROM_MARKDOWN = "dataViewerFromMarkdown";
|
|
40
|
+
/** The URL of a document page. */
|
|
41
|
+
export function documentHref(relPath) {
|
|
42
|
+
return `/doc/${encodeRepoPath(relPath)}`;
|
|
43
|
+
}
|
|
44
|
+
/** The URL of the listing of a corpus folder, `""` being the repository's root. */
|
|
45
|
+
export function folderHref(relPath) {
|
|
46
|
+
return relPath === "" ? "/folder/" : `/folder/${encodeRepoPath(relPath)}/`;
|
|
47
|
+
}
|
|
48
|
+
/** The URL of a non-Markdown repository file a document links to. */
|
|
49
|
+
export function fileHref(relPath) {
|
|
50
|
+
return `/file/${encodeRepoPath(relPath)}`;
|
|
51
|
+
}
|
|
52
|
+
export async function renderDocument(document, resolvers, change, annotate) {
|
|
53
|
+
const head = await renderTree(document, resolvers);
|
|
54
|
+
let frontmatterChange;
|
|
55
|
+
if (change !== undefined) {
|
|
56
|
+
const base = await renderTree({ path: change.basePath, content: change.baseText }, resolvers);
|
|
57
|
+
for (const href of base.missingHrefs) {
|
|
58
|
+
head.missingHrefs.add(href);
|
|
59
|
+
}
|
|
60
|
+
frontmatterChange = markChanges(head, base, change.changes);
|
|
61
|
+
}
|
|
62
|
+
// After the removed blocks are in, so their code is highlighted and their links marked too.
|
|
63
|
+
await replaceCodeBlocks(head.tree);
|
|
64
|
+
markLinks(head.tree, head.missingHrefs);
|
|
65
|
+
if (annotate !== undefined) {
|
|
66
|
+
// Every element that carries its source lines — the same ones the margin numbers — and none of
|
|
67
|
+
// the removed blocks put back, which carry none.
|
|
68
|
+
visit(head.tree, "element", (node) => {
|
|
69
|
+
const { dataLine, dataLineEnd } = node.properties;
|
|
70
|
+
if (dataLine === undefined || dataLineEnd === undefined) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const note = annotate(Number(dataLine), Number(dataLineEnd));
|
|
74
|
+
if (note !== undefined) {
|
|
75
|
+
node.properties.dataBlame = note.text;
|
|
76
|
+
node.properties.title = note.title;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
html: toHtml(head.tree),
|
|
82
|
+
title: documentTitle(head.parsed),
|
|
83
|
+
...(frontmatterChange === undefined ? {} : { frontmatterChange }),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// A pre-order walk meets headings in the order core recorded them, so the index pairs each node with
|
|
87
|
+
// its slug.
|
|
88
|
+
function assignHeadingIds(tree, parsed) {
|
|
89
|
+
let headingIndex = 0;
|
|
90
|
+
visit(tree, "heading", (node) => {
|
|
91
|
+
const slug = parsed.headings[headingIndex]?.slug;
|
|
92
|
+
headingIndex += 1;
|
|
93
|
+
if (slug !== undefined) {
|
|
94
|
+
node.data = {
|
|
95
|
+
...node.data,
|
|
96
|
+
hProperties: { ...node.data?.hProperties, id: slug },
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
// A reference (`[text][id]`, `![alt][id]`) becomes the link or image it stands for, with its
|
|
102
|
+
// definition's target. A definition is shared by every reference to it and does not say whether
|
|
103
|
+
// those are links or images, while a target resolves differently for each: an image never goes
|
|
104
|
+
// through the site router, which maps URLs to Markdown sources only. Core reads a reference's
|
|
105
|
+
// target from the same definition, the first one with its label, so the page and the linter still
|
|
106
|
+
// agree on it.
|
|
107
|
+
function resolveReferences(tree) {
|
|
108
|
+
const definitions = new Map();
|
|
109
|
+
visit(tree, "definition", (node) => {
|
|
110
|
+
if (!definitions.has(node.identifier)) {
|
|
111
|
+
definitions.set(node.identifier, node);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
visit(tree, (node, index, parent) => {
|
|
115
|
+
if ((node.type !== "linkReference" && node.type !== "imageReference") ||
|
|
116
|
+
parent === undefined ||
|
|
117
|
+
index === undefined) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const reference = node;
|
|
121
|
+
const definition = definitions.get(reference.identifier);
|
|
122
|
+
if (definition === undefined) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const common = {
|
|
126
|
+
url: definition.url,
|
|
127
|
+
title: definition.title,
|
|
128
|
+
position: reference.position,
|
|
129
|
+
data: reference.data,
|
|
130
|
+
};
|
|
131
|
+
parent.children[index] =
|
|
132
|
+
reference.type === "linkReference"
|
|
133
|
+
? { type: "link", children: reference.children, ...common }
|
|
134
|
+
: { type: "image", alt: reference.alt, ...common };
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
// Targets are rewritten on the Markdown tree, where `url` is still the target exactly as the linter
|
|
138
|
+
// saw it, and before `toHast` percent-encodes it. Returns the hrefs that point at nothing.
|
|
139
|
+
function rewriteMarkdownTargets(tree, relPath, resolvers) {
|
|
140
|
+
const missingHrefs = new Set();
|
|
141
|
+
visit(tree, (node) => {
|
|
142
|
+
if (node.type !== "link" && node.type !== "image") {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const target = node;
|
|
146
|
+
target.data = {
|
|
147
|
+
...target.data,
|
|
148
|
+
hProperties: { ...target.data?.hProperties, [FROM_MARKDOWN]: "" },
|
|
149
|
+
};
|
|
150
|
+
if (!isLocalTarget(target.url)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const rewritten = rewriteTarget(target.url, resolvers.resolve(relPath, target.url, node.type === "image"));
|
|
154
|
+
if (rewritten !== undefined) {
|
|
155
|
+
target.url = rewritten.url;
|
|
156
|
+
if (rewritten.missing) {
|
|
157
|
+
// Recorded in the form `toHast` will write into `href`, so the lookup after sanitizing
|
|
158
|
+
// matches a target with spaces or non-ASCII characters too.
|
|
159
|
+
missingHrefs.add(normalizeUri(rewritten.url));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return missingHrefs;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* One side's sanitized tree, every block numbered with its source lines, before the viewer's own
|
|
167
|
+
* code-block markup goes in: everything about the author's content that does not depend on what
|
|
168
|
+
* else is shown beside it.
|
|
169
|
+
*/
|
|
170
|
+
async function renderTree(document, resolvers) {
|
|
171
|
+
const { document: parsed, tree } = parseDocumentWithTree({
|
|
172
|
+
path: document.path,
|
|
173
|
+
content: document.content,
|
|
174
|
+
});
|
|
175
|
+
assignHeadingIds(tree, parsed);
|
|
176
|
+
resolveReferences(tree);
|
|
177
|
+
const missingHrefs = rewriteMarkdownTargets(tree, document.path, resolvers);
|
|
178
|
+
const hast = toHast(tree, {
|
|
179
|
+
allowDangerousHtml: true,
|
|
180
|
+
// Front matter is shown as its own card from `ParsedDocument.frontmatter`; left to the default
|
|
181
|
+
// handler, the YAML block would render as a paragraph of raw text at the top of the article.
|
|
182
|
+
handlers: { yaml: () => undefined },
|
|
183
|
+
});
|
|
184
|
+
// The file lets `raw` keep each node's place in the source as it reparses the tree; without it
|
|
185
|
+
// every position is dropped, and with them the line numbers the page shows beside each block.
|
|
186
|
+
const reparsed = raw(hast, {
|
|
187
|
+
file: new VFile({ value: document.content }),
|
|
188
|
+
});
|
|
189
|
+
await rewriteEmbeddedTargets(reparsed, resolvers, missingHrefs);
|
|
190
|
+
const clean = sanitize(reparsed, SANITIZE_SCHEMA);
|
|
191
|
+
markSourceLines(clean);
|
|
192
|
+
// Numbered as the diff numbers them: the parser counts a lone `\r` as a line ending and a byte
|
|
193
|
+
// order mark as no line at all, and so does `normalizeText`.
|
|
194
|
+
const lines = normalizeText(document.content).split("\n");
|
|
195
|
+
const yaml = tree.children[0]?.type === "yaml" ? tree.children[0] : undefined;
|
|
196
|
+
return {
|
|
197
|
+
tree: clean,
|
|
198
|
+
parsed,
|
|
199
|
+
missingHrefs,
|
|
200
|
+
blank: (line) => (lines[line - 1] ?? "").trim() === "",
|
|
201
|
+
...(yaml?.position === undefined || parsed.frontmatter === null
|
|
202
|
+
? {}
|
|
203
|
+
: {
|
|
204
|
+
frontmatter: {
|
|
205
|
+
start: yaml.position.start.line,
|
|
206
|
+
end: yaml.position.end.line,
|
|
207
|
+
raw: parsed.frontmatter.raw,
|
|
208
|
+
},
|
|
209
|
+
}),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The last heading at or above a 1-based source line, or `undefined` above the first heading.
|
|
214
|
+
*
|
|
215
|
+
* Search hits and lint findings carry a source line, while the rendered page only has anchors at
|
|
216
|
+
* headings, so this is the closest place on the page to send a reader for that line.
|
|
217
|
+
*/
|
|
218
|
+
export function headingAbove(document, line) {
|
|
219
|
+
let found;
|
|
220
|
+
for (const heading of document.headings) {
|
|
221
|
+
if (heading.line > line) {
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
found = heading;
|
|
225
|
+
}
|
|
226
|
+
return found;
|
|
227
|
+
}
|
|
228
|
+
/** A document's first top-level heading, or its file name when it has none. */
|
|
229
|
+
export function documentTitle(document) {
|
|
230
|
+
return (document.headings.find((heading) => heading.depth === 1)?.text ??
|
|
231
|
+
document.path.split("/").pop() ??
|
|
232
|
+
document.path);
|
|
233
|
+
}
|
|
234
|
+
// A bare fragment stays on this page, where its slug already matches a heading id, and an external
|
|
235
|
+
// URL leaves the repository; only the rest name something the corpus can resolve.
|
|
236
|
+
function isLocalTarget(url) {
|
|
237
|
+
return url.length > 0 && !url.startsWith("#") && !isExternalTarget(url);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Point the `<a href>` and `<img src>` an author wrote as raw HTML where the same target written as
|
|
241
|
+
* Markdown would point: a document's page or the file route, resolved from the document's own
|
|
242
|
+
* folder. Left alone, a relative target resolves against the page's address under `/doc/`, where no
|
|
243
|
+
* image or source file is ever served. Done before sanitizing, so the sanitizer still judges every
|
|
244
|
+
* URL the page ends up with.
|
|
245
|
+
*/
|
|
246
|
+
// The raw HTML elements whose target is rewritten, and the property that holds it.
|
|
247
|
+
const EMBEDDED_TARGETS = new Map([
|
|
248
|
+
["a", "href"],
|
|
249
|
+
["img", "src"],
|
|
250
|
+
]);
|
|
251
|
+
async function rewriteEmbeddedTargets(tree, resolvers, missingHrefs) {
|
|
252
|
+
const targets = [];
|
|
253
|
+
visit(tree, "element", (node) => {
|
|
254
|
+
const property = EMBEDDED_TARGETS.get(node.tagName);
|
|
255
|
+
const url = property === undefined ? undefined : node.properties[property];
|
|
256
|
+
if (property !== undefined &&
|
|
257
|
+
node.properties[FROM_MARKDOWN] === undefined &&
|
|
258
|
+
typeof url === "string" &&
|
|
259
|
+
isLocalTarget(url)) {
|
|
260
|
+
targets.push({ node, property });
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
for (const { node, property } of targets) {
|
|
264
|
+
const url = node.properties[property];
|
|
265
|
+
const rewritten = rewriteTarget(url, await resolvers.resolveEmbedded(url, property === "src"));
|
|
266
|
+
if (rewritten !== undefined) {
|
|
267
|
+
node.properties[property] = rewritten.url;
|
|
268
|
+
if (rewritten.missing && property === "href") {
|
|
269
|
+
missingHrefs.add(rewritten.url);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function rewriteTarget(url, resolved) {
|
|
275
|
+
const hashIndex = url.indexOf("#");
|
|
276
|
+
const fragment = hashIndex === -1 ? "" : url.slice(hashIndex);
|
|
277
|
+
switch (resolved.kind) {
|
|
278
|
+
case "document":
|
|
279
|
+
return {
|
|
280
|
+
url: `${documentHref(resolved.path)}${fragment}`,
|
|
281
|
+
missing: false,
|
|
282
|
+
};
|
|
283
|
+
case "file":
|
|
284
|
+
return { url: `${fileHref(resolved.path)}${fragment}`, missing: false };
|
|
285
|
+
// A listing has no headings, so a fragment would name nothing on it.
|
|
286
|
+
case "folder":
|
|
287
|
+
return { url: folderHref(resolved.path), missing: false };
|
|
288
|
+
case "missing":
|
|
289
|
+
return { url, missing: true };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Blocks that scroll sideways. A number set beside one would be cut off by its own overflow, so it
|
|
293
|
+
// goes on a wrapper around the block instead.
|
|
294
|
+
const SCROLLING_BLOCKS = new Set(["table", "pre"]);
|
|
295
|
+
/**
|
|
296
|
+
* Give each block and each list item the line it starts on in the Markdown file, as `data-line`,
|
|
297
|
+
* for the page to show beside it — so a reader can go from the page to the file, and from a
|
|
298
|
+
* finding's "line 42" to the block it is about — and the line it ends on, as `data-line-end`, so a
|
|
299
|
+
* block's whole range is known without guessing it from where the next block starts.
|
|
300
|
+
*
|
|
301
|
+
* Done on the sanitized tree: the attributes are the viewer's own, and the sanitizer has already
|
|
302
|
+
* stripped any `data-*` an author wrote, so no document can label a block with a line it is not on.
|
|
303
|
+
* A list gets no number of its own, since its first item carries the same line.
|
|
304
|
+
*/
|
|
305
|
+
function markSourceLines(tree) {
|
|
306
|
+
tree.children = tree.children.map((node) => {
|
|
307
|
+
if (node.type !== "element" ||
|
|
308
|
+
node.position === undefined ||
|
|
309
|
+
node.tagName === "ul" ||
|
|
310
|
+
node.tagName === "ol") {
|
|
311
|
+
return node;
|
|
312
|
+
}
|
|
313
|
+
const line = node.position.start.line;
|
|
314
|
+
const lineEnd = node.position.end.line;
|
|
315
|
+
if (SCROLLING_BLOCKS.has(node.tagName)) {
|
|
316
|
+
return h("div.source-line", { dataLine: line, dataLineEnd: lineEnd }, [
|
|
317
|
+
node,
|
|
318
|
+
]);
|
|
319
|
+
}
|
|
320
|
+
node.properties.dataLine = line;
|
|
321
|
+
node.properties.dataLineEnd = lineEnd;
|
|
322
|
+
return node;
|
|
323
|
+
});
|
|
324
|
+
visit(tree, "element", (node) => {
|
|
325
|
+
if (node.tagName === "li" && node.position !== undefined) {
|
|
326
|
+
node.properties.dataLine = node.position.start.line;
|
|
327
|
+
node.properties.dataLineEnd = node.position.end.line;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
async function replaceCodeBlocks(tree) {
|
|
332
|
+
const blocks = [];
|
|
333
|
+
visit(tree, "element", (node, index, parent) => {
|
|
334
|
+
if (node.tagName !== "pre" || parent === undefined || index === undefined) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const code = node.children.find((child) => child.type === "element" && child.tagName === "code");
|
|
338
|
+
if (code === undefined) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const className = code.properties.className;
|
|
342
|
+
const languageClass = Array.isArray(className)
|
|
343
|
+
? className.map(String).find((name) => name.startsWith("language-"))
|
|
344
|
+
: undefined;
|
|
345
|
+
blocks.push({
|
|
346
|
+
parent,
|
|
347
|
+
index,
|
|
348
|
+
code: textContent(code),
|
|
349
|
+
info: languageClass?.slice("language-".length),
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
for (const block of blocks) {
|
|
353
|
+
let replacement;
|
|
354
|
+
const language = block.info?.toLowerCase();
|
|
355
|
+
if (language === "echarts") {
|
|
356
|
+
replacement = chartBlock(block.code);
|
|
357
|
+
}
|
|
358
|
+
else if (language === "mermaid") {
|
|
359
|
+
// The diagram is drawn in the browser; the source stays readable as the fallback when the
|
|
360
|
+
// script is blocked or the diagram fails to parse.
|
|
361
|
+
replacement = {
|
|
362
|
+
type: "element",
|
|
363
|
+
tagName: "div",
|
|
364
|
+
properties: { className: ["mermaid-diagram"] },
|
|
365
|
+
children: [
|
|
366
|
+
{
|
|
367
|
+
type: "element",
|
|
368
|
+
tagName: "pre",
|
|
369
|
+
properties: { className: ["mermaid-source"] },
|
|
370
|
+
children: [{ type: "text", value: block.code }],
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
replacement = withCopyButton(await highlight(block.code, languageForFence(block.info)));
|
|
377
|
+
}
|
|
378
|
+
block.parent.children[block.index] = replacement;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* An `echarts` block: a chart option written as JSON, drawn in the browser like a Mermaid diagram.
|
|
383
|
+
*
|
|
384
|
+
* JSON and nothing else — not a JavaScript object literal, never a function — because the option is
|
|
385
|
+
* author content and the page must not evaluate it. The block is checked here rather than only in
|
|
386
|
+
* the browser so that a block which can never become a chart shows its reason beside the source
|
|
387
|
+
* without script, and the browser skips it.
|
|
388
|
+
*/
|
|
389
|
+
function chartBlock(code) {
|
|
390
|
+
const problem = chartOptionProblem(code);
|
|
391
|
+
return h("div.echarts-chart", { dataState: problem === undefined ? undefined : "invalid" }, [
|
|
392
|
+
problem === undefined
|
|
393
|
+
? undefined
|
|
394
|
+
: h("p.echarts-error", `Chart could not be drawn: ${problem}`),
|
|
395
|
+
h("pre.echarts-source", code),
|
|
396
|
+
]);
|
|
397
|
+
}
|
|
398
|
+
function chartOptionProblem(code) {
|
|
399
|
+
let option;
|
|
400
|
+
try {
|
|
401
|
+
option = JSON.parse(code);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
return `the option is not valid JSON: ${error.message}`;
|
|
405
|
+
}
|
|
406
|
+
return typeof option === "object" && option !== null && !Array.isArray(option)
|
|
407
|
+
? undefined
|
|
408
|
+
: "the option must be a JSON object";
|
|
409
|
+
}
|
|
410
|
+
function markLinks(tree, missingHrefs) {
|
|
411
|
+
visit(tree, "element", (node) => {
|
|
412
|
+
if (node.tagName !== "a" || typeof node.properties.href !== "string") {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const href = node.properties.href;
|
|
416
|
+
if (isExternalTarget(href)) {
|
|
417
|
+
node.properties.target = "_blank";
|
|
418
|
+
node.properties.rel = ["noopener", "noreferrer"];
|
|
419
|
+
node.properties.className = [...classNames(node), "link-external"];
|
|
420
|
+
}
|
|
421
|
+
else if (missingHrefs.has(href)) {
|
|
422
|
+
node.properties.className = [...classNames(node), "link-missing"];
|
|
423
|
+
// Missing, or a file the viewer does not serve: Markdown outside the corpus, or a path an
|
|
424
|
+
// `exclude` pattern or a `.gitignore` covers.
|
|
425
|
+
node.properties.title =
|
|
426
|
+
"The viewer has no page for this target: it does not exist, or is outside what the viewer serves";
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
function classNames(node) {
|
|
431
|
+
const value = node.properties.className;
|
|
432
|
+
return Array.isArray(value) ? value.map(String) : [];
|
|
433
|
+
}
|
|
434
|
+
function textContent(node) {
|
|
435
|
+
let text = "";
|
|
436
|
+
visit(node, "text", (child) => {
|
|
437
|
+
text += child.value;
|
|
438
|
+
});
|
|
439
|
+
return text;
|
|
440
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decode the path part of a request (everything after the route prefix) into a repo-relative POSIX
|
|
3
|
+
* path, or `undefined` when it cannot name a file inside the repository.
|
|
4
|
+
*
|
|
5
|
+
* The raw request target is decoded segment by segment rather than handed to `new URL`, because the
|
|
6
|
+
* WHATWG parser silently resolves `..` (including `%2e%2e`) and turns `\` into `/` for http URLs, so
|
|
7
|
+
* an escape attempt would arrive here already rewritten into some other in-repository path. Refusing
|
|
8
|
+
* the attempt outright is easier to reason about than serving whatever it was normalized into.
|
|
9
|
+
*
|
|
10
|
+
* `platform` is a parameter so the Windows rules are tested on every host: on Windows `\` is a
|
|
11
|
+
* separator and `:` introduces a drive or an alternate data stream (`README.md::$DATA`), so a segment
|
|
12
|
+
* carrying either can address something other than the file its text names. Elsewhere both are
|
|
13
|
+
* ordinary filename characters and stay allowed.
|
|
14
|
+
*/
|
|
15
|
+
export declare function decodeRequestPath(encoded: string, platform?: NodeJS.Platform): string | undefined;
|
|
16
|
+
/** The URL path for a repo-relative POSIX path: each segment encoded, the separators kept. */
|
|
17
|
+
export declare function encodeRepoPath(relPath: string): string;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Turning a URL path into a repository path is the one place a request reaches the filesystem, so
|
|
2
|
+
// it is a pure function with its own tests rather than a line inside a route handler. Every caller
|
|
3
|
+
// still checks the result against a set it built from the corpus before reading anything; this
|
|
4
|
+
// function exists so that a path which could only mean "outside the repository" is refused here,
|
|
5
|
+
// explicitly, instead of depending on that later lookup happening to miss.
|
|
6
|
+
/**
|
|
7
|
+
* Decode the path part of a request (everything after the route prefix) into a repo-relative POSIX
|
|
8
|
+
* path, or `undefined` when it cannot name a file inside the repository.
|
|
9
|
+
*
|
|
10
|
+
* The raw request target is decoded segment by segment rather than handed to `new URL`, because the
|
|
11
|
+
* WHATWG parser silently resolves `..` (including `%2e%2e`) and turns `\` into `/` for http URLs, so
|
|
12
|
+
* an escape attempt would arrive here already rewritten into some other in-repository path. Refusing
|
|
13
|
+
* the attempt outright is easier to reason about than serving whatever it was normalized into.
|
|
14
|
+
*
|
|
15
|
+
* `platform` is a parameter so the Windows rules are tested on every host: on Windows `\` is a
|
|
16
|
+
* separator and `:` introduces a drive or an alternate data stream (`README.md::$DATA`), so a segment
|
|
17
|
+
* carrying either can address something other than the file its text names. Elsewhere both are
|
|
18
|
+
* ordinary filename characters and stay allowed.
|
|
19
|
+
*/
|
|
20
|
+
export function decodeRequestPath(encoded, platform = process.platform) {
|
|
21
|
+
if (encoded.length === 0) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const decoded = [];
|
|
25
|
+
for (const segment of encoded.split("/")) {
|
|
26
|
+
let value;
|
|
27
|
+
try {
|
|
28
|
+
value = decodeURIComponent(segment);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
// An encoded `/` (`%2F`) inside one segment would smuggle a second level past the split above.
|
|
34
|
+
if (value === "" ||
|
|
35
|
+
value === "." ||
|
|
36
|
+
value === ".." ||
|
|
37
|
+
value.includes("/") ||
|
|
38
|
+
value.includes("\0")) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (platform === "win32" && (value.includes("\\") || value.includes(":"))) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
decoded.push(value);
|
|
45
|
+
}
|
|
46
|
+
return decoded.join("/");
|
|
47
|
+
}
|
|
48
|
+
/** The URL path for a repo-relative POSIX path: each segment encoded, the separators kept. */
|
|
49
|
+
export function encodeRepoPath(relPath) {
|
|
50
|
+
return relPath.split("/").map(encodeURIComponent).join("/");
|
|
51
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ContextDelta, type GitRepository } from "@wastech-mdlint/core";
|
|
2
|
+
import type { Review } from "./changes.js";
|
|
3
|
+
export type ReviewContext = {
|
|
4
|
+
delta: ContextDelta;
|
|
5
|
+
/** The review's documents in the order to read them, with documents given new errors moved up. */
|
|
6
|
+
order: string[];
|
|
7
|
+
/** Why a side could not be linted with its own config, when it could not. */
|
|
8
|
+
configProblems: string[];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The context delta of `review`. `worktree` is the work-tree review of the same merge-base, which
|
|
12
|
+
* `review` is itself in that mode: only it lists every document that differs from the merge-base,
|
|
13
|
+
* so only it can turn the files on disk into the merge-base's version of the corpus.
|
|
14
|
+
*
|
|
15
|
+
* In the work-tree mode the head side is the corpus as read from disk. With commits only, it is the
|
|
16
|
+
* merge-base's version with the branch's commits applied, so uncommitted edits stay out of it just
|
|
17
|
+
* as they stay out of that mode's diffs.
|
|
18
|
+
*/
|
|
19
|
+
export declare function computeReviewContext(params: {
|
|
20
|
+
repository: GitRepository;
|
|
21
|
+
rootDir: string;
|
|
22
|
+
review: Review;
|
|
23
|
+
worktree: Review;
|
|
24
|
+
}): Promise<ReviewContext>;
|