@bigmistqke/workpad 0.0.0-stage → 0.18.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 +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
|
@@ -0,0 +1,863 @@
|
|
|
1
|
+
import { html, raw } from "../html.js";
|
|
2
|
+
import { anchorsOf, slugText } from "../anchors.js";
|
|
3
|
+
import { agentMessagesIn, isQuestionName } from "../status.js";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import crypto from "node:crypto";
|
|
6
|
+
import { unified } from "unified";
|
|
7
|
+
import remarkParse from "remark-parse";
|
|
8
|
+
import remarkGfm from "remark-gfm";
|
|
9
|
+
import remarkDirective from "remark-directive";
|
|
10
|
+
import remarkRehype from "remark-rehype";
|
|
11
|
+
import rehypeStringify from "rehype-stringify";
|
|
12
|
+
import { toString } from "mdast-util-to-string";
|
|
13
|
+
// src/pages/markdown.ts
|
|
14
|
+
const parser = unified().use(remarkParse).use(remarkGfm).use(remarkDirective).freeze();
|
|
15
|
+
const HIDDEN_COLON = "";
|
|
16
|
+
const FENCE = /^(\s*)(`{3,}|~{3,})/;
|
|
17
|
+
function hideColonsInFences(source) {
|
|
18
|
+
let open = null;
|
|
19
|
+
return source.split("\n").map((line) => {
|
|
20
|
+
const fence = FENCE.exec(line);
|
|
21
|
+
if (!open) {
|
|
22
|
+
if (fence) open = {
|
|
23
|
+
char: fence[2][0],
|
|
24
|
+
length: fence[2].length
|
|
25
|
+
};
|
|
26
|
+
return line;
|
|
27
|
+
}
|
|
28
|
+
if (fence && fence[2][0] === open.char && fence[2].length >= open.length && !line.slice(fence[0].length).trim()) {
|
|
29
|
+
open = null;
|
|
30
|
+
return line;
|
|
31
|
+
}
|
|
32
|
+
return line.replace(/^(\s*)(:+)/, (_, space, colons) => space + HIDDEN_COLON.repeat(colons.length));
|
|
33
|
+
}).join("\n");
|
|
34
|
+
}
|
|
35
|
+
function showColons(node) {
|
|
36
|
+
if (typeof node.value === "string" && node.value.includes(HIDDEN_COLON)) node.value = node.value.replaceAll(HIDDEN_COLON, ":");
|
|
37
|
+
node.children?.forEach(showColons);
|
|
38
|
+
}
|
|
39
|
+
/** A message's raw HTML, as text: it shows as written, and runs nothing. */
|
|
40
|
+
function asText(node) {
|
|
41
|
+
if (node.type === "html") {
|
|
42
|
+
node.type = "text";
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
node.children?.forEach(asText);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A heading's anchor as GitHub and GitLab make it: lowercased, punctuation dropped, spaces as
|
|
49
|
+
* hyphens. `taken` holds those given out on the page: a repeat gets -1, -2 and on.
|
|
50
|
+
*/
|
|
51
|
+
function slug(text, taken) {
|
|
52
|
+
const base = slugText(text);
|
|
53
|
+
let id = base;
|
|
54
|
+
if (taken.has(base)) {
|
|
55
|
+
let n = taken.get(base);
|
|
56
|
+
do
|
|
57
|
+
id = `${base}-${++n}`;
|
|
58
|
+
while (taken.has(id));
|
|
59
|
+
taken.set(base, n);
|
|
60
|
+
}
|
|
61
|
+
taken.set(id, 0);
|
|
62
|
+
return id;
|
|
63
|
+
}
|
|
64
|
+
/** Markdown → mdast: GFM and directives, with code fences safe inside containers. */
|
|
65
|
+
function parseMarkdown(source) {
|
|
66
|
+
const tree = parser.parse(hideColonsInFences(source));
|
|
67
|
+
showColons(tree);
|
|
68
|
+
return tree;
|
|
69
|
+
}
|
|
70
|
+
const toHast = unified().use(remarkRehype, { allowDangerousHtml: true }).use(rehypeStringify, { allowDangerousHtml: true }).freeze();
|
|
71
|
+
/**
|
|
72
|
+
* `cols=3 open title="a b"` → { cols: '3', open: '', title: 'a b' } (bare words get '' like directive
|
|
73
|
+
* attrs). In braces too, as a fence writes them: `{compose=window}` (an unquoted value ends at `}`).
|
|
74
|
+
*/
|
|
75
|
+
function parseAttrs(s) {
|
|
76
|
+
const out = {};
|
|
77
|
+
for (const m of String(s ?? "").matchAll(/([\w-]+)(?:=("[^"]*"|'[^']*'|[^\s}]+))?/g)) out[m[1]] = m[2] ? m[2].replace(/^["']|["']$/g, "") : "";
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
/** A heading's text as a key: lowercase, words joined by - */
|
|
81
|
+
const slugOf = (text) => text.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-|-$/g, "");
|
|
82
|
+
/** The blocks that, directly inside a block that takes items (a grid), are an item as they are: their label its title. */
|
|
83
|
+
const ITEM_BLOCKS = /* @__PURE__ */ new Set(["form", "details"]);
|
|
84
|
+
/** The containers whose label is a title, where an inline block ending it (a badge) sits at its right (endTitles). */
|
|
85
|
+
const TITLED = /* @__PURE__ */ new Set(["item", "form"]);
|
|
86
|
+
const DIRECTIVES = /* @__PURE__ */ new Set([
|
|
87
|
+
"containerDirective",
|
|
88
|
+
"leafDirective",
|
|
89
|
+
"textDirective"
|
|
90
|
+
]);
|
|
91
|
+
const PHRASING = /* @__PURE__ */ new Set([
|
|
92
|
+
"text",
|
|
93
|
+
"emphasis",
|
|
94
|
+
"strong",
|
|
95
|
+
"delete",
|
|
96
|
+
"inlineCode",
|
|
97
|
+
"break",
|
|
98
|
+
"link",
|
|
99
|
+
"linkReference",
|
|
100
|
+
"image",
|
|
101
|
+
"imageReference",
|
|
102
|
+
"footnoteReference",
|
|
103
|
+
"textDirective"
|
|
104
|
+
]);
|
|
105
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
106
|
+
/**
|
|
107
|
+
* Point relative links and images at what they name on disk: each is resolved from the markdown
|
|
108
|
+
* file's own folder and handed to `linkFor(absolutePath)`, which returns the URL that serves it
|
|
109
|
+
* (or null to leave the link as written).
|
|
110
|
+
*/
|
|
111
|
+
const NO_TEXT_LINKS = /* @__PURE__ */ new Set([
|
|
112
|
+
"link",
|
|
113
|
+
"linkReference",
|
|
114
|
+
"inlineCode",
|
|
115
|
+
"code",
|
|
116
|
+
"html",
|
|
117
|
+
"textDirective",
|
|
118
|
+
"definition"
|
|
119
|
+
]);
|
|
120
|
+
/** Turn the plugins' text links (`#12`) into links, in plain text only. */
|
|
121
|
+
function linkText(tree, links) {
|
|
122
|
+
if (!links.length) return;
|
|
123
|
+
const split = (text) => {
|
|
124
|
+
let parts = [{
|
|
125
|
+
type: "text",
|
|
126
|
+
value: text
|
|
127
|
+
}];
|
|
128
|
+
for (const link of links) {
|
|
129
|
+
const flags = link.pattern.flags.includes("g") ? link.pattern.flags : link.pattern.flags + "g";
|
|
130
|
+
parts = parts.flatMap((part) => {
|
|
131
|
+
if (part.type !== "text") return [part];
|
|
132
|
+
const out = [];
|
|
133
|
+
const value = part.value;
|
|
134
|
+
const re = new RegExp(link.pattern.source, flags);
|
|
135
|
+
let at = 0;
|
|
136
|
+
for (let m = re.exec(value); m; m = re.exec(value)) {
|
|
137
|
+
if (!m[0]) {
|
|
138
|
+
re.lastIndex++;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const href = link.href(m);
|
|
142
|
+
if (!href) continue;
|
|
143
|
+
if (m.index > at) out.push({
|
|
144
|
+
type: "text",
|
|
145
|
+
value: value.slice(at, m.index)
|
|
146
|
+
});
|
|
147
|
+
out.push({
|
|
148
|
+
type: "link",
|
|
149
|
+
url: href,
|
|
150
|
+
title: link.title?.(m) ?? null,
|
|
151
|
+
children: [{
|
|
152
|
+
type: "text",
|
|
153
|
+
value: m[0]
|
|
154
|
+
}]
|
|
155
|
+
});
|
|
156
|
+
at = m.index + m[0].length;
|
|
157
|
+
}
|
|
158
|
+
if (at < value.length) out.push({
|
|
159
|
+
type: "text",
|
|
160
|
+
value: value.slice(at)
|
|
161
|
+
});
|
|
162
|
+
return out;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return parts;
|
|
166
|
+
};
|
|
167
|
+
const walk = (node) => {
|
|
168
|
+
if (!node.children || NO_TEXT_LINKS.has(node.type)) return;
|
|
169
|
+
node.children = node.children.flatMap((child) => child.type === "text" ? split(child.value) : (walk(child), [child]));
|
|
170
|
+
};
|
|
171
|
+
walk(tree);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The address of something named relative to the page (a link, an image, a block's `src`): resolved
|
|
175
|
+
* from the page's folder and handed to `linkFor`. A URL, an address from the root, an anchor, or
|
|
176
|
+
* something nothing serves stays as written.
|
|
177
|
+
*/
|
|
178
|
+
function addressOf(ref, file, linkFor) {
|
|
179
|
+
if (!linkFor || !file || !ref || SCHEME.test(ref) || /^[/#]/.test(ref)) return ref;
|
|
180
|
+
const [, target, rest] = /^([^?#]*)(.*)$/s.exec(ref);
|
|
181
|
+
let decoded = target;
|
|
182
|
+
try {
|
|
183
|
+
decoded = decodeURIComponent(target);
|
|
184
|
+
} catch {}
|
|
185
|
+
const url = decoded && linkFor(path.resolve(path.dirname(file), decoded));
|
|
186
|
+
return url ? url + rest : ref;
|
|
187
|
+
}
|
|
188
|
+
/** Raw HTML's references to files beside the page (`<img src>`, `<video poster>`, `<a href>`). */
|
|
189
|
+
const HTML_REFS = /(\b(?:src|href|poster)=")([^"]*)(")/g;
|
|
190
|
+
function rewriteLinks(tree, file, linkFor) {
|
|
191
|
+
if (!linkFor || !file) return;
|
|
192
|
+
const walk = (node) => {
|
|
193
|
+
if ((node.type === "link" || node.type === "image" || node.type === "definition") && node.url) node.url = addressOf(node.url, file, linkFor);
|
|
194
|
+
if (node.type === "html" && typeof node.value === "string") node.value = node.value.replace(HTML_REFS, (_, open, ref, close) => open + addressOf(ref, file, linkFor) + close);
|
|
195
|
+
node.children?.forEach(walk);
|
|
196
|
+
};
|
|
197
|
+
walk(tree);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* A quote that is an alert, `> [!WARNING] A title` and its lines below (GitHub's, and Obsidian's
|
|
201
|
+
* title and fold marker): as the container directive its block renders, `[!TYPE]` its `type`, the
|
|
202
|
+
* title its label, the rest its content. Else null: a quote like any other.
|
|
203
|
+
*/
|
|
204
|
+
function alertOf(quote) {
|
|
205
|
+
const [first, ...rest] = quote.children ?? [];
|
|
206
|
+
const head = first?.type === "paragraph" ? first.children?.[0] : null;
|
|
207
|
+
const text = head?.type === "text" ? head.value ?? "" : "";
|
|
208
|
+
const m = /^\[!([A-Za-z]+)\][+-]?[ \t]*/.exec(text);
|
|
209
|
+
if (!m) return null;
|
|
210
|
+
const after = [{
|
|
211
|
+
...head,
|
|
212
|
+
value: text.slice(m[0].length)
|
|
213
|
+
}, ...(first.children ?? []).slice(1)];
|
|
214
|
+
const title = [];
|
|
215
|
+
const body = [];
|
|
216
|
+
for (const node of after) if (body.length) body.push(node);
|
|
217
|
+
else if (node.type === "break") body.push({
|
|
218
|
+
type: "text",
|
|
219
|
+
value: ""
|
|
220
|
+
});
|
|
221
|
+
else if (node.type === "text" && node.value?.includes("\n")) {
|
|
222
|
+
const at = node.value.indexOf("\n");
|
|
223
|
+
if (at > 0) title.push({
|
|
224
|
+
...node,
|
|
225
|
+
value: node.value.slice(0, at)
|
|
226
|
+
});
|
|
227
|
+
body.push({
|
|
228
|
+
...node,
|
|
229
|
+
value: node.value.slice(at + 1)
|
|
230
|
+
});
|
|
231
|
+
} else title.push(node);
|
|
232
|
+
const label = title.filter((n) => n.type !== "text" || n.value?.trim());
|
|
233
|
+
const content = body.filter((n) => n.type !== "text" || n.value !== "");
|
|
234
|
+
const children = [
|
|
235
|
+
...label.length ? [{
|
|
236
|
+
type: "paragraph",
|
|
237
|
+
data: { directiveLabel: true },
|
|
238
|
+
children: label
|
|
239
|
+
}] : [],
|
|
240
|
+
...content.length ? [{
|
|
241
|
+
type: "paragraph",
|
|
242
|
+
children: content
|
|
243
|
+
}] : [],
|
|
244
|
+
...rest
|
|
245
|
+
];
|
|
246
|
+
return (name) => ({
|
|
247
|
+
type: "containerDirective",
|
|
248
|
+
name,
|
|
249
|
+
attributes: { type: m[1].toLowerCase() },
|
|
250
|
+
children
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/** Separate a directive's `[label]` (remark-directive stores it as a flagged first paragraph). */
|
|
254
|
+
function splitLabel(node) {
|
|
255
|
+
const [head, ...rest] = node.children ?? [];
|
|
256
|
+
if (node.type === "containerDirective" && head?.data?.directiveLabel) return {
|
|
257
|
+
label: head.children ?? null,
|
|
258
|
+
children: rest
|
|
259
|
+
};
|
|
260
|
+
if (node.type !== "containerDirective") return {
|
|
261
|
+
label: node.children?.length ? node.children : null,
|
|
262
|
+
children: []
|
|
263
|
+
};
|
|
264
|
+
return {
|
|
265
|
+
label: null,
|
|
266
|
+
children: node.children ?? []
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
const MARKER = {
|
|
270
|
+
textDirective: ":",
|
|
271
|
+
leafDirective: "::",
|
|
272
|
+
containerDirective: ":::"
|
|
273
|
+
};
|
|
274
|
+
function literalDirective(node, label) {
|
|
275
|
+
const marker = MARKER[node.type];
|
|
276
|
+
const attrs = Object.entries(node.attributes ?? {}).map(([k, v]) => v ? `${k}="${v}"` : k).join(" ");
|
|
277
|
+
return `${marker}${node.name}${label ? `[${toString({
|
|
278
|
+
type: "root",
|
|
279
|
+
children: label
|
|
280
|
+
})}]` : ""}${attrs ? `{${attrs}}` : ""}`;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* A title's own words, as plain text: a heading's, an item's label. The inline blocks in it (a
|
|
284
|
+
* badge, a `:when`) are left out: they mark the title rather than say it, so what is made of its
|
|
285
|
+
* words (a heading's anchor, an item's key) stays while a status or a date changes. An inline
|
|
286
|
+
* directive no block brings (the `:59` in `19:59`) is words, as written.
|
|
287
|
+
*/
|
|
288
|
+
function titleWords(nodes, resolve) {
|
|
289
|
+
const GONE = "\0";
|
|
290
|
+
const words = (node) => {
|
|
291
|
+
if (node.type === "textDirective") return resolve(node.name) ? GONE : literalDirective(node, node.children?.length ? node.children : null);
|
|
292
|
+
if (typeof node.value === "string") return node.value;
|
|
293
|
+
if (typeof node.alt === "string") return node.alt;
|
|
294
|
+
return (node.children ?? []).map(words).join("");
|
|
295
|
+
};
|
|
296
|
+
return nodes.map(words).join("").replace(/\s*\u0000[\s\u0000]*/g, " ").trim();
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* The forms a page asks the reader to answer, by name: `:::form{name=…}` containers and
|
|
300
|
+
* hand-written <form name="…"> without an action. Parsed, so a form shown as an example inside a
|
|
301
|
+
* code block doesn't count.
|
|
302
|
+
*/
|
|
303
|
+
function formNames(source) {
|
|
304
|
+
const tree = parseMarkdown(String(source ?? ""));
|
|
305
|
+
const names = [];
|
|
306
|
+
const walk = (node) => {
|
|
307
|
+
if (node.type === "containerDirective" && node.name === "form") {
|
|
308
|
+
const attrs = node.attributes ?? {};
|
|
309
|
+
names.push(attrs.name || attrs.id || "form");
|
|
310
|
+
} else if (node.type === "html") for (const m of (node.value ?? "").matchAll(/<form\b([^>]*)>/gi)) {
|
|
311
|
+
if (/\baction\s*=/i.test(m[1])) continue;
|
|
312
|
+
const name = /\bname\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(m[1]);
|
|
313
|
+
if (name) names.push(name[2] ?? name[3] ?? name[4]);
|
|
314
|
+
}
|
|
315
|
+
for (const child of node.children ?? []) walk(child);
|
|
316
|
+
};
|
|
317
|
+
walk(tree);
|
|
318
|
+
return [...new Set(names)].filter((n) => isQuestionName(n) && !n.includes("/"));
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The `:::form`s in a source by the name they are written with, null for one without; in order, and
|
|
322
|
+
* not those shown in a code fence. For a message's forms, which are named in their chat.
|
|
323
|
+
*/
|
|
324
|
+
function formsIn(source) {
|
|
325
|
+
const text = String(source ?? "");
|
|
326
|
+
const out = [];
|
|
327
|
+
const walk = (node) => {
|
|
328
|
+
if (node.type === "containerDirective" && node.name === "form") {
|
|
329
|
+
const attrs = node.attributes ?? {};
|
|
330
|
+
const [start, end] = [node.position?.start?.offset, node.position?.end?.offset];
|
|
331
|
+
out.push({
|
|
332
|
+
name: attrs.name || attrs.id || null,
|
|
333
|
+
markdown: typeof start === "number" ? text.slice(start, end) : ""
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
for (const child of node.children ?? []) walk(child);
|
|
337
|
+
};
|
|
338
|
+
walk(parseMarkdown(text));
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
/** The text of nodes without the inline blocks in them (a form's inputs). */
|
|
342
|
+
const plainText = (nodes) => nodes.map((n) => n.type === "textDirective" || n.type === "leafDirective" ? " " : n.children ? plainText(n.children) : n.value ?? "").join("").replace(/\s+/g, " ").trim();
|
|
343
|
+
/** What one `:::form` node asks (FormDetail). */
|
|
344
|
+
function describeForm(node) {
|
|
345
|
+
const attrs = node.attributes ?? {};
|
|
346
|
+
const children = node.children ?? [];
|
|
347
|
+
const label = children[0]?.data?.directiveLabel ? plainText(children[0].children ?? []) : "";
|
|
348
|
+
const first = children.find((c) => c.type === "paragraph" && !c.data?.directiveLabel && plainText(c.children ?? []));
|
|
349
|
+
const options = [];
|
|
350
|
+
const buttons = [];
|
|
351
|
+
const walk = (n) => {
|
|
352
|
+
if (n.type === "containerDirective" && n.name === "form" && n !== node) return;
|
|
353
|
+
if (n.type === "textDirective" || n.type === "leafDirective") {
|
|
354
|
+
const a = n.attributes ?? {};
|
|
355
|
+
const text = toString(n).trim();
|
|
356
|
+
if ((n.name === "radio" || n.name === "checkbox") && a.name) options.push({
|
|
357
|
+
field: a.name,
|
|
358
|
+
label: text || ("other" in a ? "Other" : ""),
|
|
359
|
+
value: "other" in a ? "" : a.value ?? text,
|
|
360
|
+
kind: n.name,
|
|
361
|
+
recommended: "recommended" in a,
|
|
362
|
+
other: "other" in a
|
|
363
|
+
});
|
|
364
|
+
if (n.name === "button") buttons.push(text || "Send");
|
|
365
|
+
}
|
|
366
|
+
for (const c of n.children ?? []) walk(c);
|
|
367
|
+
};
|
|
368
|
+
children.forEach(walk);
|
|
369
|
+
return {
|
|
370
|
+
name: attrs.name || attrs.id || "form",
|
|
371
|
+
question: label || (first ? plainText(first.children ?? []) : null),
|
|
372
|
+
options,
|
|
373
|
+
buttons
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/** What each `:::form` in a source asks, in order (not those shown in a code fence). */
|
|
377
|
+
function formDetails(source) {
|
|
378
|
+
const out = [];
|
|
379
|
+
const walk = (node) => {
|
|
380
|
+
if (node.type === "containerDirective" && node.name === "form") out.push(describeForm(node));
|
|
381
|
+
for (const child of node.children ?? []) walk(child);
|
|
382
|
+
};
|
|
383
|
+
walk(parseMarkdown(String(source ?? "")));
|
|
384
|
+
return out;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* The forms in the agent's messages in `chats` (a page's chats, chatNames), by their names there:
|
|
388
|
+
* `<chat>/<name>`, an unnamed one numbered in its chat (Api.message). They are questions of the
|
|
389
|
+
* page as much as its own forms are.
|
|
390
|
+
*/
|
|
391
|
+
function messageForms(events, page, chats) {
|
|
392
|
+
return [...new Set(chats.flatMap((chat) => chatForms(events, page, chat).map((f) => f.name)))];
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* The forms in the agent's messages in one chat, in order: each by its name there (`<chat>/<name>`,
|
|
396
|
+
* or its number), as written, and the message it is in.
|
|
397
|
+
*/
|
|
398
|
+
function chatForms(events, page, chat) {
|
|
399
|
+
const out = [];
|
|
400
|
+
let n = 1;
|
|
401
|
+
for (const message of agentMessagesIn(events, page, chat)) for (const form of formsIn(String(message.data.text ?? ""))) {
|
|
402
|
+
if (form.name && (!isQuestionName(form.name) || form.name.includes("/"))) continue;
|
|
403
|
+
out.push({
|
|
404
|
+
name: `${chat}/${form.name ?? n++}`,
|
|
405
|
+
markdown: form.markdown,
|
|
406
|
+
message
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* The chats a page shows, by name: the one under every form (by the form's name, see formNames) and
|
|
413
|
+
* each chat block, `:::chat{name=…}` or `::chat{name=…}`. A chat message (`data.chat`) of any other name is in a chat no longer
|
|
414
|
+
* on the page.
|
|
415
|
+
*/
|
|
416
|
+
function chatNames(source) {
|
|
417
|
+
const tree = parseMarkdown(String(source ?? ""));
|
|
418
|
+
const names = formNames(source);
|
|
419
|
+
const without = /* @__PURE__ */ new Set();
|
|
420
|
+
const walk = (node) => {
|
|
421
|
+
if (node.type === "containerDirective" && node.name === "form" && node.attributes?.chat === "false") without.add(node.attributes.name ?? "");
|
|
422
|
+
if ((node.type === "containerDirective" || node.type === "leafDirective") && node.name === "chat") {
|
|
423
|
+
const attrs = node.attributes ?? {};
|
|
424
|
+
names.push(attrs.name || attrs.id || "chat");
|
|
425
|
+
}
|
|
426
|
+
for (const child of node.children ?? []) walk(child);
|
|
427
|
+
};
|
|
428
|
+
walk(tree);
|
|
429
|
+
return [...new Set(names)].filter((name) => !without.has(name));
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The form `name` as written on a page: its markdown (the container, fences and all) and the text of
|
|
433
|
+
* the last heading before it, for an answer shown with its question (`workpad thread`). Null when
|
|
434
|
+
* the page has no `:::form` of that name.
|
|
435
|
+
*/
|
|
436
|
+
function formSource(source, name) {
|
|
437
|
+
const text = String(source ?? "");
|
|
438
|
+
const tree = parseMarkdown(text);
|
|
439
|
+
let heading = null;
|
|
440
|
+
let found = null;
|
|
441
|
+
const walk = (node) => {
|
|
442
|
+
if (found) return;
|
|
443
|
+
if (node.type === "heading") heading = toString(node);
|
|
444
|
+
if (node.type === "containerDirective" && node.name === "form") {
|
|
445
|
+
const attrs = node.attributes ?? {};
|
|
446
|
+
if ((attrs.name || attrs.id || "form") === name) {
|
|
447
|
+
const start = node.position?.start?.offset;
|
|
448
|
+
const end = node.position?.end?.offset;
|
|
449
|
+
found = {
|
|
450
|
+
heading,
|
|
451
|
+
markdown: typeof start === "number" ? text.slice(start, end) : ""
|
|
452
|
+
};
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
for (const child of node.children ?? []) walk(child);
|
|
457
|
+
};
|
|
458
|
+
walk(tree);
|
|
459
|
+
return found;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Markdown (or an mdast root, rendered as it is) → HTML, with directives and fences handed to
|
|
463
|
+
* blocks. Returns the page's HTML, its first heading, the islands to mount and the blocks'
|
|
464
|
+
* stylesheets.
|
|
465
|
+
*/
|
|
466
|
+
async function renderMarkdown(source, { page, file, resolve, code = null, alert = null, linkFor, textLinks = [], message = false, composers, chat, prefix }) {
|
|
467
|
+
let nextId = 0;
|
|
468
|
+
const islands = {};
|
|
469
|
+
const problems = [];
|
|
470
|
+
const problem = (text) => {
|
|
471
|
+
if (!message && !problems.includes(text)) problems.push(text);
|
|
472
|
+
};
|
|
473
|
+
const keyCount = {};
|
|
474
|
+
const used = /* @__PURE__ */ new Map();
|
|
475
|
+
const stringify = (nodes, inline = nodes.length > 0 && nodes.every((n) => PHRASING.has(n.type))) => {
|
|
476
|
+
if (inline) return toHast.stringify(toHast.runSync({
|
|
477
|
+
type: "root",
|
|
478
|
+
children: [{
|
|
479
|
+
type: "paragraph",
|
|
480
|
+
children: nodes
|
|
481
|
+
}]
|
|
482
|
+
})).replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
483
|
+
return toHast.stringify(toHast.runSync({
|
|
484
|
+
type: "root",
|
|
485
|
+
children: nodes
|
|
486
|
+
}));
|
|
487
|
+
};
|
|
488
|
+
/** Render mdast nodes to HTML. Transforms nested directives first; safe to call twice on the same nodes. */
|
|
489
|
+
const toHtml = async (nodes) => {
|
|
490
|
+
const inline = nodes.length > 0 && nodes.every((n) => PHRASING.has(n.type));
|
|
491
|
+
await transform({
|
|
492
|
+
type: "root",
|
|
493
|
+
children: nodes
|
|
494
|
+
});
|
|
495
|
+
return stringify(nodes, inline);
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* A block's items (DirectiveContext.items): each `:::item[Title]` container directly in it, its
|
|
499
|
+
* label the title (none: untitled), its content any markdown, headings too. A `:::form` or a
|
|
500
|
+
* `:::details` directly in it is an item as it is: its label the title, the block itself the
|
|
501
|
+
* content. Anything else directly in it shows as an untitled item of its own, and is a problem: a
|
|
502
|
+
* heading or a `---` starts no item. Renders the items once; afterwards the block's children are
|
|
503
|
+
* the items' HTML, so rendering them as they are (Block.html's fallback, defaults()) shows the same.
|
|
504
|
+
*/
|
|
505
|
+
async function itemsOf(children) {
|
|
506
|
+
const groups = [];
|
|
507
|
+
let loose = null;
|
|
508
|
+
for (const n of children) {
|
|
509
|
+
if (n.type === "text" && !String(n.value ?? "").trim()) continue;
|
|
510
|
+
if (n.type === "html" && /^\s*<!--[\s\S]*-->\s*$/.test(String(n.value ?? ""))) continue;
|
|
511
|
+
if (n.type === "containerDirective" && n.name === "item") {
|
|
512
|
+
const { label, children: body } = splitLabel(n);
|
|
513
|
+
groups.push({
|
|
514
|
+
label,
|
|
515
|
+
body,
|
|
516
|
+
block: null
|
|
517
|
+
});
|
|
518
|
+
loose = null;
|
|
519
|
+
} else if (n.type === "containerDirective" && ITEM_BLOCKS.has(n.name) && resolve(n.name)) {
|
|
520
|
+
groups.push({
|
|
521
|
+
label: splitLabel(n).label,
|
|
522
|
+
body: [n],
|
|
523
|
+
block: n.name
|
|
524
|
+
});
|
|
525
|
+
loose = null;
|
|
526
|
+
} else {
|
|
527
|
+
if (!loose) groups.push(loose = {
|
|
528
|
+
label: null,
|
|
529
|
+
body: [],
|
|
530
|
+
block: null,
|
|
531
|
+
loose: true
|
|
532
|
+
});
|
|
533
|
+
loose.body.push(n);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
const taken = /* @__PURE__ */ new Map();
|
|
537
|
+
const items = [];
|
|
538
|
+
for (const [i, g] of groups.entries()) {
|
|
539
|
+
const label = g.label && structuredClone(g.label);
|
|
540
|
+
const text = g.label ? titleWords(g.label, resolve) : null;
|
|
541
|
+
const title = g.label && await toHtml(g.block ? structuredClone(g.label) : g.label);
|
|
542
|
+
const base = text && slugOf(text) || String(i + 1);
|
|
543
|
+
const n = (taken.get(base) ?? 0) + 1;
|
|
544
|
+
taken.set(base, n);
|
|
545
|
+
const html = await toHtml(g.body);
|
|
546
|
+
items.push({
|
|
547
|
+
title,
|
|
548
|
+
text: text || null,
|
|
549
|
+
label,
|
|
550
|
+
html,
|
|
551
|
+
nodes: g.body,
|
|
552
|
+
block: g.block,
|
|
553
|
+
key: n > 1 ? `${base}-${n}` : base
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
children.splice(0, children.length, ...items.map((item) => htmlNode(item.html)));
|
|
557
|
+
return {
|
|
558
|
+
items,
|
|
559
|
+
loose: groups.some((g) => g.loose)
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
/** HTML into the markdown tree, where the rest of the pipeline takes it as it is: the one place it becomes a string. */
|
|
563
|
+
const htmlNode = (markup) => ({
|
|
564
|
+
type: "html",
|
|
565
|
+
value: String(markup)
|
|
566
|
+
});
|
|
567
|
+
/**
|
|
568
|
+
* A block's output for one directive or fence: static HTML, or an island around it; then put in
|
|
569
|
+
* what the plugins its `compose` attribute names compose around it (Plugin.compose), in the order
|
|
570
|
+
* written, the first innermost: `{compose=lightbox,window}`. A name no plugin composes is said in
|
|
571
|
+
* place. Not inline blocks, not a block of the composing plugin's own name, not one that renders
|
|
572
|
+
* nothing (left out of a build), and not one that says what's wrong with it instead.
|
|
573
|
+
*/
|
|
574
|
+
async function run(block, ctx, fallback) {
|
|
575
|
+
let out = await output(block, ctx, fallback);
|
|
576
|
+
const attrs = ctx.attrs ?? {};
|
|
577
|
+
const names = String(attrs.compose ?? "").split(/[\s,]+/).filter(Boolean);
|
|
578
|
+
const shown = String(out).trim();
|
|
579
|
+
if (!names.length || ctx.kind === "textDirective" || !shown || shown.startsWith("<pre class=\"wp-error\"")) return out;
|
|
580
|
+
let open;
|
|
581
|
+
for (const name of names) {
|
|
582
|
+
if (block.name === name) continue;
|
|
583
|
+
const compose = composers?.get(name);
|
|
584
|
+
if (!compose) {
|
|
585
|
+
out = html`<pre class="wp-error">compose: no plugin named "${name}" composes around blocks</pre>
|
|
586
|
+
${out}`;
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
try {
|
|
590
|
+
open ??= block.open?.(ctx) ?? null;
|
|
591
|
+
out = raw(String(await compose({
|
|
592
|
+
children: out,
|
|
593
|
+
block: block.name,
|
|
594
|
+
attrs,
|
|
595
|
+
label: ctx.label ?? null,
|
|
596
|
+
open
|
|
597
|
+
})));
|
|
598
|
+
} catch (err) {
|
|
599
|
+
console.error(`plugin ${name}: compose:`, err);
|
|
600
|
+
out = html`<pre class="wp-error">
|
|
601
|
+
plugin "${name}" failed to compose around block "${block.name}": ${err.message}</pre>`;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return out;
|
|
605
|
+
}
|
|
606
|
+
async function output(block, ctx, fallback) {
|
|
607
|
+
used.set(block.name, block);
|
|
608
|
+
try {
|
|
609
|
+
const own = block.html ? await block.html(ctx) : null;
|
|
610
|
+
const body = raw(own ?? await fallback());
|
|
611
|
+
const tag = raw(ctx.kind === "textDirective" ? "span" : "div");
|
|
612
|
+
if (!block.client) return own != null ? body : html`<${tag} data-block="${ctx.name}">${body}</${tag}>`;
|
|
613
|
+
const props = block.props ? await block.props(ctx) : await ctx.defaults();
|
|
614
|
+
const id = `${chat?.prefix ?? prefix ?? ""}${++nextId}`;
|
|
615
|
+
islands[id] = {
|
|
616
|
+
block: block.name,
|
|
617
|
+
client: block.client,
|
|
618
|
+
props
|
|
619
|
+
};
|
|
620
|
+
const hash = crypto.createHash("sha1").update(block.name + "\0" + JSON.stringify(props)).digest("hex").slice(0, 12);
|
|
621
|
+
const key = `${hash}-${keyCount[hash] = (keyCount[hash] ?? 0) + 1}`;
|
|
622
|
+
const anchorId = ctx.attrs?.anchor ? html` id="${ctx.attrs.anchor}"` : "";
|
|
623
|
+
return html`<${tag} class="wp-island" data-block="${ctx.name}" data-wp-island="${id}" data-wp-key="${key}"${anchorId}>${body}</${tag}>`;
|
|
624
|
+
} catch (err) {
|
|
625
|
+
console.error(`block ${block.name}:`, err);
|
|
626
|
+
return html`<pre class="wp-error">block "${block.name}" failed: ${err.message}</pre>`;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const slugs = /* @__PURE__ */ new Map();
|
|
630
|
+
const anchorHeadings = (node) => {
|
|
631
|
+
if (node.type === "heading") {
|
|
632
|
+
const id = slug(titleWords(node.children ?? [], resolve), slugs);
|
|
633
|
+
if (id && !id.startsWith("wp-")) node.data = {
|
|
634
|
+
...node.data,
|
|
635
|
+
hProperties: {
|
|
636
|
+
...node.data?.hProperties,
|
|
637
|
+
id
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
node.children?.forEach(anchorHeadings);
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* An inline block that ends a heading (a badge: `### Parser :badge[done]`), or the label of an item
|
|
645
|
+
* or a form (`:::item[Parser :badge[done]]`, TITLED), sits at the title's
|
|
646
|
+
* right, on its first line, the title wrapping beside it: it moves to the heading's start, where a
|
|
647
|
+
* float rises to the first line (at the end it floats on the last, under a title that fills its
|
|
648
|
+
* first). Wrapped as it renders
|
|
649
|
+
* (`wp-title-end`, see `directive`).
|
|
650
|
+
*/
|
|
651
|
+
const endTitles = (node) => {
|
|
652
|
+
const title = node.type === "heading" ? node : node.type === "containerDirective" && TITLED.has(node.name) && node.children?.[0]?.data?.directiveLabel ? node.children[0] : null;
|
|
653
|
+
if (title?.children?.length) {
|
|
654
|
+
const kids = title.children;
|
|
655
|
+
let at = kids.length - 1;
|
|
656
|
+
while (at > 0 && kids[at].type === "text" && !kids[at].value?.trim()) at--;
|
|
657
|
+
const end = kids[at];
|
|
658
|
+
if (at > 0 && end.type === "textDirective" && resolve(end.name)) {
|
|
659
|
+
end.data = {
|
|
660
|
+
...end.data,
|
|
661
|
+
titleEnd: true
|
|
662
|
+
};
|
|
663
|
+
kids.splice(at, 1);
|
|
664
|
+
kids.unshift(end);
|
|
665
|
+
const last = kids[kids.length - 1];
|
|
666
|
+
if (last?.type === "text" && last.value) last.value = last.value.replace(/\s+$/, "");
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
node.children?.forEach(endTitles);
|
|
670
|
+
};
|
|
671
|
+
const parse = (src) => {
|
|
672
|
+
const tree = parseMarkdown(String(src ?? ""));
|
|
673
|
+
if (message) asText(tree);
|
|
674
|
+
else if (!chat) anchorHeadings(tree);
|
|
675
|
+
if (!message) endTitles(tree);
|
|
676
|
+
rewriteLinks(tree, file, linkFor);
|
|
677
|
+
linkText(tree, textLinks);
|
|
678
|
+
return tree;
|
|
679
|
+
};
|
|
680
|
+
const markdown = async (src) => toHtml(parse(src).children ?? []);
|
|
681
|
+
const inline = async (src) => (await markdown(src)).trim().replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
682
|
+
const fileUrl = (src) => addressOf(src, file, linkFor);
|
|
683
|
+
const anchors = /* @__PURE__ */ new Map();
|
|
684
|
+
const anchor = (type, name, { numbered = false } = {}) => {
|
|
685
|
+
if (!name || message) return null;
|
|
686
|
+
if (chat && !name.startsWith(`${chat.name}/`)) name = `${chat.name}/${name}`;
|
|
687
|
+
const base = `${type}:${name}`;
|
|
688
|
+
if (!anchors.has(base)) {
|
|
689
|
+
anchors.set(base, 0);
|
|
690
|
+
return base;
|
|
691
|
+
}
|
|
692
|
+
if (!numbered) return null;
|
|
693
|
+
let n = anchors.get(base);
|
|
694
|
+
let id;
|
|
695
|
+
do
|
|
696
|
+
id = `${base}-${++n}`;
|
|
697
|
+
while (anchors.has(id));
|
|
698
|
+
anchors.set(base, n);
|
|
699
|
+
anchors.set(id, 0);
|
|
700
|
+
return id;
|
|
701
|
+
};
|
|
702
|
+
let unnamed = chat?.first ?? 1;
|
|
703
|
+
const inChat = chat ? {
|
|
704
|
+
name: chat.name,
|
|
705
|
+
next: () => unnamed++
|
|
706
|
+
} : null;
|
|
707
|
+
const base = {
|
|
708
|
+
page,
|
|
709
|
+
file,
|
|
710
|
+
dir: path.dirname(file),
|
|
711
|
+
parseAttrs,
|
|
712
|
+
toHtml,
|
|
713
|
+
toString,
|
|
714
|
+
markdown,
|
|
715
|
+
inline,
|
|
716
|
+
fileUrl,
|
|
717
|
+
anchor,
|
|
718
|
+
chat: inChat
|
|
719
|
+
};
|
|
720
|
+
/**
|
|
721
|
+
* Attributes a block doesn't read (Block.attrs), when it says which it does: a typo, or one from
|
|
722
|
+
* another block (`wide` on a grid). Those every block takes: `compose` and the composers' own names
|
|
723
|
+
* (and `title`, a window's label), `id`, `class`. A block composed is left be: its composers read
|
|
724
|
+
* attributes of their own (`wide`, `height`).
|
|
725
|
+
*/
|
|
726
|
+
function unread(block, attrs, written) {
|
|
727
|
+
if (!block.attrs || attrs && "compose" in attrs) return;
|
|
728
|
+
const unknown = Object.keys(attrs ?? {}).filter((k) => !block.attrs.includes(k) && ![
|
|
729
|
+
"compose",
|
|
730
|
+
"title",
|
|
731
|
+
"id",
|
|
732
|
+
"class"
|
|
733
|
+
].includes(k) && !composers?.has(k));
|
|
734
|
+
for (const k of unknown) problem(`\`${written}\` doesn't read \`${k}\`: it does nothing there`);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Before rendering, what only the whole tree shows: a form's input outside any form (a container
|
|
738
|
+
* inside the form one colon short closes it early: what follows shows, and sends nothing), and the
|
|
739
|
+
* links to an anchor on this page (checked once the page is rendered).
|
|
740
|
+
*/
|
|
741
|
+
const links = [];
|
|
742
|
+
function survey(node, inForm) {
|
|
743
|
+
const form = resolve("form");
|
|
744
|
+
for (const child of node.children ?? []) {
|
|
745
|
+
if (child.type === "link" && typeof child.url === "string" && child.url.startsWith("#")) links.push(child.url);
|
|
746
|
+
const within = inForm || child.type === "containerDirective" && child.name === "form";
|
|
747
|
+
if (child.type === "textDirective" && !within && form && child.name !== "form") {
|
|
748
|
+
const block = resolve(child.name);
|
|
749
|
+
if (block && block.source === form.source) problem(`\`:${child.name}\` is outside any form: it shows, and sends nothing. A block inside the form needs fewer colons than the form (\`::::form\` around \`:::details\`)`);
|
|
750
|
+
}
|
|
751
|
+
survey(child, within);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
async function directive(node, quoted = false) {
|
|
755
|
+
const { label, children } = splitLabel(node);
|
|
756
|
+
const found = message ? null : resolve(node.name);
|
|
757
|
+
const block = found && !!found.alerts !== quoted ? null : found;
|
|
758
|
+
if (!block) {
|
|
759
|
+
if (node.type === "containerDirective" && node.name === "item") problem("`:::item` is an item of a grid, tabs or a timeline, directly inside one: it shows its content as it is here. The block around items takes one colon more than they do (`::::grid` around `:::item`)");
|
|
760
|
+
else if (node.type !== "textDirective" || label || Object.keys(node.attributes ?? {}).length) problem(`no block \`${MARKER[node.type]}${node.name}\` here: it shows its content as it is. \`workpad docs\` lists the blocks`);
|
|
761
|
+
if (node.type === "textDirective") return {
|
|
762
|
+
type: "text",
|
|
763
|
+
value: literalDirective(node, label)
|
|
764
|
+
};
|
|
765
|
+
const labelHtml = label && html`<p class="wp-label"><strong>${raw(await toHtml(label))}</strong></p>`;
|
|
766
|
+
return htmlNode(html`<div class="wp-unknown" data-directive="${node.name}">${labelHtml}${raw(await toHtml(children))}</div>`);
|
|
767
|
+
}
|
|
768
|
+
unread(block, node.attributes, `${MARKER[node.type]}${node.name}`);
|
|
769
|
+
const labelHtml = label ? await toHtml(label) : null;
|
|
770
|
+
let defaults;
|
|
771
|
+
let split;
|
|
772
|
+
const itemsOnce = async () => (await (split ??= itemsOf(children))).items;
|
|
773
|
+
const out = await run(block, {
|
|
774
|
+
...base,
|
|
775
|
+
kind: node.type,
|
|
776
|
+
name: node.name,
|
|
777
|
+
attrs: { ...node.attributes },
|
|
778
|
+
label: labelHtml,
|
|
779
|
+
labelText: label ? toString({
|
|
780
|
+
type: "root",
|
|
781
|
+
children: label
|
|
782
|
+
}) : null,
|
|
783
|
+
children,
|
|
784
|
+
node,
|
|
785
|
+
items: async () => {
|
|
786
|
+
const { items, loose } = await (split ??= itemsOf(children));
|
|
787
|
+
if (loose) problem(`\`:::${node.name}\` takes its items as \`:::item[Title]\` containers (the block one colon more: \`::::${node.name}\`): what is outside one shows as an untitled item, and a heading or a \`---\` starts none`);
|
|
788
|
+
return items;
|
|
789
|
+
},
|
|
790
|
+
defaults: () => defaults ??= (async () => {
|
|
791
|
+
const list = (await itemsOnce()).map(({ nodes: _, label: _label, ...item }) => item);
|
|
792
|
+
return {
|
|
793
|
+
name: node.name,
|
|
794
|
+
attrs: { ...node.attributes },
|
|
795
|
+
label: labelHtml,
|
|
796
|
+
html: await toHtml(children),
|
|
797
|
+
items: list
|
|
798
|
+
};
|
|
799
|
+
})()
|
|
800
|
+
}, () => toHtml(children));
|
|
801
|
+
return htmlNode(node.data?.titleEnd ? html`<span class="wp-title-end">${raw(String(out))}</span>` : out);
|
|
802
|
+
}
|
|
803
|
+
async function fence(node) {
|
|
804
|
+
const lang = node.lang || null;
|
|
805
|
+
const plain = async () => html`<pre><code${lang && html` class="language-${lang}"`}>${node.value}</code></pre>`;
|
|
806
|
+
const named = lang ? resolve(lang) : null;
|
|
807
|
+
const block = (named?.fences ? named : null) || code;
|
|
808
|
+
if (!block) return htmlNode(await plain());
|
|
809
|
+
const name = code && block === code && lang !== code.name ? code.name : lang;
|
|
810
|
+
const attrs = parseAttrs(node.meta);
|
|
811
|
+
if (named?.fences) unread(block, attrs, `\`\`\`${lang}`);
|
|
812
|
+
const ctx = {
|
|
813
|
+
...base,
|
|
814
|
+
kind: "fence",
|
|
815
|
+
name,
|
|
816
|
+
lang,
|
|
817
|
+
attrs,
|
|
818
|
+
text: node.value,
|
|
819
|
+
node,
|
|
820
|
+
defaults: async () => ({
|
|
821
|
+
name,
|
|
822
|
+
attrs,
|
|
823
|
+
text: node.value
|
|
824
|
+
})
|
|
825
|
+
};
|
|
826
|
+
return htmlNode(await run(block, ctx, plain));
|
|
827
|
+
}
|
|
828
|
+
async function transform(parent) {
|
|
829
|
+
const kids = parent.children;
|
|
830
|
+
if (!kids) return;
|
|
831
|
+
for (let i = 0; i < kids.length; i++) {
|
|
832
|
+
const node = kids[i];
|
|
833
|
+
if (DIRECTIVES.has(node.type)) kids[i] = await directive(node);
|
|
834
|
+
else if (node.type === "code") kids[i] = await fence(node);
|
|
835
|
+
else if (node.type === "blockquote" && alert && !message && alertOf(node)) kids[i] = await directive(alertOf(node)(alert), true);
|
|
836
|
+
else await transform(node);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
const tree = typeof source === "string" ? parse(source) : source;
|
|
840
|
+
const h1 = tree.children?.find((n) => n.type === "heading" && n.depth === 1);
|
|
841
|
+
const title = h1 ? toString(h1) : null;
|
|
842
|
+
if (!message && !chat) survey(tree, false);
|
|
843
|
+
await transform(tree);
|
|
844
|
+
const out = stringify(tree.children ?? []);
|
|
845
|
+
if (links.length) {
|
|
846
|
+
const ids = new Set(anchorsOf(String(out)).map((a) => a.id));
|
|
847
|
+
for (const url of links) {
|
|
848
|
+
let id = url.slice(1);
|
|
849
|
+
try {
|
|
850
|
+
id = decodeURIComponent(id);
|
|
851
|
+
} catch {}
|
|
852
|
+
if (id && !id.startsWith("message:") && !ids.has(id)) problem(`a link to \`${url}\`, which this page doesn't have: \`workpad anchors\` lists what it has`);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return {
|
|
856
|
+
html: out,
|
|
857
|
+
title,
|
|
858
|
+
islands,
|
|
859
|
+
styles: [...new Set([...used.values()].map((t) => t.style).filter((s) => !!s))],
|
|
860
|
+
problems
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
export { chatForms, chatNames, formDetails, formNames, formSource, formsIn, messageForms, parseAttrs, parseMarkdown, renderMarkdown, slug, titleWords };
|