-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl-jsfiddle.ts
49 lines (39 loc) · 1.04 KB
/
dl-jsfiddle.ts
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
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const parsedArgs = parse(Deno.args);
const { fiddle } = parsedArgs;
const TEMPLATE = (
{
html,
css,
js,
}: Record<"html" | "css" | "js", string>,
) =>
`<!DOCTYPE html>
<body>
${html}
<style>
${css}
</style>
<script type="${/import.*from/.test(js) ? "module" : ""}">
${js}
</script>
</body>
`;
if (fiddle) {
const res = await fetch(`https://jsfiddle.net/${fiddle}`);
const html = await res.text();
const doc = new DOMParser().parseFromString(html, "text/html");
const firstScript = doc?.querySelector("script")?.innerHTML;
if (firstScript) {
const tempFilePath = await Deno.makeTempFile();
await Deno.writeTextFile(
tempFilePath,
`${firstScript} export default EditorConfig;`,
);
const { css, html, js } = (await import(`file://${tempFilePath}`))
.default.value;
console.log(TEMPLATE({ css, html, js }));
await Deno.remove(tempFilePath);
}
}