mirror of
https://github.com/cloudflare/cloudflare-docs.git
synced 2026-01-11 20:06:58 +00:00
* [Docs Site] Adopt eslint * Demonstrate a fixable suggestion, add VSCode plugin and package.json script * Fix slice in ModelCatalog * Remove test error in AnchorHeading * recreate package-lock.json * update new .jsx components to .tsx * amend deps, fix react types, organise ec plugins * another attempt at fixing platform-specific deps * fix FieldCatalog filters, remove test block from code.mdx * use opacity instead of brightness for ruleid * fix lockfile * amend ruleid opacity styling * test onetrust * enable prefer const rule, remove onetrust test * add save-dev
80 lines
1.9 KiB
JavaScript
80 lines
1.9 KiB
JavaScript
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()}`,
|
|
);
|
|
}
|
|
|
|
export default () => {
|
|
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;
|
|
},
|
|
},
|
|
});
|
|
};
|