forked from cloudflare/cloudflare-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec.config.mjs
93 lines (77 loc) · 2.33 KB
/
ec.config.mjs
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
87
88
89
90
91
92
93
import darkTheme from "solarflare-theme/themes/cloudflare-dark-color-theme.json" with { type: "json" };
import lightTheme from "solarflare-theme/themes/cloudflare-light-color-theme.json" with { type: "json" };
import { definePlugin } from "@expressive-code/core";
import { h } from "@expressive-code/core/hast";
import lzstring from "lz-string";
export function serialiseWorker(code) {
const formData = new FormData();
const metadata = {
main_module: "index.js",
};
formData.set(
"index.js",
new Blob([code], {
type: "application/javascript+module",
}),
"index.js",
);
formData.set(
"metadata",
new Blob([JSON.stringify(metadata)], { type: "application/json" }),
);
return formData;
}
export async function compressWorker(worker) {
const serialisedWorker = new Response(worker);
return lzstring.compressToEncodedURIComponent(
`${serialisedWorker.headers.get(
"content-type",
)}:${await serialisedWorker.text()}`,
);
}
function workersPlaygroundButton() {
return definePlugin({
name: "Adds 'Run Worker' button to JS codeblocks",
baseStyles: `
.run {
display: flex;
gap: 0.25rem;
flex-direction: row;
position: absolute;
inset-block-start: calc(var(--ec-brdWd) + var(--button-spacing));
inset-inline-end: calc(var(--ec-brdWd) + var(--ec-uiPadInl) * 3);
direction: ltr;
unicode-bidi: isolate;
text-decoration-color: var(--sl-color-accent);
span {
color: var(--sl-color-white);
font-family: var(--sl-font-system);
}
}
`,
hooks: {
postprocessRenderedBlock: async (context) => {
if (!context.codeBlock.meta.includes("playground")) return;
const serialised = await compressWorker(
serialiseWorker(context.codeBlock.code),
);
const url = `https://workers.cloudflare.com/playground#${serialised}`;
const runButton = h("a.run", { href: url, target: "__blank" }, [
h("span", "Run Worker in Playground"),
]);
const ast = context.renderData.blockAst;
ast.children.push(runButton);
context.renderData.blockAst = ast;
},
},
});
}
export default {
plugins: [workersPlaygroundButton()],
themes: [darkTheme, lightTheme],
styleOverrides: {
textMarkers: {
defaultLuminance: ["32%", "88%"],
},
},
};