forked from cloudflare/cloudflare-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec.config.mjs
156 lines (134 loc) · 3.74 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
},
},
});
}
function outputCodeblocks() {
return definePlugin({
name: "Adds the '.code-output' class if 'output' is passed on the opening codefence.",
hooks: {
preprocessMetadata: async (context) => {
if (!context.codeBlock.meta.includes("output")) return;
context.codeBlock.props.frame = "none";
},
postprocessRenderedBlock: async (context) => {
if (!context.codeBlock.meta.includes("output")) return;
context.renderData.blockAst.properties.className.push("code-output");
context.addStyles(`
div.expressive-code:has(figure.code-output) {
margin-top: 0 !important;
}
.code-output .copy {
display: none !important;
}
.code-output > pre {
border-top-width: 0 !important;
background: var(--sl-color-gray-6) !important;
}
.code-output > pre > code {
user-select: none;
transition: opacity 0.5s ease;
}
.code-output > pre > code:hover {
cursor: default;
opacity: 0.5;
}
`);
},
},
});
}
function defaultLanguageTitles() {
return definePlugin({
name: "Adds language-specific default titles.",
hooks: {
preprocessLanguage: async (context) => {
switch (context.codeBlock.language) {
case "powershell": {
context.codeBlock.props.title ??= "PowerShell";
break;
}
default: {
return;
}
}
},
},
});
}
export default {
plugins: [
workersPlaygroundButton(),
outputCodeblocks(),
defaultLanguageTitles(),
],
themes: [darkTheme, lightTheme],
styleOverrides: {
textMarkers: {
defaultLuminance: ["32%", "88%"],
},
},
};