Refactoring about how proxy fetching happens

This commit is contained in:
Zef Hemel
2023-08-23 19:08:21 +02:00
parent 38a95d2382
commit 5a88254cde
18 changed files with 320 additions and 93 deletions
+10 -10
View File
@@ -8,13 +8,13 @@ import { CodeWidgetT } from "../web/hooks/code_widget.ts";
import { MQHookT } from "../plugos/hooks/mq.ts";
import { EndpointHookT } from "../plugos/hooks/endpoint.ts";
/** Silverbullet hooks give plugs access to silverbullet core systems.
*
/** Silverbullet hooks give plugs access to silverbullet core systems.
*
* Hooks are associated with typescript functions through a manifest file.
* On various triggers (user enters a slash command, an HTTP endpoint is hit, user clicks, etc) the typescript function is called.
*
*
* related: plugos/type.ts#FunctionDef
*/
*/
export type SilverBulletHooks =
& CommandHookT
& SlashCommandHookT
@@ -28,7 +28,7 @@ export type SilverBulletHooks =
/** Syntax extension allow plugs to declaratively add new *inline* parse tree nodes to the markdown parser. */
export type SyntaxExtensions = {
/** A map of node **name** (also called "type"), to parsing and highlighting instructions. Each entry defines a new node. By convention node names (types) are UpperCamelCase (PascalCase).
*
*
* see: plug-api/lib/tree.ts#ParseTree
*/
syntax?: { [key: string]: NodeDef };
@@ -37,21 +37,21 @@ export type SyntaxExtensions = {
/** Parsing and highlighting instructions for SyntaxExtension */
export type NodeDef = {
/** An array of possible first characters to begin matching on.
*
*
* **Example**: If this node has the regex '[abc][123]', NodeDef.firstCharacters should be ["a", "b", "c"].
*/
*/
firstCharacters: string[];
/** A regular expression that matches the *entire* syntax, including the first character. */
regex: string;
/** CSS styles to apply to the matched text.
*
*
* Key-value pair of CSS key to value:
*
*
* **Example**: `backgroundColor: "rgba(22,22,22,0.07)"`
*/
styles: { [key: string]: string };
styles?: { [key: string]: string };
/** CSS class name to apply to the matched text */
className?: string;
+14 -10
View File
@@ -1,7 +1,7 @@
import { Tag } from "../deps.ts";
import type { MarkdownConfig } from "../deps.ts";
import { System } from "../../plugos/system.ts";
import { Manifest } from "../manifest.ts";
import { Manifest, NodeDef } from "../manifest.ts";
export type MDExt = {
// unicode char code for efficiency .charCodeAt(0)
@@ -9,7 +9,7 @@ export type MDExt = {
regex: RegExp;
nodeType: string;
tag: Tag;
styles: { [key: string]: string };
styles?: { [key: string]: string };
className?: string;
};
@@ -53,16 +53,20 @@ export function loadMarkdownExtensions(system: System<any>): MDExt[] {
const manifest = plug.manifest as Manifest;
if (manifest.syntax) {
for (const [nodeType, def] of Object.entries(manifest.syntax)) {
mdExtensions.push({
nodeType,
tag: Tag.define(),
firstCharCodes: def.firstCharacters.map((ch) => ch.charCodeAt(0)),
regex: new RegExp("^" + def.regex),
styles: def.styles,
className: def.className,
});
mdExtensions.push(nodeDefToMDExt(nodeType, def));
}
}
}
return mdExtensions;
}
export function nodeDefToMDExt(nodeType: string, def: NodeDef): MDExt {
return {
nodeType,
tag: Tag.define(),
firstCharCodes: def.firstCharacters.map((ch) => ch.charCodeAt(0)),
regex: new RegExp("^" + def.regex),
styles: def.styles,
className: def.className,
};
}
+3 -3
View File
@@ -1,9 +1,9 @@
import { base64Encode } from "../plugos/asset_bundle/base64.ts";
import { base64Decode, base64Encode } from "../plugos/asset_bundle/base64.ts";
export type ProxyFetchRequest = {
method?: string;
headers?: Record<string, string>;
body?: string;
base64Body?: string;
};
export type ProxyFetchResponse = {
@@ -23,7 +23,7 @@ export async function performLocalFetch(
req && {
method: req.method,
headers: req.headers,
body: req.body,
body: req.base64Body && base64Decode(req.base64Body),
},
);
return {