ovr 7.0.0-next.37 → 7.0.0-next.38
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/CHANGELOG.md +6 -0
- package/dist/context/index.d.ts +2 -2
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +4 -7
- package/dist/markdown/index.d.ts +38 -52
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +6 -73
- package/dist/markup/index.d.ts +6 -49
- package/dist/markup/index.d.ts.map +1 -1
- package/dist/markup/index.js +19 -92
- package/dist/render/index.d.ts +13 -12
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +54 -21
- package/package.json +1 -1
- package/src/context/index.ts +14 -11
- package/src/markdown/index.ts +41 -120
- package/src/markup/index.ts +21 -109
- package/src/render/index.ts +71 -30
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# ovr
|
|
2
2
|
|
|
3
|
+
## 7.0.0-next.38
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0e9715a: The option types of a render are `Render.Options.Markup` and `Render.Options.Markdown`, where they were `Render.Markup.Options` and `Render.Markdown.Options`. Inside, `Render` drives the renderers itself, without a class of each between.
|
|
8
|
+
|
|
3
9
|
## 7.0.0-next.37
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/dist/context/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { App } from "../app/index.js";
|
|
2
2
|
import { Cookie } from "../cookie/index.js";
|
|
3
3
|
import { type Field } from "../form/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import { type Options, tools } from "../markdown/index.js";
|
|
5
5
|
import type { Middleware } from "../middleware/index.js";
|
|
6
6
|
import { Multipart } from "../multipart/index.js";
|
|
7
7
|
import { Route } from "../route/index.js";
|
|
@@ -20,7 +20,7 @@ export declare const agent: WeakMap<Request, {
|
|
|
20
20
|
readonly message: string;
|
|
21
21
|
}[];
|
|
22
22
|
/** The tool a form submits to and its arguments, so a page points to it */
|
|
23
|
-
readonly tool?:
|
|
23
|
+
readonly tool?: Options[typeof tools];
|
|
24
24
|
}>;
|
|
25
25
|
/** Properties to build the final `Response` with once middleware has run. */
|
|
26
26
|
declare class PreparedResponse {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,KAAK,KAAK,EAA6B,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,KAAK,KAAK,EAA6B,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAkC,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7E;;;;GAIG;AACH,eAAO,MAAM,KAAK;uBAGG,OAAO;IAC1B,8DAA8D;oBAC9C,WAAW,CAAC,MAAM,CAAC;aAC1B,SAAS;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAA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SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAC/B,CAAC,SAAS,GAAG,MAAM,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,EAAE,GACzC,KAAK,GACL,CAAC,GACF,CAAC,EACJ,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS,QAQzC;IAED;;;;;;;;OAQG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,WAalB;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,SAAS,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,aAEpC;IAED;;;;;;;;;;;OAWG;IACH,OAAO,oCAIN;IAED;;;;;;OAMG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAgEtE;IAwGD;;;;;;OAMG;IACH,OAAa,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,qBAoBxD;CACD"}
|
package/dist/context/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Cookie } from "../cookie/index.js";
|
|
2
2
|
import { Form as FormSchema, forms } from "../form/index.js";
|
|
3
|
-
import {
|
|
4
|
-
import { Markup } from "../markup/index.js";
|
|
3
|
+
import { tools } from "../markdown/index.js";
|
|
5
4
|
import { Multipart } from "../multipart/index.js";
|
|
5
|
+
import { Render } from "../render/index.js";
|
|
6
6
|
import { Route } from "../route/index.js";
|
|
7
7
|
import { Checksum, Header, Method, Mime } from "../util/index.js";
|
|
8
8
|
/**
|
|
@@ -328,15 +328,12 @@ export class Context {
|
|
|
328
328
|
// for both
|
|
329
329
|
if (!/^\s*attachment/i.test(res.headers.get("content-disposition") ?? "") &&
|
|
330
330
|
this.#markdown()) {
|
|
331
|
-
res.body = new
|
|
332
|
-
base: this.url,
|
|
333
|
-
[tools]: agent.get(this.req)?.tool,
|
|
334
|
-
}).stream();
|
|
331
|
+
res.body = new Render({ markdown: r }, { base: this.url, [tools]: agent.get(this.req)?.tool }).stream();
|
|
335
332
|
res.headers.set(Header.name.type, Header.utf8(Mime.type.md));
|
|
336
333
|
return;
|
|
337
334
|
}
|
|
338
335
|
}
|
|
339
|
-
res.body = new
|
|
336
|
+
res.body = new Render({ markup: r }, {
|
|
340
337
|
// other defined types are not markup, so they are not escaped
|
|
341
338
|
raw: Boolean(mime && !Mime.markup(mime)),
|
|
342
339
|
}).stream();
|
package/dist/markdown/index.d.ts
CHANGED
|
@@ -1,66 +1,52 @@
|
|
|
1
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
2
|
/** Internal protocol for the MCP endpoint, to write a form as its tool's call. */
|
|
3
3
|
export declare const tools: unique symbol;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
}
|
|
4
|
+
/** Markdown conversion options */
|
|
5
|
+
export type Options = {
|
|
6
|
+
/** Resolves relative links and form actions, usually the request URL */
|
|
7
|
+
readonly base?: string | URL | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Finds the tool that submits a form, if there is one, set by
|
|
10
|
+
* an MCP endpoint. A form is then written as the call to its tool, with
|
|
11
|
+
* the arguments its address holds, its values, and its messages. A form
|
|
12
|
+
* without a tool is left out: the agent has no way to submit it.
|
|
13
|
+
*
|
|
14
|
+
* @param method Form method
|
|
15
|
+
* @param action Resolved form action
|
|
16
|
+
* @returns The tool's name, the arguments from the address's parameters,
|
|
17
|
+
* the fields whose choices are given as the form renders, which its
|
|
18
|
+
* input schema can't list, and the fields it takes as an array, which
|
|
19
|
+
* the form shows as rows or lines
|
|
20
|
+
*/
|
|
21
|
+
readonly [tools]?: ((method: string, action: URL) => {
|
|
22
|
+
name: string;
|
|
23
|
+
args: Record<string, string>;
|
|
24
|
+
choices: ReadonlySet<string>;
|
|
25
|
+
lists: ReadonlySet<string>;
|
|
26
|
+
} | undefined) | undefined;
|
|
27
|
+
};
|
|
30
28
|
/**
|
|
31
|
-
* Renders an element to Markdown
|
|
32
|
-
*
|
|
33
|
-
* made with `new Render`. Each top level block is
|
|
34
|
-
* written as soon as it closes, and when the page has a `<main>` element, only
|
|
35
|
-
* its contents are kept. Raw HTML is written as it is.
|
|
36
|
-
*
|
|
37
|
-
* ```tsx
|
|
38
|
-
* const markdown = await new Render({ markdown: <Post post={post} /> }, { base: url }).string();
|
|
39
|
-
* ```
|
|
29
|
+
* Renders an element to Markdown a step at a time, as a render of markup
|
|
30
|
+
* steps through its chunks, so neither waits unless the element does.
|
|
40
31
|
*/
|
|
41
|
-
export declare class
|
|
32
|
+
export declare class Blocks {
|
|
42
33
|
#private;
|
|
43
34
|
/**
|
|
44
|
-
* @param text Plain text
|
|
45
|
-
* @returns The text escaped, to write in Markdown as it is
|
|
46
|
-
*/
|
|
47
|
-
static escape(text: string): string;
|
|
48
|
-
/**
|
|
49
|
-
* Creates an `AsyncIterable` that renders the element to Markdown.
|
|
50
|
-
*
|
|
51
35
|
* @param element Element to render
|
|
52
36
|
* @param options Markdown options
|
|
53
37
|
*/
|
|
54
|
-
constructor(element: JSX.Element, options?:
|
|
55
|
-
/** @yields Markdown as each block completes */
|
|
56
|
-
[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>;
|
|
57
|
-
/** @returns `ReadableStream` of Markdown */
|
|
58
|
-
stream(): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
38
|
+
constructor(element: JSX.Element, options?: Options);
|
|
59
39
|
/**
|
|
60
|
-
* The
|
|
61
|
-
*
|
|
62
|
-
* @returns Concatenated Markdown
|
|
40
|
+
* @returns The Markdown of the next complete block or blocks, a promise to
|
|
41
|
+
* wait for before the next step, or `undefined` once all of it is written
|
|
63
42
|
*/
|
|
64
|
-
|
|
43
|
+
step(): string | Promise<unknown> | undefined;
|
|
44
|
+
/** Ends the render if it is not read to its end. */
|
|
45
|
+
close(): Promise<void>;
|
|
65
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* @param text Plain text
|
|
49
|
+
* @returns The text escaped, to write in Markdown as it is
|
|
50
|
+
*/
|
|
51
|
+
export declare function escape(text: string): string;
|
|
66
52
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AA0C3C,kFAAkF;AAClF,eAAO,MAAM,KAAK,eAAkB,CAAC;AAErC,kCAAkC;AAClC,MAAM,MAAM,OAAO,GAAG;IACrB,wEAAwE;IACxE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC;IAEzC;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EACd,CAAC,CACD,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,GAAG,KAET;QACA,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC7B,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QAC7B,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;KAC1B,GACD,SAAS,CAAC,GACZ,SAAS,CAAC;CACb,CAAC;AAg0CF;;;GAGG;AACH,qBAAa,MAAM;;IAUlB;;;OAGG;IACH,YAAY,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,EAGlD;IAED;;;OAGG;IACH,IAAI,0CAaH;IAED,oDAAoD;IACpD,KAAK,kBAEJ;CACD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,UAElC"}
|
package/dist/markdown/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Group, Renderer, Source, Tag
|
|
1
|
+
import { Group, Renderer, Source, Tag } from "../markup/index.js";
|
|
2
2
|
/** Internal protocol for the MCP endpoint, to write a form as its tool's call. */
|
|
3
3
|
export const tools = Symbol("tools");
|
|
4
4
|
/** Writes Markdown from rendered chunks, one block at a time */
|
|
@@ -1246,7 +1246,7 @@ class Writer {
|
|
|
1246
1246
|
* Renders an element to Markdown a step at a time, as a render of markup
|
|
1247
1247
|
* steps through its chunks, so neither waits unless the element does.
|
|
1248
1248
|
*/
|
|
1249
|
-
class Blocks {
|
|
1249
|
+
export class Blocks {
|
|
1250
1250
|
/** Render of the element's chunks */
|
|
1251
1251
|
#render;
|
|
1252
1252
|
/** Writes Markdown from the chunks */
|
|
@@ -1285,76 +1285,9 @@ class Blocks {
|
|
|
1285
1285
|
}
|
|
1286
1286
|
}
|
|
1287
1287
|
/**
|
|
1288
|
-
*
|
|
1289
|
-
*
|
|
1290
|
-
* made with `new Render`. Each top level block is
|
|
1291
|
-
* written as soon as it closes, and when the page has a `<main>` element, only
|
|
1292
|
-
* its contents are kept. Raw HTML is written as it is.
|
|
1293
|
-
*
|
|
1294
|
-
* ```tsx
|
|
1295
|
-
* const markdown = await new Render({ markdown: <Post post={post} /> }, { base: url }).string();
|
|
1296
|
-
* ```
|
|
1288
|
+
* @param text Plain text
|
|
1289
|
+
* @returns The text escaped, to write in Markdown as it is
|
|
1297
1290
|
*/
|
|
1298
|
-
export
|
|
1299
|
-
|
|
1300
|
-
* @param text Plain text
|
|
1301
|
-
* @returns The text escaped, to write in Markdown as it is
|
|
1302
|
-
*/
|
|
1303
|
-
static escape(text) {
|
|
1304
|
-
return Writer.escape(text);
|
|
1305
|
-
}
|
|
1306
|
-
/** Element to render */
|
|
1307
|
-
#element;
|
|
1308
|
-
/** Markdown options */
|
|
1309
|
-
#options;
|
|
1310
|
-
/**
|
|
1311
|
-
* Creates an `AsyncIterable` that renders the element to Markdown.
|
|
1312
|
-
*
|
|
1313
|
-
* @param element Element to render
|
|
1314
|
-
* @param options Markdown options
|
|
1315
|
-
*/
|
|
1316
|
-
constructor(element, options) {
|
|
1317
|
-
this.#element = element;
|
|
1318
|
-
this.#options = options;
|
|
1319
|
-
}
|
|
1320
|
-
/** @yields Markdown as each block completes */
|
|
1321
|
-
async *[Symbol.asyncIterator]() {
|
|
1322
|
-
const blocks = new Blocks(this.#element, this.#options);
|
|
1323
|
-
try {
|
|
1324
|
-
for (let result; (result = blocks.step());) {
|
|
1325
|
-
if (typeof result === "string")
|
|
1326
|
-
yield result;
|
|
1327
|
-
else
|
|
1328
|
-
await result;
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
finally {
|
|
1332
|
-
await blocks.close();
|
|
1333
|
-
}
|
|
1334
|
-
}
|
|
1335
|
-
/** @returns `ReadableStream` of Markdown */
|
|
1336
|
-
stream() {
|
|
1337
|
-
return readable(new Blocks(this.#element, this.#options));
|
|
1338
|
-
}
|
|
1339
|
-
/**
|
|
1340
|
-
* The whole render as one string.
|
|
1341
|
-
*
|
|
1342
|
-
* @returns Concatenated Markdown
|
|
1343
|
-
*/
|
|
1344
|
-
async string() {
|
|
1345
|
-
const blocks = new Blocks(this.#element, this.#options);
|
|
1346
|
-
let markdown = "";
|
|
1347
|
-
try {
|
|
1348
|
-
for (let result; (result = blocks.step());) {
|
|
1349
|
-
if (typeof result === "string")
|
|
1350
|
-
markdown += result;
|
|
1351
|
-
else
|
|
1352
|
-
await result;
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
finally {
|
|
1356
|
-
await blocks.close();
|
|
1357
|
-
}
|
|
1358
|
-
return markdown;
|
|
1359
|
-
}
|
|
1291
|
+
export function escape(text) {
|
|
1292
|
+
return Writer.escape(text);
|
|
1360
1293
|
}
|
package/dist/markup/index.d.ts
CHANGED
|
@@ -129,55 +129,12 @@ export declare const readable: (render: {
|
|
|
129
129
|
step(): Chunk | string | Promise<unknown> | undefined;
|
|
130
130
|
close(): Promise<unknown>;
|
|
131
131
|
}) => ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
132
|
-
export declare namespace Markup {
|
|
133
|
-
/** Markup options */
|
|
134
|
-
type Options = {
|
|
135
|
-
/** Set to `true` to disable escaping, for content that is not markup */
|
|
136
|
-
raw?: boolean;
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
132
|
/**
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
133
|
+
* Escapes text for markup.
|
|
134
|
+
*
|
|
135
|
+
* @param text Text to escape
|
|
136
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
137
|
+
* @returns Escaped text
|
|
143
138
|
*/
|
|
144
|
-
export declare
|
|
145
|
-
#private;
|
|
146
|
-
/**
|
|
147
|
-
* Creates an `AsyncIterable` that renders the `Element`.
|
|
148
|
-
*
|
|
149
|
-
* @param element Element to render
|
|
150
|
-
* @param options Markup options
|
|
151
|
-
*/
|
|
152
|
-
constructor(element: JSX.Element, options?: Markup.Options);
|
|
153
|
-
/** @returns The element, rendered in place where a render is a child */
|
|
154
|
-
get [source](): JSX.Element;
|
|
155
|
-
/** @yields Markup as the `Element` resolves */
|
|
156
|
-
[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>;
|
|
157
|
-
/**
|
|
158
|
-
* The render piped into a `ReadableStream`. Iterate it when possible, to
|
|
159
|
-
* avoid the overhead of the stream.
|
|
160
|
-
*
|
|
161
|
-
* @returns `ReadableStream` of markup
|
|
162
|
-
*/
|
|
163
|
-
stream(): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
164
|
-
/**
|
|
165
|
-
* The whole render as one string.
|
|
166
|
-
*
|
|
167
|
-
* ### WARNING
|
|
168
|
-
*
|
|
169
|
-
* This negates streaming benefits and buffers the result into a string.
|
|
170
|
-
*
|
|
171
|
-
* @returns Concatenated markup
|
|
172
|
-
*/
|
|
173
|
-
string(): Promise<string>;
|
|
174
|
-
/**
|
|
175
|
-
* Escapes text for markup.
|
|
176
|
-
*
|
|
177
|
-
* @param html String to escape
|
|
178
|
-
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
179
|
-
* @returns Escaped text
|
|
180
|
-
*/
|
|
181
|
-
static escape(html: string, attr?: boolean): string;
|
|
182
|
-
}
|
|
139
|
+
export declare function escape(text: string, attr?: boolean): string;
|
|
183
140
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markup/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAG3C,mDAAmD;AACnD,qBAAa,KAAK;IACjB,iCAAiC;IACjC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,0DAA0D;IAC1D,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,CAAC;IAElC;;;;;OAKG;IACH,YAAY,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,OAAO,EAGxC;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM,CAEjB;CACD;AAED;;;GAGG;AACH,qBAAa,GAAI,SAAQ,KAAK;IAC7B,eAAe;IACf,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,uDAAuD;IACvD,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,GAAG,SAAS,CAAC;IAEtC,mEAAmE;IACnE,QAAQ,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IAED,MAAM,CAAC,KAAK,EAAE,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,wCAErC;IAED,KAAK,CAAC,KAAK,EAAE,OAAO,wCAEnB;IAED,CAAC,MAAM,CAAC,aAAa,CAAC,SAErB;IAEK,CAAC,MAAM,CAAC,YAAY,CAAC,kBAE1B;CACD;AAED,sEAAsE;AACtE,eAAO,MAAM,MAAM,eAAmB,CAAC;AAEvC,6FAA6F;AAC7F,MAAM,MAAM,IAAI,GAAG,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC;AAQxD;;;;GAIG;AACH,qBAAa,QAAQ;;IACpB,uEAAuE;IACvE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC;IAE3B,4CAA4C;IAC5C,OAAO,UAAS;IAEhB,2DAA2D;IAC3D,IAAI,UAAS;IAiBb;;;OAGG;IACH,YAAY,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,GAAG,SAAS,EAGrD;IAED;;;OAGG;IACH,IAAI,IAAI,IAAI,CAgJX;IAED;;;OAGG;IACG,KAAK,kBAqBV;CACD;AA0MD;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,WAAY;IAChC,IAAI,IAAI,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC;IACtD,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B,gDA0EA,CAAC;AAeF;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,UAElD"}
|
package/dist/markup/index.js
CHANGED
|
@@ -19,7 +19,7 @@ export class Chunk {
|
|
|
19
19
|
* @returns Safe markup to render
|
|
20
20
|
*/
|
|
21
21
|
toString() {
|
|
22
|
-
return this.raw ? this.value :
|
|
22
|
+
return this.raw ? this.value : escape(this.value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -61,7 +61,7 @@ export class Tag extends Chunk {
|
|
|
61
61
|
opening += ` ${key}`;
|
|
62
62
|
}
|
|
63
63
|
else if (typeof value === "string") {
|
|
64
|
-
opening += ` ${key}="${
|
|
64
|
+
opening += ` ${key}="${escape(value, true)}"`;
|
|
65
65
|
}
|
|
66
66
|
else if (typeof value === "number" || typeof value === "bigint") {
|
|
67
67
|
opening += ` ${key}="${value}"`;
|
|
@@ -626,96 +626,23 @@ export const readable = (render) => {
|
|
|
626
626
|
},
|
|
627
627
|
}, { highWaterMark });
|
|
628
628
|
};
|
|
629
|
+
/** Regex to find escape character for attributes */
|
|
630
|
+
const attribute = /[&"<]/g;
|
|
631
|
+
/** Regex to find escape character for content */
|
|
632
|
+
const content = /[&<]/g;
|
|
633
|
+
/** Escape character map */
|
|
634
|
+
const entities = {
|
|
635
|
+
"&": "&",
|
|
636
|
+
'"': """,
|
|
637
|
+
"<": "<",
|
|
638
|
+
};
|
|
629
639
|
/**
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
640
|
+
* Escapes text for markup.
|
|
641
|
+
*
|
|
642
|
+
* @param text Text to escape
|
|
643
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
644
|
+
* @returns Escaped text
|
|
633
645
|
*/
|
|
634
|
-
export
|
|
635
|
-
|
|
636
|
-
static #attr = /[&"<]/g;
|
|
637
|
-
/** Regex to find escape character for content */
|
|
638
|
-
static #content = /[&<]/g;
|
|
639
|
-
/** Escape character map */
|
|
640
|
-
static #map = { "&": "&", '"': """, "<": "<" };
|
|
641
|
-
/** Element to render */
|
|
642
|
-
#element;
|
|
643
|
-
/** Markup options */
|
|
644
|
-
#options;
|
|
645
|
-
/**
|
|
646
|
-
* Creates an `AsyncIterable` that renders the `Element`.
|
|
647
|
-
*
|
|
648
|
-
* @param element Element to render
|
|
649
|
-
* @param options Markup options
|
|
650
|
-
*/
|
|
651
|
-
constructor(element, options) {
|
|
652
|
-
this.#element = element;
|
|
653
|
-
this.#options = options;
|
|
654
|
-
}
|
|
655
|
-
/** @returns The element, rendered in place where a render is a child */
|
|
656
|
-
get [source]() {
|
|
657
|
-
return this.#element;
|
|
658
|
-
}
|
|
659
|
-
/** @yields Markup as the `Element` resolves */
|
|
660
|
-
async *[Symbol.asyncIterator]() {
|
|
661
|
-
const render = new Renderer(this.#element, this.#options?.raw);
|
|
662
|
-
try {
|
|
663
|
-
for (let result; (result = render.step());) {
|
|
664
|
-
if (result instanceof Chunk)
|
|
665
|
-
yield result.toString();
|
|
666
|
-
else
|
|
667
|
-
await result;
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
finally {
|
|
671
|
-
await render.close();
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
/**
|
|
675
|
-
* The render piped into a `ReadableStream`. Iterate it when possible, to
|
|
676
|
-
* avoid the overhead of the stream.
|
|
677
|
-
*
|
|
678
|
-
* @returns `ReadableStream` of markup
|
|
679
|
-
*/
|
|
680
|
-
stream() {
|
|
681
|
-
return readable(new Renderer(this.#element, this.#options?.raw));
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* The whole render as one string.
|
|
685
|
-
*
|
|
686
|
-
* ### WARNING
|
|
687
|
-
*
|
|
688
|
-
* This negates streaming benefits and buffers the result into a string.
|
|
689
|
-
*
|
|
690
|
-
* @returns Concatenated markup
|
|
691
|
-
*/
|
|
692
|
-
async string() {
|
|
693
|
-
const render = new Renderer(this.#element, this.#options?.raw);
|
|
694
|
-
const chunks = [];
|
|
695
|
-
try {
|
|
696
|
-
for (let result; (result = render.step());) {
|
|
697
|
-
if (result instanceof Chunk)
|
|
698
|
-
chunks.push(result);
|
|
699
|
-
else
|
|
700
|
-
await result;
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
finally {
|
|
704
|
-
await render.close();
|
|
705
|
-
}
|
|
706
|
-
// written once every chunk is rendered, as a group of them is
|
|
707
|
-
return new Group(chunks).toString();
|
|
708
|
-
}
|
|
709
|
-
/**
|
|
710
|
-
* Escapes text for markup.
|
|
711
|
-
*
|
|
712
|
-
* @param html String to escape
|
|
713
|
-
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
714
|
-
* @returns Escaped text
|
|
715
|
-
*/
|
|
716
|
-
static escape(html, attr) {
|
|
717
|
-
return html.replace(attr ? Markup.#attr : Markup.#content, (c) =>
|
|
718
|
-
// @ts-expect-error - private method type error
|
|
719
|
-
Markup.#map[c]);
|
|
720
|
-
}
|
|
646
|
+
export function escape(text, attr) {
|
|
647
|
+
return text.replace(attr ? attribute : content, (c) => entities[c]);
|
|
721
648
|
}
|
package/dist/render/index.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
|
-
import {
|
|
3
|
-
import { Chunk as ChunkClass,
|
|
2
|
+
import { type Options as MarkdownOptions } from "../markdown/index.js";
|
|
3
|
+
import { Chunk as ChunkClass, source } from "../markup/index.js";
|
|
4
4
|
export declare namespace Render {
|
|
5
5
|
/** A piece of a render, an interface so declaration files name it */
|
|
6
6
|
interface Chunk extends ChunkClass {
|
|
7
7
|
}
|
|
8
|
-
namespace
|
|
9
|
-
/**
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
namespace Options {
|
|
9
|
+
/** Options of a render of markup */
|
|
10
|
+
type Markup = {
|
|
11
|
+
/** Set to `true` to disable escaping, for content that is not markup */
|
|
12
|
+
raw?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/** Options of a render of Markdown */
|
|
15
|
+
type Markdown = MarkdownOptions;
|
|
15
16
|
}
|
|
16
17
|
/** Text to escape, by where it goes: one of them */
|
|
17
18
|
type Escape = {
|
|
@@ -69,7 +70,7 @@ export declare class Render {
|
|
|
69
70
|
*/
|
|
70
71
|
constructor(element: {
|
|
71
72
|
readonly markup: JSX.Element;
|
|
72
|
-
}, options?: Render.Markup
|
|
73
|
+
}, options?: Render.Options.Markup);
|
|
73
74
|
/**
|
|
74
75
|
* @param element The element, by what to render it to: `markdown`, as
|
|
75
76
|
* agents that ask for it read a page. Each top level block is written as
|
|
@@ -79,10 +80,10 @@ export declare class Render {
|
|
|
79
80
|
*/
|
|
80
81
|
constructor(element: {
|
|
81
82
|
readonly markdown: JSX.Element;
|
|
82
|
-
}, options?: Render.Markdown
|
|
83
|
+
}, options?: Render.Options.Markdown);
|
|
83
84
|
/** @returns The element, rendered in place where a render is a child */
|
|
84
85
|
get [source](): JSX.Element;
|
|
85
|
-
/** @
|
|
86
|
+
/** @yields Strings of the render, as the element resolves */
|
|
86
87
|
[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>;
|
|
87
88
|
/**
|
|
88
89
|
* The render piped into a `ReadableStream`. Iterate it when possible, to
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAEN,KAAK,OAAO,IAAI,eAAe,EAE/B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,KAAK,IAAI,UAAU,EAOnB,MAAM,EACN,MAAM,oBAAoB,CAAC;AAE5B,yBAAiB,MAAM,CAAC;IACvB,qEAAqE;IACrE,UAAiB,KAAM,SAAQ,UAAU;KAAG;IAE5C,UAAiB,OAAO,CAAC;QACxB,oCAAoC;QACpC,KAAY,MAAM,GAAG;YACpB,wEAAwE;YACxE,GAAG,CAAC,EAAE,OAAO,CAAC;SACd,CAAC;QAEF,sCAAsC;QACtC,KAAY,QAAQ,GAAG,eAAe,CAAC;KACvC;IAED,oDAAoD;IACpD,KAAY,MA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CAAC,gBAEX;IASD,6DAA6D;IACtD,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAYrE;IAED;;;;;OAKG;IACH,MAAM,gDAEL;IAED;;;;;OAKG;IACG,MAAM,oBAiBX;IAED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,KAAK,CAIxC;IAED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,UAMhC;CACD"}
|
package/dist/render/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Chunk as ChunkClass,
|
|
1
|
+
import { Blocks, escape as markdown, } from "../markdown/index.js";
|
|
2
|
+
import { Chunk as ChunkClass, Group, Renderer, Source, escape as markup, readable, source, } from "../markup/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* A render of an element, to markup such as HTML for browsers or to Markdown
|
|
5
5
|
* for agents: an `AsyncIterable` of strings, a stream, or a string. Its
|
|
@@ -17,25 +17,41 @@ import { Chunk as ChunkClass, Markup as MarkupRender, Source, source, } from "..
|
|
|
17
17
|
export class Render {
|
|
18
18
|
/** Element to render */
|
|
19
19
|
#element;
|
|
20
|
-
/**
|
|
21
|
-
#
|
|
20
|
+
/** Rendered to Markdown, otherwise to markup */
|
|
21
|
+
#markdown;
|
|
22
|
+
/** Options of either */
|
|
23
|
+
#options;
|
|
22
24
|
constructor(element, options) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
this.#element = element.markup;
|
|
29
|
-
this.#render = new MarkupRender(this.#element, options);
|
|
30
|
-
}
|
|
25
|
+
this.#markdown = "markdown" in element;
|
|
26
|
+
this.#element = "markdown" in element ? element.markdown : element.markup;
|
|
27
|
+
this.#options = options;
|
|
31
28
|
}
|
|
32
29
|
/** @returns The element, rendered in place where a render is a child */
|
|
33
30
|
get [source]() {
|
|
34
31
|
return this.#element;
|
|
35
32
|
}
|
|
36
|
-
/** @returns
|
|
37
|
-
|
|
38
|
-
return this.#
|
|
33
|
+
/** @returns What renders the element a step at a time, Markdown a block at a time */
|
|
34
|
+
#steps() {
|
|
35
|
+
return this.#markdown
|
|
36
|
+
? new Blocks(this.#element, this.#options)
|
|
37
|
+
: new Renderer(this.#element, this.#options?.raw);
|
|
38
|
+
}
|
|
39
|
+
/** @yields Strings of the render, as the element resolves */
|
|
40
|
+
async *[Symbol.asyncIterator]() {
|
|
41
|
+
const steps = this.#steps();
|
|
42
|
+
try {
|
|
43
|
+
for (let result; (result = steps.step());) {
|
|
44
|
+
if (typeof result === "string")
|
|
45
|
+
yield result;
|
|
46
|
+
else if (result instanceof ChunkClass)
|
|
47
|
+
yield result.toString();
|
|
48
|
+
else
|
|
49
|
+
await result;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
await steps.close();
|
|
54
|
+
}
|
|
39
55
|
}
|
|
40
56
|
/**
|
|
41
57
|
* The render piped into a `ReadableStream`. Iterate it when possible, to
|
|
@@ -44,7 +60,7 @@ export class Render {
|
|
|
44
60
|
* @returns `ReadableStream` of the render
|
|
45
61
|
*/
|
|
46
62
|
stream() {
|
|
47
|
-
return this.#
|
|
63
|
+
return readable(this.#steps());
|
|
48
64
|
}
|
|
49
65
|
/**
|
|
50
66
|
* The whole render as one string, which buffers it and gives up the
|
|
@@ -52,8 +68,25 @@ export class Render {
|
|
|
52
68
|
*
|
|
53
69
|
* @returns The render, concatenated
|
|
54
70
|
*/
|
|
55
|
-
string() {
|
|
56
|
-
|
|
71
|
+
async string() {
|
|
72
|
+
const steps = this.#steps();
|
|
73
|
+
const chunks = [];
|
|
74
|
+
let text = "";
|
|
75
|
+
try {
|
|
76
|
+
for (let result; (result = steps.step());) {
|
|
77
|
+
if (typeof result === "string")
|
|
78
|
+
text += result;
|
|
79
|
+
else if (result instanceof ChunkClass)
|
|
80
|
+
chunks.push(result);
|
|
81
|
+
else
|
|
82
|
+
await result;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
await steps.close();
|
|
87
|
+
}
|
|
88
|
+
// markup is written once every chunk is rendered, as a group of them is
|
|
89
|
+
return this.#markdown ? text : new Group(chunks).toString();
|
|
57
90
|
}
|
|
58
91
|
/**
|
|
59
92
|
* Writes markup, Markdown, or both as they are, dangerously bypassing
|
|
@@ -90,9 +123,9 @@ export class Render {
|
|
|
90
123
|
*/
|
|
91
124
|
static escape(text) {
|
|
92
125
|
return text.markdown !== undefined
|
|
93
|
-
?
|
|
126
|
+
? markdown(text.markdown)
|
|
94
127
|
: text.attr !== undefined
|
|
95
|
-
?
|
|
96
|
-
:
|
|
128
|
+
? markup(text.attr, true)
|
|
129
|
+
: markup(text.markup);
|
|
97
130
|
}
|
|
98
131
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ovr",
|
|
3
3
|
"description": "Web apps anyone can use. Each route serves people a page and agents Markdown and MCP tools, with forms, Postgres, and passkey sign-in built in.",
|
|
4
|
-
"version": "7.0.0-next.
|
|
4
|
+
"version": "7.0.0-next.38",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
package/src/context/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { App } from "../app/index.js";
|
|
2
2
|
import { Cookie } from "../cookie/index.js";
|
|
3
3
|
import { type Field, Form as FormSchema, forms } from "../form/index.js";
|
|
4
|
-
import {
|
|
5
|
-
import { Markup } from "../markup/index.js";
|
|
4
|
+
import { type Options, tools } from "../markdown/index.js";
|
|
6
5
|
import type { Middleware } from "../middleware/index.js";
|
|
7
6
|
import { Multipart } from "../multipart/index.js";
|
|
7
|
+
import { Render } from "../render/index.js";
|
|
8
8
|
import { Route } from "../route/index.js";
|
|
9
9
|
import { Checksum, Header, Method, Mime, type Util } from "../util/index.js";
|
|
10
10
|
|
|
@@ -21,7 +21,7 @@ export const agent = new WeakMap<
|
|
|
21
21
|
readonly sent?: ReadonlySet<string>;
|
|
22
22
|
issues?: readonly { readonly path: string; readonly message: string }[];
|
|
23
23
|
/** The tool a form submits to and its arguments, so a page points to it */
|
|
24
|
-
readonly tool?:
|
|
24
|
+
readonly tool?: Options[typeof tools];
|
|
25
25
|
}
|
|
26
26
|
>();
|
|
27
27
|
|
|
@@ -450,19 +450,22 @@ export class Context<
|
|
|
450
450
|
) &&
|
|
451
451
|
this.#markdown()
|
|
452
452
|
) {
|
|
453
|
-
res.body = new
|
|
454
|
-
|
|
455
|
-
[tools]: agent.get(this.req)?.tool,
|
|
456
|
-
|
|
453
|
+
res.body = new Render(
|
|
454
|
+
{ markdown: r },
|
|
455
|
+
{ base: this.url, [tools]: agent.get(this.req)?.tool },
|
|
456
|
+
).stream();
|
|
457
457
|
res.headers.set(Header.name.type, Header.utf8(Mime.type.md));
|
|
458
458
|
return;
|
|
459
459
|
}
|
|
460
460
|
}
|
|
461
461
|
|
|
462
|
-
res.body = new
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
462
|
+
res.body = new Render(
|
|
463
|
+
{ markup: r },
|
|
464
|
+
{
|
|
465
|
+
// other defined types are not markup, so they are not escaped
|
|
466
|
+
raw: Boolean(mime && !Mime.markup(mime)),
|
|
467
|
+
},
|
|
468
|
+
).stream();
|
|
466
469
|
|
|
467
470
|
if (!mime) {
|
|
468
471
|
// default to HTML
|
package/src/markdown/index.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
|
-
import {
|
|
3
|
-
type Chunk,
|
|
4
|
-
Group,
|
|
5
|
-
Renderer,
|
|
6
|
-
Source,
|
|
7
|
-
Tag,
|
|
8
|
-
readable,
|
|
9
|
-
} from "../markup/index.js";
|
|
2
|
+
import { type Chunk, Group, Renderer, Source, Tag } from "../markup/index.js";
|
|
10
3
|
|
|
11
4
|
/** Parsed element */
|
|
12
5
|
type Node = {
|
|
@@ -50,40 +43,38 @@ type Field = {
|
|
|
50
43
|
/** Internal protocol for the MCP endpoint, to write a form as its tool's call. */
|
|
51
44
|
export const tools = Symbol("tools");
|
|
52
45
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
86
|
-
}
|
|
46
|
+
/** Markdown conversion options */
|
|
47
|
+
export type Options = {
|
|
48
|
+
/** Resolves relative links and form actions, usually the request URL */
|
|
49
|
+
readonly base?: string | URL | undefined;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Finds the tool that submits a form, if there is one, set by
|
|
53
|
+
* an MCP endpoint. A form is then written as the call to its tool, with
|
|
54
|
+
* the arguments its address holds, its values, and its messages. A form
|
|
55
|
+
* without a tool is left out: the agent has no way to submit it.
|
|
56
|
+
*
|
|
57
|
+
* @param method Form method
|
|
58
|
+
* @param action Resolved form action
|
|
59
|
+
* @returns The tool's name, the arguments from the address's parameters,
|
|
60
|
+
* the fields whose choices are given as the form renders, which its
|
|
61
|
+
* input schema can't list, and the fields it takes as an array, which
|
|
62
|
+
* the form shows as rows or lines
|
|
63
|
+
*/
|
|
64
|
+
readonly [tools]?:
|
|
65
|
+
| ((
|
|
66
|
+
method: string,
|
|
67
|
+
action: URL,
|
|
68
|
+
) =>
|
|
69
|
+
| {
|
|
70
|
+
name: string;
|
|
71
|
+
args: Record<string, string>;
|
|
72
|
+
choices: ReadonlySet<string>;
|
|
73
|
+
lists: ReadonlySet<string>;
|
|
74
|
+
}
|
|
75
|
+
| undefined)
|
|
76
|
+
| undefined;
|
|
77
|
+
};
|
|
87
78
|
|
|
88
79
|
/** Writes Markdown from rendered chunks, one block at a time */
|
|
89
80
|
class Writer {
|
|
@@ -162,7 +153,7 @@ class Writer {
|
|
|
162
153
|
/** Attributes of an element without any, never changed */
|
|
163
154
|
static readonly #none: Record<string, string> = Object.freeze({});
|
|
164
155
|
|
|
165
|
-
readonly #options:
|
|
156
|
+
readonly #options: Options;
|
|
166
157
|
|
|
167
158
|
/** Open elements below the top level */
|
|
168
159
|
readonly #stack: Node[] = [];
|
|
@@ -195,7 +186,7 @@ class Writer {
|
|
|
195
186
|
#count = 0;
|
|
196
187
|
|
|
197
188
|
/** @param options Conversion options */
|
|
198
|
-
constructor(options:
|
|
189
|
+
constructor(options: Options = {}) {
|
|
199
190
|
this.#options = options;
|
|
200
191
|
}
|
|
201
192
|
|
|
@@ -1431,7 +1422,7 @@ class Writer {
|
|
|
1431
1422
|
* Renders an element to Markdown a step at a time, as a render of markup
|
|
1432
1423
|
* steps through its chunks, so neither waits unless the element does.
|
|
1433
1424
|
*/
|
|
1434
|
-
class Blocks {
|
|
1425
|
+
export class Blocks {
|
|
1435
1426
|
/** Render of the element's chunks */
|
|
1436
1427
|
readonly #render: Renderer;
|
|
1437
1428
|
|
|
@@ -1445,7 +1436,7 @@ class Blocks {
|
|
|
1445
1436
|
* @param element Element to render
|
|
1446
1437
|
* @param options Markdown options
|
|
1447
1438
|
*/
|
|
1448
|
-
constructor(element: JSX.Element, options?:
|
|
1439
|
+
constructor(element: JSX.Element, options?: Options) {
|
|
1449
1440
|
this.#render = new Renderer(element, undefined);
|
|
1450
1441
|
this.#writer = new Writer(options);
|
|
1451
1442
|
}
|
|
@@ -1476,79 +1467,9 @@ class Blocks {
|
|
|
1476
1467
|
}
|
|
1477
1468
|
|
|
1478
1469
|
/**
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
* made with `new Render`. Each top level block is
|
|
1482
|
-
* written as soon as it closes, and when the page has a `<main>` element, only
|
|
1483
|
-
* its contents are kept. Raw HTML is written as it is.
|
|
1484
|
-
*
|
|
1485
|
-
* ```tsx
|
|
1486
|
-
* const markdown = await new Render({ markdown: <Post post={post} /> }, { base: url }).string();
|
|
1487
|
-
* ```
|
|
1470
|
+
* @param text Plain text
|
|
1471
|
+
* @returns The text escaped, to write in Markdown as it is
|
|
1488
1472
|
*/
|
|
1489
|
-
export
|
|
1490
|
-
|
|
1491
|
-
* @param text Plain text
|
|
1492
|
-
* @returns The text escaped, to write in Markdown as it is
|
|
1493
|
-
*/
|
|
1494
|
-
static escape(text: string) {
|
|
1495
|
-
return Writer.escape(text);
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
/** Element to render */
|
|
1499
|
-
readonly #element: JSX.Element;
|
|
1500
|
-
|
|
1501
|
-
/** Markdown options */
|
|
1502
|
-
readonly #options: Markdown.Options | undefined;
|
|
1503
|
-
|
|
1504
|
-
/**
|
|
1505
|
-
* Creates an `AsyncIterable` that renders the element to Markdown.
|
|
1506
|
-
*
|
|
1507
|
-
* @param element Element to render
|
|
1508
|
-
* @param options Markdown options
|
|
1509
|
-
*/
|
|
1510
|
-
constructor(element: JSX.Element, options?: Markdown.Options) {
|
|
1511
|
-
this.#element = element;
|
|
1512
|
-
this.#options = options;
|
|
1513
|
-
}
|
|
1514
|
-
|
|
1515
|
-
/** @yields Markdown as each block completes */
|
|
1516
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown> {
|
|
1517
|
-
const blocks = new Blocks(this.#element, this.#options);
|
|
1518
|
-
|
|
1519
|
-
try {
|
|
1520
|
-
for (let result; (result = blocks.step());) {
|
|
1521
|
-
if (typeof result === "string") yield result;
|
|
1522
|
-
else await result;
|
|
1523
|
-
}
|
|
1524
|
-
} finally {
|
|
1525
|
-
await blocks.close();
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
|
|
1529
|
-
/** @returns `ReadableStream` of Markdown */
|
|
1530
|
-
stream() {
|
|
1531
|
-
return readable(new Blocks(this.#element, this.#options));
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
/**
|
|
1535
|
-
* The whole render as one string.
|
|
1536
|
-
*
|
|
1537
|
-
* @returns Concatenated Markdown
|
|
1538
|
-
*/
|
|
1539
|
-
async string() {
|
|
1540
|
-
const blocks = new Blocks(this.#element, this.#options);
|
|
1541
|
-
let markdown = "";
|
|
1542
|
-
|
|
1543
|
-
try {
|
|
1544
|
-
for (let result; (result = blocks.step());) {
|
|
1545
|
-
if (typeof result === "string") markdown += result;
|
|
1546
|
-
else await result;
|
|
1547
|
-
}
|
|
1548
|
-
} finally {
|
|
1549
|
-
await blocks.close();
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
return markdown;
|
|
1553
|
-
}
|
|
1473
|
+
export function escape(text: string) {
|
|
1474
|
+
return Writer.escape(text);
|
|
1554
1475
|
}
|
package/src/markup/index.ts
CHANGED
|
@@ -24,7 +24,7 @@ export class Chunk {
|
|
|
24
24
|
* @returns Safe markup to render
|
|
25
25
|
*/
|
|
26
26
|
toString(): string {
|
|
27
|
-
return this.raw ? this.value :
|
|
27
|
+
return this.raw ? this.value : escape(this.value);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -72,7 +72,7 @@ export class Tag extends Chunk {
|
|
|
72
72
|
// just put the key without the value
|
|
73
73
|
opening += ` ${key}`;
|
|
74
74
|
} else if (typeof value === "string") {
|
|
75
|
-
opening += ` ${key}="${
|
|
75
|
+
opening += ` ${key}="${escape(value, true)}"`;
|
|
76
76
|
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
77
77
|
opening += ` ${key}="${value}"`;
|
|
78
78
|
}
|
|
@@ -738,114 +738,26 @@ export const readable = (render: {
|
|
|
738
738
|
);
|
|
739
739
|
};
|
|
740
740
|
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
export type Options = {
|
|
744
|
-
/** Set to `true` to disable escaping, for content that is not markup */
|
|
745
|
-
raw?: boolean;
|
|
746
|
-
};
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
* Renders an element to markup, such as HTML, SVG, or XML, as an
|
|
751
|
-
* `AsyncIterable` of strings, a stream, or a string. `Markdown` renders the
|
|
752
|
-
* same elements to Markdown. Internal to ovr, made with `new Render`.
|
|
753
|
-
*/
|
|
754
|
-
export class Markup {
|
|
755
|
-
/** Regex to find escape character for attributes */
|
|
756
|
-
static readonly #attr = /[&"<]/g;
|
|
757
|
-
|
|
758
|
-
/** Regex to find escape character for content */
|
|
759
|
-
static readonly #content = /[&<]/g;
|
|
760
|
-
|
|
761
|
-
/** Escape character map */
|
|
762
|
-
static readonly #map = { "&": "&", '"': """, "<": "<" };
|
|
763
|
-
|
|
764
|
-
/** Element to render */
|
|
765
|
-
readonly #element: JSX.Element;
|
|
766
|
-
|
|
767
|
-
/** Markup options */
|
|
768
|
-
readonly #options: Markup.Options | undefined;
|
|
769
|
-
|
|
770
|
-
/**
|
|
771
|
-
* Creates an `AsyncIterable` that renders the `Element`.
|
|
772
|
-
*
|
|
773
|
-
* @param element Element to render
|
|
774
|
-
* @param options Markup options
|
|
775
|
-
*/
|
|
776
|
-
constructor(element: JSX.Element, options?: Markup.Options) {
|
|
777
|
-
this.#element = element;
|
|
778
|
-
this.#options = options;
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
/** @returns The element, rendered in place where a render is a child */
|
|
782
|
-
get [source]() {
|
|
783
|
-
return this.#element;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
/** @yields Markup as the `Element` resolves */
|
|
787
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown> {
|
|
788
|
-
const render = new Renderer(this.#element, this.#options?.raw);
|
|
789
|
-
|
|
790
|
-
try {
|
|
791
|
-
for (let result: Step; (result = render.step());) {
|
|
792
|
-
if (result instanceof Chunk) yield result.toString();
|
|
793
|
-
else await result;
|
|
794
|
-
}
|
|
795
|
-
} finally {
|
|
796
|
-
await render.close();
|
|
797
|
-
}
|
|
798
|
-
}
|
|
741
|
+
/** Regex to find escape character for attributes */
|
|
742
|
+
const attribute = /[&"<]/g;
|
|
799
743
|
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
* avoid the overhead of the stream.
|
|
803
|
-
*
|
|
804
|
-
* @returns `ReadableStream` of markup
|
|
805
|
-
*/
|
|
806
|
-
stream() {
|
|
807
|
-
return readable(new Renderer(this.#element, this.#options?.raw));
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
/**
|
|
811
|
-
* The whole render as one string.
|
|
812
|
-
*
|
|
813
|
-
* ### WARNING
|
|
814
|
-
*
|
|
815
|
-
* This negates streaming benefits and buffers the result into a string.
|
|
816
|
-
*
|
|
817
|
-
* @returns Concatenated markup
|
|
818
|
-
*/
|
|
819
|
-
async string() {
|
|
820
|
-
const render = new Renderer(this.#element, this.#options?.raw);
|
|
821
|
-
const chunks: Chunk[] = [];
|
|
744
|
+
/** Regex to find escape character for content */
|
|
745
|
+
const content = /[&<]/g;
|
|
822
746
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
await render.close();
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
// written once every chunk is rendered, as a group of them is
|
|
833
|
-
return new Group(chunks).toString();
|
|
834
|
-
}
|
|
747
|
+
/** Escape character map */
|
|
748
|
+
const entities: Record<string, string> = {
|
|
749
|
+
"&": "&",
|
|
750
|
+
'"': """,
|
|
751
|
+
"<": "<",
|
|
752
|
+
};
|
|
835
753
|
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
attr ? Markup.#attr : Markup.#content,
|
|
846
|
-
(c) =>
|
|
847
|
-
// @ts-expect-error - private method type error
|
|
848
|
-
Markup.#map[c],
|
|
849
|
-
);
|
|
850
|
-
}
|
|
754
|
+
/**
|
|
755
|
+
* Escapes text for markup.
|
|
756
|
+
*
|
|
757
|
+
* @param text Text to escape
|
|
758
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
759
|
+
* @returns Escaped text
|
|
760
|
+
*/
|
|
761
|
+
export function escape(text: string, attr?: boolean) {
|
|
762
|
+
return text.replace(attr ? attribute : content, (c) => entities[c]!);
|
|
851
763
|
}
|
package/src/render/index.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Blocks,
|
|
4
|
+
type Options as MarkdownOptions,
|
|
5
|
+
escape as markdown,
|
|
6
|
+
} from "../markdown/index.js";
|
|
3
7
|
import {
|
|
4
8
|
Chunk as ChunkClass,
|
|
5
|
-
|
|
9
|
+
Group,
|
|
10
|
+
Renderer,
|
|
6
11
|
Source,
|
|
12
|
+
type Step,
|
|
13
|
+
escape as markup,
|
|
14
|
+
readable,
|
|
7
15
|
source,
|
|
8
16
|
} from "../markup/index.js";
|
|
9
17
|
|
|
@@ -11,14 +19,15 @@ export namespace Render {
|
|
|
11
19
|
/** A piece of a render, an interface so declaration files name it */
|
|
12
20
|
export interface Chunk extends ChunkClass {}
|
|
13
21
|
|
|
14
|
-
export namespace
|
|
15
|
-
/**
|
|
16
|
-
export type
|
|
17
|
-
|
|
22
|
+
export namespace Options {
|
|
23
|
+
/** Options of a render of markup */
|
|
24
|
+
export type Markup = {
|
|
25
|
+
/** Set to `true` to disable escaping, for content that is not markup */
|
|
26
|
+
raw?: boolean;
|
|
27
|
+
};
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export type Options = MarkdownRender.Options;
|
|
29
|
+
/** Options of a render of Markdown */
|
|
30
|
+
export type Markdown = MarkdownOptions;
|
|
22
31
|
}
|
|
23
32
|
|
|
24
33
|
/** Text to escape, by where it goes: one of them */
|
|
@@ -79,8 +88,12 @@ export class Render {
|
|
|
79
88
|
/** Element to render */
|
|
80
89
|
readonly #element: JSX.Element;
|
|
81
90
|
|
|
82
|
-
/**
|
|
83
|
-
readonly #
|
|
91
|
+
/** Rendered to Markdown, otherwise to markup */
|
|
92
|
+
readonly #markdown: boolean;
|
|
93
|
+
|
|
94
|
+
/** Options of either */
|
|
95
|
+
readonly #options:
|
|
96
|
+
(Render.Options.Markup & Render.Options.Markdown) | undefined;
|
|
84
97
|
|
|
85
98
|
/**
|
|
86
99
|
* @param element The element, by what to render it to: `markup`, such as
|
|
@@ -89,7 +102,7 @@ export class Render {
|
|
|
89
102
|
*/
|
|
90
103
|
constructor(
|
|
91
104
|
element: { readonly markup: JSX.Element },
|
|
92
|
-
options?: Render.Markup
|
|
105
|
+
options?: Render.Options.Markup,
|
|
93
106
|
);
|
|
94
107
|
/**
|
|
95
108
|
* @param element The element, by what to render it to: `markdown`, as
|
|
@@ -100,20 +113,16 @@ export class Render {
|
|
|
100
113
|
*/
|
|
101
114
|
constructor(
|
|
102
115
|
element: { readonly markdown: JSX.Element },
|
|
103
|
-
options?: Render.Markdown
|
|
116
|
+
options?: Render.Options.Markdown,
|
|
104
117
|
);
|
|
105
118
|
constructor(
|
|
106
119
|
element:
|
|
107
120
|
{ readonly markup: JSX.Element } | { readonly markdown: JSX.Element },
|
|
108
|
-
options?: Render.Markup
|
|
121
|
+
options?: Render.Options.Markup & Render.Options.Markdown,
|
|
109
122
|
) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
} else {
|
|
114
|
-
this.#element = element.markup;
|
|
115
|
-
this.#render = new MarkupRender(this.#element, options);
|
|
116
|
-
}
|
|
123
|
+
this.#markdown = "markdown" in element;
|
|
124
|
+
this.#element = "markdown" in element ? element.markdown : element.markup;
|
|
125
|
+
this.#options = options;
|
|
117
126
|
}
|
|
118
127
|
|
|
119
128
|
/** @returns The element, rendered in place where a render is a child */
|
|
@@ -121,9 +130,26 @@ export class Render {
|
|
|
121
130
|
return this.#element;
|
|
122
131
|
}
|
|
123
132
|
|
|
124
|
-
/** @returns
|
|
125
|
-
|
|
126
|
-
return this.#
|
|
133
|
+
/** @returns What renders the element a step at a time, Markdown a block at a time */
|
|
134
|
+
#steps() {
|
|
135
|
+
return this.#markdown
|
|
136
|
+
? new Blocks(this.#element, this.#options)
|
|
137
|
+
: new Renderer(this.#element, this.#options?.raw);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** @yields Strings of the render, as the element resolves */
|
|
141
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<string, void, unknown> {
|
|
142
|
+
const steps = this.#steps();
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
for (let result: Step | string; (result = steps.step());) {
|
|
146
|
+
if (typeof result === "string") yield result;
|
|
147
|
+
else if (result instanceof ChunkClass) yield result.toString();
|
|
148
|
+
else await result;
|
|
149
|
+
}
|
|
150
|
+
} finally {
|
|
151
|
+
await steps.close();
|
|
152
|
+
}
|
|
127
153
|
}
|
|
128
154
|
|
|
129
155
|
/**
|
|
@@ -133,7 +159,7 @@ export class Render {
|
|
|
133
159
|
* @returns `ReadableStream` of the render
|
|
134
160
|
*/
|
|
135
161
|
stream() {
|
|
136
|
-
return this.#
|
|
162
|
+
return readable(this.#steps());
|
|
137
163
|
}
|
|
138
164
|
|
|
139
165
|
/**
|
|
@@ -142,8 +168,23 @@ export class Render {
|
|
|
142
168
|
*
|
|
143
169
|
* @returns The render, concatenated
|
|
144
170
|
*/
|
|
145
|
-
string() {
|
|
146
|
-
|
|
171
|
+
async string() {
|
|
172
|
+
const steps = this.#steps();
|
|
173
|
+
const chunks: ChunkClass[] = [];
|
|
174
|
+
let text = "";
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
for (let result: Step | string; (result = steps.step());) {
|
|
178
|
+
if (typeof result === "string") text += result;
|
|
179
|
+
else if (result instanceof ChunkClass) chunks.push(result);
|
|
180
|
+
else await result;
|
|
181
|
+
}
|
|
182
|
+
} finally {
|
|
183
|
+
await steps.close();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// markup is written once every chunk is rendered, as a group of them is
|
|
187
|
+
return this.#markdown ? text : new Group(chunks).toString();
|
|
147
188
|
}
|
|
148
189
|
|
|
149
190
|
/**
|
|
@@ -182,9 +223,9 @@ export class Render {
|
|
|
182
223
|
*/
|
|
183
224
|
static escape(text: Render.Escape) {
|
|
184
225
|
return text.markdown !== undefined
|
|
185
|
-
?
|
|
226
|
+
? markdown(text.markdown)
|
|
186
227
|
: text.attr !== undefined
|
|
187
|
-
?
|
|
188
|
-
:
|
|
228
|
+
? markup(text.attr, true)
|
|
229
|
+
: markup(text.markup);
|
|
189
230
|
}
|
|
190
231
|
}
|