-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplantuml.ts
executable file
·63 lines (59 loc) · 1.83 KB
/
plantuml.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { editor, shell, system } from "@silverbulletmd/silverbullet/syscalls";
import { readSetting } from "$sb/lib/settings_page.ts";
import plantumlEncoder from "npm:plantuml-encoder";
export async function pumllocal(generator: string, uml: string) {
try {
const buml = btoa(uml)
const { stdout, stderr } = await shell.run(generator, [buml]);
console.log(stderr)
return stdout;
} catch (error) {
console.error("PUML generation failed", error)
return error
// We can ignore, this happens when there's no changes to commit
}
return "";
}
export async function pumlserver(serverurl: string, uml: string) {
try {
let encoded = plantumlEncoder.encode(uml)
let sep = "/"
if (serverurl.endsWith("/"))
sep = ""
let url = serverurl + sep + 'plantuml/svg/' + encoded
console.log("silverbullet-plantuml: requesting", url)
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text()
return data;
} catch (error) {
console.error("PUML generation failed", error)
return error
// We can ignore, this happens when there's no changes to commit
}
return "";
}
export async function widget(
bodyText: string,
) {
const userConfig = await readSetting("plantuml");
let result: string = bodyText
if ('serverurl' in userConfig) {
result = await pumlserver(userConfig.serverurl, bodyText)
} else if ('generator' in userConfig) {
result = await pumllocal(userConfig.generator, bodyText)
} else {
console.error("silverbullet-plantuml: neither serverurl nor generator configured")
}
userConfig.serverurl
return {
html: `<pre id="plantuml">${result}</pre>`,
script: `
document.addEventListener("click", () => {
api({type: "blur"});
});
`,
};
}