-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.ts
217 lines (193 loc) · 8.83 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {Plugin, parseYaml, MarkdownRenderer, Component, MarkdownPostProcessorContext, setIcon} from "obsidian";
interface MailBlockParameters {
to: [string] | string | undefined;
cc: [string] | string | undefined;
bcc: [string] | string | undefined;
subject: string | undefined;
body: string | undefined;
showmailto: boolean | undefined;
variables: { [name: string]: string | undefined }
from: string | undefined;
}
export default class MailBlockPlugin extends Plugin {
async onload() {
console.log("email block loading...");
this.registerMarkdownCodeBlockProcessor("email", async (src, el, ctx) => {
// Get Parameters
let parameters: MailBlockParameters | null = null;
try {
parameters = this.readParameters(src, ctx);
} catch (e) {
el.createEl("h3", {text: "Email parameters invalid: \n" + e.message});
return;
}
// console.log("Render the Email " + parameters);
try {
const rootEl = el.createEl("div", {cls: "email-block email-block-border"});
let fromContent = undefined;
if (parameters.from !== undefined) {
rootEl.createEl("div", {cls: "email-block-info", text: "From:"});
fromContent = this.replaceVariables(this.renderAddress(parameters.from), parameters.variables)
rootEl.createEl("div", {cls: "email-block-info-value", text: fromContent});
}
let toContent = undefined;
if (parameters.to !== undefined) {
rootEl.createEl("div", {cls: "email-block-info", text: "To:"});
toContent = this.replaceVariables(this.renderAddress(parameters.to), parameters.variables)
rootEl.createEl("div", {cls: "email-block-info-value", text: toContent});
}
let ccContent = undefined;
if (parameters.cc !== undefined) {
rootEl.createEl("div", {cls: "email-block-info", text: "Cc:"});
ccContent = this.replaceVariables(this.renderAddress(parameters.cc), parameters.variables)
rootEl.createEl("div", {cls: "email-block-info-value", text: ccContent});
}
let bccContent = undefined;
if (parameters.bcc !== undefined) {
rootEl.createEl("div", {cls: "email-block-info", text: "Bcc:"});
bccContent = this.replaceVariables(this.renderAddress(parameters.bcc), parameters.variables)
rootEl.createEl("div", {cls: "email-block-info-value", text: bccContent});
}
rootEl.createEl("div", {cls: "email-block-info", text: "Subject:"});
const subjectContent = this.replaceVariables(parameters.subject, parameters.variables);
rootEl.createEl("div", {cls: "email-block-info-value", text: subjectContent});
const bodyContent = rootEl.createEl("div", {cls: "email-block-body"});
await this.renderBody(bodyContent, parameters.body, parameters.variables, ctx);
if (parameters.showmailto) {
const data = "mailto:" + this.encodeToHtml(toContent) +
"?subject=" + this.encodeToHtml(subjectContent) +
(ccContent !== undefined ? "&cc=" + this.encodeToHtml(ccContent) : "") +
(bccContent !== undefined ? "&bcc=" + this.encodeToHtml(bccContent) : "") +
(bodyContent.innerText.length !== 0 ? "&body=" + this.encodeToHtml(bodyContent.innerText) : "");
const mailToButton = rootEl
.createEl("div", {cls: "email-block-mailto"})
.createEl("a", {href: data, text: "Mailto"});
setIcon(mailToButton, "mail");
}
} catch (error) {
el.createEl("h3", {text: error});
}
});
}
private readParameters(yamlString: string, ctx: MarkdownPostProcessorContext) {
if (yamlString.contains("[[") && !yamlString.contains('"[[')) {
yamlString = yamlString.replace("[[", '"[[');
yamlString = yamlString.replace("]]", ']]"');
}
let extraBody = "";
if (yamlString.contains("---")) {
let data = yamlString.split("---");
yamlString = data[0];
extraBody = data[1];
}
//Necessary to do not parse {{value}} into on object by parseYaml
if (yamlString.contains("{{") && !yamlString.contains('"{{')) {
yamlString = yamlString.replace("{{", '"{{');
yamlString = yamlString.replace("}}", '}}"');
}
const parameters: MailBlockParameters = parseYaml(yamlString);
parameters.to = this.fixAddress(parameters.to);
parameters.cc = this.fixAddress(parameters.cc);
parameters.bcc = this.fixAddress(parameters.bcc);
if (parameters.subject == undefined) {
parameters.subject = "";
} else {
//remove double quotes (it has been utilized after parsing)
parameters.subject = parameters.subject.replace('"{{', "{{");
parameters.subject = parameters.subject.replace('}}"', "}}");
}
if (parameters.showmailto == undefined) {
parameters.showmailto = true;
}
if (parameters.body === undefined) {
parameters.body = extraBody;
}
// Variables
if (parameters.variables === undefined) {
parameters.variables = {};
}
const sourceFile = this.app.metadataCache.getFirstLinkpathDest(
ctx.sourcePath,
"",
);
if (sourceFile != null) {
const sourceCache = this.app.metadataCache.getFileCache(sourceFile);
if (sourceCache != null) {
if (sourceCache.frontmatter != undefined) {
for (const [key, value] of Object.entries(sourceCache.frontmatter)) {
if (value && typeof value.toString === 'function') {
parameters.variables[key] = value.toString();
}
}
}
}
}
return parameters;
}
private fixAddress(address: [string] | string | undefined) {
if (address === undefined) {
return undefined;
}
let fixedAddress: string = "";
if (Array.isArray(address)) {
fixedAddress = address.join(",");
} else {
fixedAddress = address;
}
fixedAddress = fixedAddress.replace(/\s/g, "").replace(";", ",");
return fixedAddress;
}
private renderAddress(address: [string] | string) {
if (Array.isArray(address)) {
return address.join(", ")
}
return address.split(",").join(", ");
}
private async renderBody(bodyContentEl: HTMLElement, bodyContent: string | undefined, variables: {
[name: string]: string | undefined
}, ctx: MarkdownPostProcessorContext) {
if (bodyContent === undefined) {
return;
}
// render a markdown file
if (bodyContent.startsWith("[[")) {
bodyContent = bodyContent.substring(2, bodyContent.length - 2);
const mdFile = this.app.metadataCache.getFirstLinkpathDest(
bodyContent,
ctx.sourcePath
);
if (mdFile != null) {
let mdContent = await this.app.vault.read(mdFile);
mdContent = this.replaceVariables(mdContent, variables);
await MarkdownRenderer.renderMarkdown(mdContent, bodyContentEl, mdFile.path, new Component());
}
} else { // Render line by line as plain text
bodyContent = this.replaceVariables(bodyContent, variables);
let lines = bodyContent.split("\n");
lines.forEach(line => {
bodyContentEl.createEl("div", {cls: "email-block-body-line", text: line});
})
}
}
private replaceVariables(content: string | undefined, variables: { [name: string]: string | undefined }): string {
if (content === undefined) {
return "";
}
let resultingContent = content;
for (const [variable, value] of Object.entries(variables)) {
if (value != undefined) {
resultingContent = resultingContent?.replace("{{" + variable + "}}", value);
}
}
return resultingContent;
}
private encodeToHtml(rawStr: string | undefined) {
if (rawStr === undefined) {
return "";
}
return encodeURIComponent(rawStr);
}
onunload() {
console.log("Unloading email plugin...");
}
}