@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,81 @@
|
|
|
1
|
+
import { effectiveTheme, onThemeChange } from "./theme-toggle.js";
|
|
2
|
+
let mermaid;
|
|
3
|
+
let initializedFor;
|
|
4
|
+
let diagramCount = 0;
|
|
5
|
+
// Draws run one after another: a theme switch during a draw would otherwise race it and leave a
|
|
6
|
+
// diagram in whichever theme finished last.
|
|
7
|
+
let queue = Promise.resolve();
|
|
8
|
+
export function initDiagrams() {
|
|
9
|
+
if (document.querySelector(".mermaid-diagram") === null) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
schedule();
|
|
13
|
+
onThemeChange(() => {
|
|
14
|
+
for (const block of document.querySelectorAll(".mermaid-diagram")) {
|
|
15
|
+
delete block.dataset.state;
|
|
16
|
+
}
|
|
17
|
+
schedule();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function schedule() {
|
|
21
|
+
queue = queue.then(drawPending);
|
|
22
|
+
}
|
|
23
|
+
// Mermaid is several megabytes, so it is fetched only by a page that has a diagram on it.
|
|
24
|
+
async function loadMermaid(theme) {
|
|
25
|
+
// A variable, not a literal, so TypeScript does not try to resolve a URL as a module.
|
|
26
|
+
const url = "/vendor/mermaid/mermaid.esm.min.mjs";
|
|
27
|
+
mermaid ??= import(url).then((module) => module.default);
|
|
28
|
+
const instance = await mermaid;
|
|
29
|
+
if (initializedFor !== theme) {
|
|
30
|
+
instance.initialize({
|
|
31
|
+
startOnLoad: false,
|
|
32
|
+
// Strict keeps click handlers and raw HTML labels out of diagrams, which are as untrusted as
|
|
33
|
+
// the Markdown they come from.
|
|
34
|
+
securityLevel: "strict",
|
|
35
|
+
theme: theme === "dark" ? "dark" : "default",
|
|
36
|
+
});
|
|
37
|
+
initializedFor = theme;
|
|
38
|
+
}
|
|
39
|
+
return instance;
|
|
40
|
+
}
|
|
41
|
+
async function drawPending() {
|
|
42
|
+
const blocks = document.querySelectorAll(".mermaid-diagram:not([data-state])");
|
|
43
|
+
for (const block of blocks) {
|
|
44
|
+
// A diagram drawn inside a closed <details> measures as zero-sized and comes out collapsed, so
|
|
45
|
+
// it waits until its section is opened.
|
|
46
|
+
const closed = block.closest("details:not([open])");
|
|
47
|
+
if (closed !== null) {
|
|
48
|
+
closed.addEventListener("toggle", schedule, { once: true });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const source = block.querySelector(".mermaid-source")?.textContent ?? "";
|
|
52
|
+
try {
|
|
53
|
+
// Fonts change text width; measuring before they load leaves labels overflowing their boxes.
|
|
54
|
+
await document.fonts.ready;
|
|
55
|
+
const instance = await loadMermaid(effectiveTheme());
|
|
56
|
+
diagramCount += 1;
|
|
57
|
+
const { svg } = await instance.render(`wastech-mermaid-${diagramCount}`, source);
|
|
58
|
+
let target = block.querySelector(".mermaid-svg");
|
|
59
|
+
if (target === null) {
|
|
60
|
+
target = document.createElement("div");
|
|
61
|
+
target.className = "mermaid-svg";
|
|
62
|
+
block.append(target);
|
|
63
|
+
}
|
|
64
|
+
target.innerHTML = svg;
|
|
65
|
+
block.querySelector(".mermaid-error")?.remove();
|
|
66
|
+
block.dataset.state = "rendered";
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
// The source stays on the page; the message says why it is not a picture.
|
|
70
|
+
block.dataset.state = "failed";
|
|
71
|
+
block.querySelector(".mermaid-svg")?.remove();
|
|
72
|
+
let message = block.querySelector(".mermaid-error");
|
|
73
|
+
if (message === null) {
|
|
74
|
+
message = document.createElement("p");
|
|
75
|
+
message.className = "mermaid-error";
|
|
76
|
+
block.prepend(message);
|
|
77
|
+
}
|
|
78
|
+
message.textContent = `Diagram could not be drawn: ${error instanceof Error ? error.message : String(error)}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDiffExpand(): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The unchanged lines between a diff's hunks, opened a few at a time. The server puts every one of
|
|
2
|
+
// them in the table, hidden, so this only shows rows it was given; without script a diff reads as
|
|
3
|
+
// `git diff` prints it, three lines of context around each change.
|
|
4
|
+
export function initDiffExpand() {
|
|
5
|
+
for (const gap of document.querySelectorAll("tbody.diff-gap")) {
|
|
6
|
+
const row = gap.querySelector("tr.diff-expand");
|
|
7
|
+
const button = row?.querySelector("button");
|
|
8
|
+
if (row === null || button === null || button === undefined) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
row.hidden = false;
|
|
12
|
+
button.addEventListener("click", () => {
|
|
13
|
+
open(gap, row, button);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function open(gap, row, button) {
|
|
18
|
+
const step = Number(gap.dataset.step ?? "20");
|
|
19
|
+
const hidden = [
|
|
20
|
+
...gap.querySelectorAll("tr.diff-line[hidden]"),
|
|
21
|
+
];
|
|
22
|
+
// Nearest the change first: the bottom of a gap above the first hunk, the top of any other.
|
|
23
|
+
const shown = gap.dataset.from === "end" ? hidden.slice(-step) : hidden.slice(0, step);
|
|
24
|
+
for (const line of shown) {
|
|
25
|
+
line.hidden = false;
|
|
26
|
+
}
|
|
27
|
+
const left = hidden.length - shown.length;
|
|
28
|
+
if (left === 0) {
|
|
29
|
+
row.remove();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// Above the first hunk the opened lines sit below the button; elsewhere above it, so the button
|
|
33
|
+
// moves down past them and stays next to what is still hidden.
|
|
34
|
+
if (gap.dataset.from !== "end") {
|
|
35
|
+
const last = shown.at(-1);
|
|
36
|
+
last?.after(row);
|
|
37
|
+
}
|
|
38
|
+
button.textContent =
|
|
39
|
+
left <= step
|
|
40
|
+
? `Show ${left} unchanged line${left === 1 ? "" : "s"}`
|
|
41
|
+
: `Show ${step} of ${left} unchanged lines`;
|
|
42
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export type TextFont = "system" | "serif" | "wide" | "mono" | "custom";
|
|
2
|
+
export type CodeFont = "system" | "custom";
|
|
3
|
+
export type Alignment = "start" | "justify";
|
|
4
|
+
export type Accent = "blue" | "indigo" | "purple" | "red" | "teal" | "graphite";
|
|
5
|
+
/**
|
|
6
|
+
* The accent colors the reader can pick, each with its caption and its light and dark value. A closed
|
|
7
|
+
* list rather than a free color: the accent carries small text — links, rule ids — so each light value
|
|
8
|
+
* is dark enough for 4.5:1 on white and each dark value light enough for the same on the dark page,
|
|
9
|
+
* which a picked color would not be. Green and amber are left out because they already mean "added"
|
|
10
|
+
* and "changed" on every changed document, and a link in either would read as a change mark. The red
|
|
11
|
+
* is a crimson, apart from the brighter red that marks a removal.
|
|
12
|
+
*/
|
|
13
|
+
export declare const ACCENTS: readonly (readonly [
|
|
14
|
+
Accent,
|
|
15
|
+
string,
|
|
16
|
+
{
|
|
17
|
+
light: string;
|
|
18
|
+
dark: string;
|
|
19
|
+
}
|
|
20
|
+
])[];
|
|
21
|
+
/** An accent's value as the stylesheet reads it: one token for both themes. */
|
|
22
|
+
export declare function accentColor(accent: Accent): string;
|
|
23
|
+
export type DisplaySettings = {
|
|
24
|
+
accent: Accent;
|
|
25
|
+
siteFont: TextFont;
|
|
26
|
+
customSiteFont: string;
|
|
27
|
+
siteCodeFont: CodeFont;
|
|
28
|
+
customSiteCodeFont: string;
|
|
29
|
+
textFont: TextFont;
|
|
30
|
+
customTextFont: string;
|
|
31
|
+
codeFont: CodeFont;
|
|
32
|
+
customCodeFont: string;
|
|
33
|
+
fontSize: number;
|
|
34
|
+
lineHeight: number;
|
|
35
|
+
letterSpacing: number;
|
|
36
|
+
textWidth: number;
|
|
37
|
+
fullWidth: boolean;
|
|
38
|
+
blockSpacing: number;
|
|
39
|
+
codeScale: number;
|
|
40
|
+
align: Alignment;
|
|
41
|
+
paddingTop: number;
|
|
42
|
+
paddingRight: number;
|
|
43
|
+
paddingBottom: number;
|
|
44
|
+
paddingLeft: number;
|
|
45
|
+
sourceLines: boolean;
|
|
46
|
+
};
|
|
47
|
+
export declare const DEFAULT_SETTINGS: Readonly<DisplaySettings>;
|
|
48
|
+
type NumericKey = {
|
|
49
|
+
[K in keyof DisplaySettings]: DisplaySettings[K] extends number ? K : never;
|
|
50
|
+
}[keyof DisplaySettings];
|
|
51
|
+
export declare const NUMBER_RANGES: Readonly<Record<NumericKey, {
|
|
52
|
+
min: number;
|
|
53
|
+
max: number;
|
|
54
|
+
step: number;
|
|
55
|
+
}>>;
|
|
56
|
+
export declare function cleanFontName(name: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* Settings from anything: a stored object, a partial one, or junk. Each field that is missing, of the
|
|
59
|
+
* wrong type or not one of its choices takes its default; each number is clamped into its range. So
|
|
60
|
+
* storage edited by hand, or written by an older or newer viewer, can never break the page.
|
|
61
|
+
*/
|
|
62
|
+
export declare function parseSettings(value: unknown): DisplaySettings;
|
|
63
|
+
/** Every custom property the settings define, defaults included, as the stylesheet reads them. */
|
|
64
|
+
export declare function allVariables(settings: DisplaySettings): Record<string, string>;
|
|
65
|
+
/** Only the custom properties whose value differs from the stylesheet's default. */
|
|
66
|
+
export declare function changedVariables(settings: DisplaySettings): Record<string, string>;
|
|
67
|
+
export declare const STORAGE_KEY = "wastech-mdlint-viewer:display";
|
|
68
|
+
export declare const VARIABLE_NAME: RegExp;
|
|
69
|
+
export type StoredDisplay = {
|
|
70
|
+
settings: DisplaySettings;
|
|
71
|
+
vars: Record<string, string>;
|
|
72
|
+
};
|
|
73
|
+
export declare function storedDisplay(settings: DisplaySettings): StoredDisplay;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// What the reader can change about how the viewer looks: the accent color and fonts of the whole
|
|
2
|
+
// site, and the article's own fonts, sizes, spacing, width and margins. Pure — no DOM, no storage — so the rules that turn a stored or typed value
|
|
3
|
+
// into CSS are tested on their own, and the panel in `display-settings.ts` only wires inputs to them.
|
|
4
|
+
//
|
|
5
|
+
// Every setting becomes one `--reader-*` custom property on <html>, and the stylesheet reads each one
|
|
6
|
+
// with the default look as its fallback. Only settings that differ from the default are stored and
|
|
7
|
+
// set, so a reader who changed nothing gets exactly the stylesheet's own values, and Reset is simply
|
|
8
|
+
// "set none of them".
|
|
9
|
+
/**
|
|
10
|
+
* The accent colors the reader can pick, each with its caption and its light and dark value. A closed
|
|
11
|
+
* list rather than a free color: the accent carries small text — links, rule ids — so each light value
|
|
12
|
+
* is dark enough for 4.5:1 on white and each dark value light enough for the same on the dark page,
|
|
13
|
+
* which a picked color would not be. Green and amber are left out because they already mean "added"
|
|
14
|
+
* and "changed" on every changed document, and a link in either would read as a change mark. The red
|
|
15
|
+
* is a crimson, apart from the brighter red that marks a removal.
|
|
16
|
+
*/
|
|
17
|
+
export const ACCENTS = [
|
|
18
|
+
["blue", "Blue", { light: "#0066cc", dark: "#0a84ff" }],
|
|
19
|
+
["indigo", "Indigo", { light: "#4b48c8", dark: "#7d7aff" }],
|
|
20
|
+
["purple", "Purple", { light: "#8e3cc8", dark: "#bf5af2" }],
|
|
21
|
+
["red", "Red", { light: "#b8204a", dark: "#ff5c7a" }],
|
|
22
|
+
["teal", "Teal", { light: "#007a8a", dark: "#40c8e0" }],
|
|
23
|
+
["graphite", "Graphite", { light: "#48484a", dark: "#aeaeb2" }],
|
|
24
|
+
];
|
|
25
|
+
/** An accent's value as the stylesheet reads it: one token for both themes. */
|
|
26
|
+
export function accentColor(accent) {
|
|
27
|
+
const entry = ACCENTS.find(([value]) => value === accent) ?? ACCENTS[0];
|
|
28
|
+
return `light-dark(${entry[2].light}, ${entry[2].dark})`;
|
|
29
|
+
}
|
|
30
|
+
export const DEFAULT_SETTINGS = {
|
|
31
|
+
accent: "blue",
|
|
32
|
+
siteFont: "system",
|
|
33
|
+
customSiteFont: "",
|
|
34
|
+
siteCodeFont: "system",
|
|
35
|
+
customSiteCodeFont: "",
|
|
36
|
+
textFont: "system",
|
|
37
|
+
customTextFont: "",
|
|
38
|
+
codeFont: "system",
|
|
39
|
+
customCodeFont: "",
|
|
40
|
+
fontSize: 16,
|
|
41
|
+
lineHeight: 1.6,
|
|
42
|
+
letterSpacing: 0,
|
|
43
|
+
textWidth: 860,
|
|
44
|
+
fullWidth: false,
|
|
45
|
+
blockSpacing: 1,
|
|
46
|
+
codeScale: 1,
|
|
47
|
+
align: "start",
|
|
48
|
+
paddingTop: 32,
|
|
49
|
+
paddingRight: 48,
|
|
50
|
+
paddingBottom: 96,
|
|
51
|
+
paddingLeft: 48,
|
|
52
|
+
sourceLines: true,
|
|
53
|
+
};
|
|
54
|
+
// The range of each number and its step, which the panel's inputs use as their `min`, `max` and
|
|
55
|
+
// `step`. A value outside its range is clamped into it, so a stored value from a range that has since
|
|
56
|
+
// narrowed, or a number typed past the end, still lands on something the layout can carry.
|
|
57
|
+
export const NUMBER_RANGES = {
|
|
58
|
+
fontSize: { min: 12, max: 28, step: 1 },
|
|
59
|
+
lineHeight: { min: 1.1, max: 2.4, step: 0.05 },
|
|
60
|
+
letterSpacing: { min: 0, max: 0.2, step: 0.01 },
|
|
61
|
+
textWidth: { min: 480, max: 1800, step: 20 },
|
|
62
|
+
blockSpacing: { min: 0, max: 3, step: 0.05 },
|
|
63
|
+
codeScale: { min: 0.7, max: 1.4, step: 0.05 },
|
|
64
|
+
paddingTop: { min: 0, max: 320, step: 4 },
|
|
65
|
+
paddingRight: { min: 0, max: 320, step: 4 },
|
|
66
|
+
paddingBottom: { min: 0, max: 320, step: 4 },
|
|
67
|
+
paddingLeft: { min: 0, max: 320, step: 4 },
|
|
68
|
+
};
|
|
69
|
+
const TEXT_FONTS = [
|
|
70
|
+
"system",
|
|
71
|
+
"serif",
|
|
72
|
+
"wide",
|
|
73
|
+
"mono",
|
|
74
|
+
"custom",
|
|
75
|
+
];
|
|
76
|
+
const CODE_FONTS = ["system", "custom"];
|
|
77
|
+
const ALIGNMENTS = ["start", "justify"];
|
|
78
|
+
const ACCENT_VALUES = ACCENTS.map(([value]) => value);
|
|
79
|
+
// Font stacks of faces that ship with the common systems, since the viewer fetches nothing from the
|
|
80
|
+
// network: Charter/Sitka/Cambria/Georgia for serif text, Verdana and its look-alikes for a wide sans
|
|
81
|
+
// that stays legible at small sizes.
|
|
82
|
+
const SERIF_STACK = 'Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, "Noto Serif", serif';
|
|
83
|
+
const WIDE_STACK = 'Verdana, "DejaVu Sans", "Bitstream Vera Sans", Geneva, sans-serif';
|
|
84
|
+
// The two layers of stacks. The stylesheet keeps the system's own as `--font-sans-system` and
|
|
85
|
+
// `--font-mono-system`, and makes `--font-sans` and `--font-mono` — which the whole page reads — the
|
|
86
|
+
// site fonts, falling back to those. A site choice is built on the system stacks; an article choice
|
|
87
|
+
// on the site's, so "same as the site" is the article's default. Building a site font on
|
|
88
|
+
// `--font-sans` itself would make that property refer to itself, which CSS treats as invalid.
|
|
89
|
+
const SITE_BASE = {
|
|
90
|
+
sans: "var(--font-sans-system)",
|
|
91
|
+
mono: "var(--font-mono-system)",
|
|
92
|
+
};
|
|
93
|
+
const ARTICLE_BASE = { sans: "var(--font-sans)", mono: "var(--font-mono)" };
|
|
94
|
+
// A family name can hold letters of any script, digits, spaces and a little punctuation; anything
|
|
95
|
+
// else is dropped rather than quoted, so a name can never close its quotes and carry a second value
|
|
96
|
+
// into the property. Long enough for any real family name.
|
|
97
|
+
const MAX_FONT_NAME = 64;
|
|
98
|
+
export function cleanFontName(name) {
|
|
99
|
+
return name
|
|
100
|
+
.replace(/[^\p{L}\p{N} _.-]/gu, "")
|
|
101
|
+
.replace(/\s+/g, " ")
|
|
102
|
+
.trim()
|
|
103
|
+
.slice(0, MAX_FONT_NAME)
|
|
104
|
+
.trim();
|
|
105
|
+
}
|
|
106
|
+
function clamp(value, key) {
|
|
107
|
+
const { min, max, step } = NUMBER_RANGES[key];
|
|
108
|
+
const stepped = Math.round((value - min) / step) * step + min;
|
|
109
|
+
// Rounded to the step's own precision, so 1.6 stays 1.6 and is not stored as 1.6000000000000003.
|
|
110
|
+
const decimals = (String(step).split(".")[1] ?? "").length;
|
|
111
|
+
return Number(Math.min(max, Math.max(min, stepped)).toFixed(decimals));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Settings from anything: a stored object, a partial one, or junk. Each field that is missing, of the
|
|
115
|
+
* wrong type or not one of its choices takes its default; each number is clamped into its range. So
|
|
116
|
+
* storage edited by hand, or written by an older or newer viewer, can never break the page.
|
|
117
|
+
*/
|
|
118
|
+
export function parseSettings(value) {
|
|
119
|
+
const input = typeof value === "object" && value !== null && !Array.isArray(value)
|
|
120
|
+
? value
|
|
121
|
+
: {};
|
|
122
|
+
const settings = { ...DEFAULT_SETTINGS };
|
|
123
|
+
const choice = (key, options) => {
|
|
124
|
+
const raw = input[key];
|
|
125
|
+
return typeof raw === "string" &&
|
|
126
|
+
options.includes(raw)
|
|
127
|
+
? raw
|
|
128
|
+
: undefined;
|
|
129
|
+
};
|
|
130
|
+
settings.accent = choice("accent", ACCENT_VALUES) ?? settings.accent;
|
|
131
|
+
settings.siteFont = choice("siteFont", TEXT_FONTS) ?? settings.siteFont;
|
|
132
|
+
settings.siteCodeFont =
|
|
133
|
+
choice("siteCodeFont", CODE_FONTS) ?? settings.siteCodeFont;
|
|
134
|
+
settings.textFont = choice("textFont", TEXT_FONTS) ?? settings.textFont;
|
|
135
|
+
settings.codeFont = choice("codeFont", CODE_FONTS) ?? settings.codeFont;
|
|
136
|
+
settings.align = choice("align", ALIGNMENTS) ?? settings.align;
|
|
137
|
+
copyFreeFields(input, settings);
|
|
138
|
+
return settings;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The fields that are not a choice among fixed options — font names, switches and numbers — each
|
|
142
|
+
* taken only when it has the right type, so a missing or mistyped one keeps its default.
|
|
143
|
+
*/
|
|
144
|
+
function copyFreeFields(input, settings) {
|
|
145
|
+
for (const key of [
|
|
146
|
+
"customSiteFont",
|
|
147
|
+
"customSiteCodeFont",
|
|
148
|
+
"customTextFont",
|
|
149
|
+
"customCodeFont",
|
|
150
|
+
]) {
|
|
151
|
+
const raw = input[key];
|
|
152
|
+
if (typeof raw === "string") {
|
|
153
|
+
settings[key] = cleanFontName(raw);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const key of ["fullWidth", "sourceLines"]) {
|
|
157
|
+
const raw = input[key];
|
|
158
|
+
if (typeof raw === "boolean") {
|
|
159
|
+
settings[key] = raw;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (const key of Object.keys(NUMBER_RANGES)) {
|
|
163
|
+
const raw = input[key];
|
|
164
|
+
if (typeof raw === "number" && Number.isFinite(raw)) {
|
|
165
|
+
settings[key] = clamp(raw, key);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function textFontStack(choice, customName, base) {
|
|
170
|
+
switch (choice) {
|
|
171
|
+
case "serif":
|
|
172
|
+
return SERIF_STACK;
|
|
173
|
+
case "wide":
|
|
174
|
+
return WIDE_STACK;
|
|
175
|
+
case "mono":
|
|
176
|
+
return base.mono;
|
|
177
|
+
case "custom": {
|
|
178
|
+
const name = cleanFontName(customName);
|
|
179
|
+
// An installed face first, then the stack below it, so a name the machine lacks still reads.
|
|
180
|
+
return name === "" ? base.sans : `"${name}", ${base.sans}`;
|
|
181
|
+
}
|
|
182
|
+
case "system":
|
|
183
|
+
return base.sans;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function codeFontStack(choice, customName, base) {
|
|
187
|
+
const name = choice === "custom" ? cleanFontName(customName) : "";
|
|
188
|
+
return name === "" ? base : `"${name}", ${base}`;
|
|
189
|
+
}
|
|
190
|
+
/** Every custom property the settings define, defaults included, as the stylesheet reads them. */
|
|
191
|
+
export function allVariables(settings) {
|
|
192
|
+
return {
|
|
193
|
+
// Only ever one of the fixed values in `ACCENTS`, never text from storage, so nothing stored can
|
|
194
|
+
// carry a second declaration into the property.
|
|
195
|
+
"--reader-accent": accentColor(settings.accent),
|
|
196
|
+
"--reader-site-font": textFontStack(settings.siteFont, settings.customSiteFont, SITE_BASE),
|
|
197
|
+
"--reader-site-mono": codeFontStack(settings.siteCodeFont, settings.customSiteCodeFont, SITE_BASE.mono),
|
|
198
|
+
"--reader-font": textFontStack(settings.textFont, settings.customTextFont, ARTICLE_BASE),
|
|
199
|
+
"--reader-code-font": codeFontStack(settings.codeFont, settings.customCodeFont, ARTICLE_BASE.mono),
|
|
200
|
+
"--reader-font-size": `${settings.fontSize}px`,
|
|
201
|
+
"--reader-line-height": String(settings.lineHeight),
|
|
202
|
+
"--reader-letter-spacing": `${settings.letterSpacing}em`,
|
|
203
|
+
"--reader-width": settings.fullWidth ? "none" : `${settings.textWidth}px`,
|
|
204
|
+
"--reader-block-gap": `${settings.blockSpacing}em`,
|
|
205
|
+
"--reader-code-scale": String(settings.codeScale),
|
|
206
|
+
"--reader-align": settings.align,
|
|
207
|
+
// Justified text without hyphenation opens wide gaps between words on a narrow line.
|
|
208
|
+
"--reader-hyphens": settings.align === "justify" ? "auto" : "manual",
|
|
209
|
+
"--reader-pad-top": `${settings.paddingTop}px`,
|
|
210
|
+
"--reader-pad-right": `${settings.paddingRight}px`,
|
|
211
|
+
"--reader-pad-bottom": `${settings.paddingBottom}px`,
|
|
212
|
+
"--reader-pad-left": `${settings.paddingLeft}px`,
|
|
213
|
+
"--reader-source-lines": settings.sourceLines ? "block" : "none",
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
const DEFAULT_VARIABLES = allVariables(DEFAULT_SETTINGS);
|
|
217
|
+
/** Only the custom properties whose value differs from the stylesheet's default. */
|
|
218
|
+
export function changedVariables(settings) {
|
|
219
|
+
return Object.fromEntries(Object.entries(allVariables(settings)).filter(([name, value]) => DEFAULT_VARIABLES[name] !== value));
|
|
220
|
+
}
|
|
221
|
+
// The page's script stores the settings (for the panel) and the properties they produce (for the
|
|
222
|
+
// classic script in `theme.ts`, which sets them before the first paint and cannot import this
|
|
223
|
+
// module) under one key, so the two can never come from different saves.
|
|
224
|
+
export const STORAGE_KEY = "wastech-mdlint-viewer:display";
|
|
225
|
+
// The only properties `theme.ts` will set from storage.
|
|
226
|
+
export const VARIABLE_NAME = /^--reader-[a-z-]+$/;
|
|
227
|
+
export function storedDisplay(settings) {
|
|
228
|
+
return { settings, vars: changedVariables(settings) };
|
|
229
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDisplaySettings(): void;
|