-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
141 lines (121 loc) · 3.22 KB
/
main.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
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
import { Plugin } from "obsidian";
const PREFIX = "swarnam";
function base64ToBytes(base64: string) {
const binString = atob(base64);
// @ts-expect-error
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}
function bytesToBase64(bytes: Uint8Array) {
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte)
).join("");
return btoa(binString);
}
function asBase64(text: string) {
const b64 = bytesToBase64(new TextEncoder().encode(text));
return b64;
}
function getIframeDoc(htmlSource: string, cssSource: string, jsSource: string) {
const isDarkMode = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
return `
<style>
body { font-family: sans-serif; color: ${isDarkMode ? "#fff" : "#000"} }
</style>
<style>
${cssSource}
</style>
<div class="${PREFIX}-html-container">
${htmlSource}
</div>
<script>
${jsSource}
</script>
`;
}
function showError(msg: string, root: HTMLElement) {
root.classList.add("error");
root.createEl("p", { cls: "icon", text: "⚠️" });
root.createEl("p", { text: msg });
}
export default class SwarnamPlugin extends Plugin {
async onload() {
this.registerMarkdownCodeBlockProcessor(
"swarnam",
(source, el, ctx) => {
const root = el.createDiv({ cls: `${PREFIX}-root` });
let [
htmlSource = "",
cssSource = "",
jsSource = "",
// eslint-disable-next-line prefer-const
...others
] = source.split(/^\s*---\*---\s*$/m);
if (others.length > 0) {
showError(
"Swarnam only supports HTML, CSS and JS blocks but your snippet has more than 3 blocks.",
root
);
return;
}
htmlSource = htmlSource.trim();
cssSource = cssSource.trim();
jsSource = jsSource.trim();
if (!htmlSource) {
showError(
"A Swarnam block must at least contain HTML",
root
);
return;
}
const sourceRoot = root.createDiv({
cls: `${PREFIX}-source-root`,
});
const htmlContainer = sourceRoot.createDiv({
cls: `${PREFIX}-source-container`,
});
const htmlEl = htmlContainer.createEl("pre", {
cls: `${PREFIX}-source ${PREFIX}-html-source`,
});
htmlContainer.createDiv({
text: "HTML",
cls: `${PREFIX}-badge ${PREFIX}-html-badge`,
});
htmlEl.setText(htmlSource);
if (cssSource) {
const cssContainer = sourceRoot.createDiv({
cls: `${PREFIX}-source-container`,
});
const cssEl = cssContainer.createEl("pre", {
cls: `${PREFIX}-source ${PREFIX}-css-source`,
});
cssContainer.createDiv({
text: "CSS",
cls: `${PREFIX}-badge ${PREFIX}-css-badge`,
});
cssEl.setText(cssSource);
}
if (jsSource) {
const jsContainer = sourceRoot.createDiv({
cls: `${PREFIX}-source-container`,
});
const jsEl = jsContainer.createEl("pre", {
cls: `${PREFIX}-source ${PREFIX}-js-source`,
});
jsContainer.createDiv({
text: "JS",
cls: `${PREFIX}-badge ${PREFIX}-js-badge`,
});
jsEl.setText(jsSource);
}
const iframeEl = root.createEl("iframe", {
cls: `${PREFIX}-preview`,
});
iframeEl.src = `data:text/html;base64;charset=UTF-8,${asBase64(
getIframeDoc(htmlSource, cssSource, jsSource)
)}`;
}
);
}
}