-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
286 lines (249 loc) · 8.63 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { Editor, Plugin } from "obsidian";
import MarkdownIt from "markdown-it";
import hljs from "highlight.js";
interface Question {
title: string;
answer: string | null;
}
interface Card {
content: string;
question: Question[];
}
const hashCode = (s: string) =>
s.split("").reduce((a, b) => {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);
const EXPORT_FILENAME_EXTENSION = "txt";
const EXPORT_FILENAME_PREFIX = "export-to-anki-";
const CONTENT_KEYWORD = "##### content";
const QUESTIONS_KEYWORD = "##### questions";
const QUESTIONS_PREFIX = "###### ";
const DEFAULT_QUESTION_CONTENT = "Question?";
const DEFAULT_QUESTION = `${QUESTIONS_PREFIX}${DEFAULT_QUESTION_CONTENT}`;
const TEMPLATE = `${CONTENT_KEYWORD}
${QUESTIONS_KEYWORD}
${DEFAULT_QUESTION}`;
const markdown: MarkdownIt = MarkdownIt({
html: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return (
'<pre><code class="hljs">' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
"</code></pre>"
);
} catch (__) {
//
}
}
return (
'<pre><code class="hljs">' +
markdown.utils.escapeHtml(str) +
"</code></pre>"
);
},
});
export default class Ankifast extends Plugin {
async onload() {
this.addCommand({
id: "ankifast-add-template",
name: "Add template",
hotkeys: [{ modifiers: ["Mod", "Shift"], key: "d" }],
editorCallback: (editor: Editor) => {
editor.replaceSelection(TEMPLATE);
editor.focus();
editor.setCursor({
line: editor.getCursor().line - 3,
ch: 0,
});
},
});
this.addCommand({
id: "ankifast-to-anki",
name: "Export to Anki",
editorCallback: (editor: Editor) => {
let isSelection = false;
const cards: Card[] = [];
let cardsContent = "";
if (editor.somethingSelected()) {
cardsContent = editor.getSelection();
isSelection = true;
} else cardsContent = editor.getValue();
const countContentRegExp = new RegExp(
`${CONTENT_KEYWORD.replace("#", "[#]")}`,
"g"
);
const countQuestionsRegExp = new RegExp(
`${QUESTIONS_KEYWORD.replace("#", "[#]")}`,
"g"
);
const contentHeaderCount = (
cardsContent.match(countContentRegExp) || []
).length;
const questionsHeaderCount = (
cardsContent.match(countQuestionsRegExp) || []
).length;
if (contentHeaderCount != questionsHeaderCount) {
alert(
`Error: content(${contentHeaderCount}) and questions(${questionsHeaderCount}) headers mismatch`
);
return;
}
while (cardsContent.indexOf(CONTENT_KEYWORD) != -1) {
const card: Card = {
content: "",
question: [],
};
let index: number | undefined = cardsContent.indexOf(
CONTENT_KEYWORD
);
cardsContent = cardsContent.substring(
index + CONTENT_KEYWORD.length
);
index = cardsContent.indexOf(CONTENT_KEYWORD);
if (index != -1) {
index = index + CONTENT_KEYWORD.length;
} else index = undefined;
let cardBody = cardsContent.substring(0, index);
index = cardBody.indexOf(QUESTIONS_KEYWORD);
card.content = cardBody.substring(0, index).trim();
index = cardBody.indexOf(QUESTIONS_KEYWORD);
cardBody = cardBody.substring(index + QUESTIONS_KEYWORD.length);
while (cardBody.indexOf(QUESTIONS_PREFIX) != -1) {
const question: Question = {
title: "",
answer: null,
};
index = cardBody.indexOf(QUESTIONS_PREFIX);
cardBody = cardBody.substring(
index + QUESTIONS_PREFIX.length
);
question.title = cardBody.split(/\r?\n|\r|\n/g)[0];
if (question.title == DEFAULT_QUESTION_CONTENT) {
continue;
}
index = cardBody.indexOf(question.title);
cardBody = cardBody.substring(index + question.title.length);
index = cardBody.indexOf(QUESTIONS_PREFIX);
if (index == -1) {
index = cardBody.indexOf(CONTENT_KEYWORD);
}
if (index == -1) {
index = 0;
}
question.answer = cardBody.substring(0, index).trim();
if (question.answer.length == 0) question.answer = null;
card.question.push(question);
}
cards.push(card);
}
(async () => {
const renderToHtml = async (content: string) => {
return (await markdown.render(content)).replace(/\n/g, "");
};
const escapeLatexSpace = (latex: string) => {
const spaces = ["\\\\;", "\\\\:", "\\\\,", "\\\\!"];
for (let index = 0; index < spaces.length; index++) {
const element = spaces[index];
latex = latex.replace(new RegExp(element, 'g'), "\\" + element);
}
return latex;
};
const replaceLatex = (content: string) => {
const latexBlockRegExp = /\$\$(.*?)\$\$/g;
const latexInlineRegExp = /\$.*?\$/g;
if (latexBlockRegExp.test(content)) {
const matches = content.match(latexBlockRegExp);
if (matches) {
for (let index = 0; index < matches.length; index++) {
const match = matches[index];
const latexContent = match.split("$$")[1];
content = content.replace(
match,
"[latex]\\begin{displaymath}" +
escapeLatexSpace(latexContent) +
"\\end{displaymath}[/latex]"
);
}
}
}
if (latexInlineRegExp.test(content)) {
const matches = content.match(latexInlineRegExp);
if (matches) {
for (let index = 0; index < matches.length; index++) {
const match = matches[index];
const latexContent = match.split("$")[1];
content = content.replace(
match,
"[latex]\\begin{math}" +
escapeLatexSpace(latexContent) +
"\\end{math}[/latex]"
);
}
}
}
return content;
};
let exportedContent = "";
for (let index = 0; index < cards.length; index++) {
const card = cards[index];
for (
let indexQuestion = 0;
indexQuestion < card.question.length;
indexQuestion++
) {
const question = card.question[indexQuestion];
let content = "";
let title = "";
if (question.answer != null) {
content = question.answer;
} else content = card.content;
content = replaceLatex(content);
content = await renderToHtml(content);
title = replaceLatex(question.title);
title = await renderToHtml(title);
exportedContent = exportedContent + title + `\t${content}\n`;
}
}
const root = this.app.vault.getRoot();
const currentFile = this.app.workspace.getActiveFile();
if (currentFile == null) {
alert(`Error: active file not found.`);
return;
}
let filename = "";
if (!isSelection) {
filename =
EXPORT_FILENAME_PREFIX +
currentFile.name +
`.${EXPORT_FILENAME_EXTENSION}`;
} else {
filename =
EXPORT_FILENAME_PREFIX +
hashCode(cardsContent) +
"-" +
currentFile.name +
`.${EXPORT_FILENAME_EXTENSION}`;
}
const filePath = root.path + filename;
const file = this.app.vault
.getFiles()
.find((el) => el.name == filename);
if (file != undefined) {
await this.app.vault.modify(file, exportedContent);
} else {
await this.app.vault.create(filePath, exportedContent);
}
alert(
`Exported ${
isSelection ? "selection" : "content"
} to ${filename} file.`
);
})();
},
});
}
onunload() {}
}