-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed_example.ts
More file actions
35 lines (30 loc) · 991 Bytes
/
embed_example.ts
File metadata and controls
35 lines (30 loc) · 991 Bytes
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
/**
* This script embeds the example code from `example.ts` into the `html.ts` file.
*/
const rawExample = Deno.readFileSync(
new URL("example.ts", import.meta.url),
);
const sourceExample = new TextDecoder().decode(rawExample).trim();
const exampleReplacement = sourceExample
.replaceAll(/^/gm, " * ")
.replaceAll(/^ \* $/gm, " *") + "\n";
const startMarker = `\
* filename: \`example.ts\`
* \`\`\`ts
`;
const endMarker = " * ```\n";
const rawSource = Deno.readFileSync(new URL("html.ts", import.meta.url));
const source = new TextDecoder().decode(rawSource);
const startPos = source.indexOf(startMarker);
if (startPos === -1) {
throw new Error("Start marker not found");
}
const endPos = source.indexOf(endMarker, startPos);
if (endPos === -1) {
throw new Error("End marker not found");
}
const newSource = source.slice(0, startPos) +
startMarker +
exampleReplacement +
source.slice(endPos);
Deno.writeTextFileSync(new URL("html.ts", import.meta.url), newSource);