-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
483 lines (419 loc) · 15.2 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import { App, FileSystemAdapter, MarkdownPostProcessorContext, Plugin, PluginSettingTab, SectionCache, Setting, TFile, TFolder, MarkdownView, MarkdownPreviewRenderer } from 'obsidian';
import { Md5 } from 'ts-md5';
import * as fs from 'fs';
import * as temp from 'temp';
import * as path from 'path';
import {PdfTeXEngine} from './PdfTeXEngine.js';
import {PDFDocument} from 'pdf-lib';
const PdfToCairo = require("./pdftocairo.js")
import {optimize} from 'svgo';
interface SwiftlatexRenderSettings {
package_url: string,
timeout: number,
enableCache: boolean,
invertColorsInDarkMode: boolean;
cache: Array<[string, Set<string>]>;
packageCache: Array<StringMap>;
onlyRenderInReadingMode: boolean;
}
const DEFAULT_SETTINGS: SwiftlatexRenderSettings = {
package_url: `https://texlive2.swiftlatex.com/`,
timeout: 10000,
enableCache: true,
invertColorsInDarkMode: true,
cache: [],
packageCache: [{},{},{},{}],
onlyRenderInReadingMode: false,
}
type StringMap = { [key: string]: string };
const waitFor = async (condFunc: () => boolean) => {
return new Promise<void>((resolve) => {
if (condFunc()) {
resolve();
}
else {
setTimeout(async () => {
await waitFor(condFunc);
resolve();
}, 100);
}
});
};
export default class SwiftlatexRenderPlugin extends Plugin {
settings: SwiftlatexRenderSettings;
cacheFolderPath: string;
packageCacheFolderPath: string;
pluginFolderPath: string;
pdfEngine: any;
cache: Map<string, Set<string>>; // Key: md5 hash of latex source. Value: Set of file path names.
async onload() {
await this.loadSettings();
if (this.settings.enableCache) await this.loadCache();
this.pluginFolderPath = path.join(this.getVaultPath(), this.app.vault.configDir, "plugins/swiftlatex-render/");
this.addSettingTab(new SampleSettingTab(this.app, this));
// initialize the latex compiler
this.pdfEngine = new PdfTeXEngine();
await this.pdfEngine.loadEngine();
await this.loadPackageCache();
this.pdfEngine.setTexliveEndpoint(this.settings.package_url);
this.addSyntaxHighlighting();
if (this.settings.onlyRenderInReadingMode) {
const pdfBlockProcessor = MarkdownPreviewRenderer.createCodeBlockPostProcessor("latex", (source, el, ctx) => this.renderLatexToElement(source, el, ctx, false));
MarkdownPreviewRenderer.registerPostProcessor(pdfBlockProcessor);
const svgBlockProcessor = MarkdownPreviewRenderer.createCodeBlockPostProcessor("latexsvg", (source, el, ctx) => this.renderLatexToElement(source, el, ctx, true));
MarkdownPreviewRenderer.registerPostProcessor(svgBlockProcessor);
} else {
this.registerMarkdownCodeBlockProcessor("latex", (source, el, ctx) => this.renderLatexToElement(source, el, ctx, false));
this.registerMarkdownCodeBlockProcessor("latexsvg", (source, el, ctx) => this.renderLatexToElement(source, el, ctx, true));
}
}
onunload() {
if (this.settings.enableCache) this.unloadCache();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
getVaultPath() {
if (this.app.vault.adapter instanceof FileSystemAdapter) {
return this.app.vault.adapter.getBasePath();
} else {
throw new Error("SwiftLaTeX: Could not get vault path.");
}
}
async loadCache() {
const cacheFolderParentPath = path.join(this.getVaultPath(), this.app.vault.configDir, "swiftlatex-render-cache");
if (!fs.existsSync(cacheFolderParentPath)) {
fs.mkdirSync(cacheFolderParentPath);
}
this.cacheFolderPath = path.join(cacheFolderParentPath, "pdf-cache");
if (!fs.existsSync(this.cacheFolderPath)) {
fs.mkdirSync(this.cacheFolderPath);
this.cache = new Map();
} else {
this.cache = new Map(this.settings.cache);
// For some reason `this.cache` at this point is actually `Map<string, Array<string>>`
for (const [k, v] of this.cache) {
this.cache.set(k, new Set(v))
}
}
}
async loadPackageCache() {
const cacheFolderParentPath = path.join(this.getVaultPath(), this.app.vault.configDir, "swiftlatex-render-cache");
if (!fs.existsSync(cacheFolderParentPath)) {
fs.mkdirSync(cacheFolderParentPath);
}
this.packageCacheFolderPath = path.join(cacheFolderParentPath, "package-cache");
if (!fs.existsSync(this.packageCacheFolderPath)) {
fs.mkdirSync(this.packageCacheFolderPath);
}
console.log("SwiftLaTeX: Loading package cache");
// add files in the package cache folder to the cache list
const packageFiles = fs.readdirSync(this.packageCacheFolderPath);
for (const file of packageFiles) {
const filename = path.basename(file);
const value = "/tex/"+filename;
const packageValues = Object.values(this.settings.packageCache[1]);
if (!packageValues.includes(value)) {
const key = "26/" + filename
this.settings.packageCache[1][key] = value;
}
}
// move packages to the VFS
for (const [key, val] of Object.entries(this.settings.packageCache[1])) {
const filename = path.basename(val);
let read_success = false;
try {
const srccode = fs.readFileSync(path.join(this.packageCacheFolderPath, filename));
this.pdfEngine.writeTexFSFile(filename, srccode);
} catch (e) {
// when unable to read file, remove this from the cache
console.log(`Unable to read file ${filename} from package cache`)
delete this.settings.packageCache[1][key];
}
}
// write cache data to the VFS, except don't write the texlive404_cache because this will cause problems when switching between texlive sources
this.pdfEngine.writeCacheData({},
this.settings.packageCache[1],
this.settings.packageCache[2],
this.settings.packageCache[3]);
}
unloadCache() {
fs.rmdirSync(this.cacheFolderPath, { recursive: true });
}
addSyntaxHighlighting() {
// @ts-ignore
window.CodeMirror.modeInfo.push({name: "latexsvg", mime: "text/x-latex", mode: "stex"});
}
formatLatexSource(source: string) {
return source;
}
hashLatexSource(source: string) {
return Md5.hashStr(source.trim());
}
async pdfToHtml(pdfData: any) {
const {width, height} = await this.getPdfDimensions(pdfData);
const ratio = width / height;
const pdfblob = new Blob([pdfData], { type: 'application/pdf' });
const objectURL = URL.createObjectURL(pdfblob);
return {
attr: {
data: `${objectURL}#view=FitH&toolbar=0`,
type: 'application/pdf',
class: 'block-lanuage-latex',
style: `width:100%; aspect-ratio:${ratio}`
}
};
}
svgToHtml(svg: any) {
if (this.settings.invertColorsInDarkMode) {
svg = this.colorSVGinDarkMode(svg);
}
return svg;
}
async getPdfDimensions(pdf: any): Promise<{width: number, height: number}> {
const pdfDoc = await PDFDocument.load(pdf);
const firstPage = pdfDoc.getPages()[0];
const {width, height} = firstPage.getSize();
return {width, height};
}
pdfToSVG(pdfData: any) {
return PdfToCairo().then((pdftocairo: any) => {
pdftocairo.FS.writeFile('input.pdf', pdfData);
pdftocairo._convertPdfToSvg();
let svg = pdftocairo.FS.readFile('input.svg', {encoding:'utf8'});
// Generate a unique ID for each SVG to avoid conflicts
const id = Md5.hashStr(svg.trim()).toString();
const randomString = Math.random().toString(36).substring(2, 10);
const uniqueId = id.concat(randomString);
const svgoConfig = {
plugins: ['sortAttrs', { name: 'prefixIds', params: { prefix: uniqueId } }]
};
svg = optimize(svg, svgoConfig).data;
return svg;
});
}
colorSVGinDarkMode(svg: string) {
// Replace the color "black" with currentColor (the current text color)
// so that diagram axes, etc are visible in dark mode
// And replace "white" with the background color
svg = svg.replace(/rgb\(0%, 0%, 0%\)/g, "currentColor")
.replace(/rgb\(100%, 100%, 100%\)/g, "var(--background-primary)");
return svg;
}
async renderLatexToElement(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext, outputSVG: boolean = false) {
return new Promise<void>((resolve, reject) => {
let md5Hash = this.hashLatexSource(source);
let pdfPath = path.join(this.cacheFolderPath, `${md5Hash}.pdf`);
// PDF file has already been cached
// Could have a case where pdfCache has the key but the cached file has been deleted
if (this.settings.enableCache && this.cache.has(md5Hash) && fs.existsSync(pdfPath)) {
// console.log("Using cached PDF: ", md5Hash);
let pdfData = fs.readFileSync(pdfPath);
if (outputSVG) {
this.pdfToSVG(pdfData).then((svg: string) => { el.innerHTML = this.svgToHtml(svg);})
} else {
this.pdfToHtml(pdfData).then((htmlData)=>{el.createEl("object", htmlData); resolve();});
}
this.addFileToCache(md5Hash, ctx.sourcePath);
resolve();
}
else {
// console.log("Rendering PDF: ", md5Hash);
this.renderLatexToPDF(source, md5Hash).then((r: any) => {
if (this.settings.enableCache) this.addFileToCache(md5Hash, ctx.sourcePath);
if (outputSVG) {
this.pdfToSVG(r.pdf).then((svg: string) => { el.innerHTML = this.svgToHtml(svg);})
} else {
this.pdfToHtml(r.pdf).then((htmlData)=>{el.createEl("object", htmlData); resolve();});
}
fs.writeFileSync(pdfPath, r.pdf);
resolve();
}
).catch(err => {
let errorDiv = el.createEl('div', { text: `${err}`, attr: { class: 'block-latex-error' } });
reject(err);
});
}
}).then(() => {
this.pdfEngine.flushCache();
if (this.settings.enableCache) setTimeout(() => this.cleanUpCache(), 1000);
});
}
renderLatexToPDF(source: string, md5Hash: string) {
return new Promise(async (resolve, reject) => {
source = this.formatLatexSource(source);
temp.mkdir("obsidian-swiftlatex-renderer", async (err, dirPath) => {
try {
await waitFor(() => this.pdfEngine.isReady());
} catch (err) {
reject(err);
return;
}
if (err) reject(err);
this.pdfEngine.writeMemFSFile("main.tex", source);
this.pdfEngine.setEngineMainFile("main.tex");
this.pdfEngine.compileLaTeX().then((r: any) => {
if (r.status != 0) {
// manage latex errors
reject(r.log);
}
// update the list of package files in the cache
this.fetchPackageCacheData()
resolve(r);
});
})
});
}
fetchPackageCacheData(): void {
this.pdfEngine.fetchCacheData().then((r: StringMap[]) => {
for (var i = 0; i < r.length; i++) {
if (i === 1) { // currently only dealing with texlive200_cache
// get diffs
const newFileNames = this.getNewPackageFileNames(this.settings.packageCache[i], r[i]);
// fetch new package files
this.pdfEngine.fetchTexFiles(newFileNames, this.packageCacheFolderPath);
}
}
this.settings.packageCache = r;
this.saveSettings().then(); // hmm
});
}
getNewPackageFileNames(oldCacheData: StringMap, newCacheData: StringMap): string[] {
// based on the old and new package files in package cache data,
// return the new package files
let newKeys = Object.keys(newCacheData).filter(key => !(key in oldCacheData));
let newPackageFiles = newKeys.map(key => path.basename(newCacheData[key]));
return newPackageFiles;
}
async saveCache() {
let temp = new Map();
for (const [k, v] of this.cache) {
temp.set(k, [...v])
}
this.settings.cache = [...temp];
await this.saveSettings();
}
addFileToCache(hash: string, file_path: string) {
if (!this.cache.has(hash)) {
this.cache.set(hash, new Set());
}
this.cache.get(hash)?.add(file_path);
}
async cleanUpCache() {
let file_paths = new Set<string>();
for (const fps of this.cache.values()) {
for (const fp of fps) {
file_paths.add(fp);
}
}
for (const file_path of file_paths) {
let file = this.app.vault.getAbstractFileByPath(file_path);
if (file == null) {
this.removeFileFromCache(file_path);
} else {
if (file instanceof TFile) {
await this.removeUnusedCachesForFile(file);
}
}
}
await this.saveCache();
}
async removeUnusedCachesForFile(file: TFile) {
let hashes_in_file = await this.getLatexHashesFromFile(file);
let hashes_in_cache = this.getLatexHashesFromCacheForFile(file);
for (const hash of hashes_in_cache) {
if (!hashes_in_file.contains(hash)) {
this.cache.get(hash)?.delete(file.path);
if (this.cache.get(hash)?.size == 0) {
this.removePDFFromCache(hash);
}
}
}
}
removePDFFromCache(key: string) {
this.cache.delete(key);
fs.rmSync(path.join(this.cacheFolderPath, `${key}.pdf`));
}
removeFileFromCache(file_path: string) {
for (const hash of this.cache.keys()) {
this.cache.get(hash)?.delete(file_path);
if (this.cache.get(hash)?.size == 0) {
this.removePDFFromCache(hash);
}
}
}
getLatexHashesFromCacheForFile(file: TFile) {
let hashes: string[] = [];
let path = file.path;
for (const [k, v] of this.cache.entries()) {
if (v.has(path)) {
hashes.push(k);
}
}
return hashes;
}
async getLatexHashesFromFile(file: TFile) {
let hashes: string[] = [];
let sections = this.app.metadataCache.getFileCache(file)?.sections
if (sections != undefined) {
let lines = (await this.app.vault.read(file)).split('\n');
for (const section of sections) {
if (section.type != "code" && lines[section.position.start.line].match("``` *latex") == null) continue;
let source = lines.slice(section.position.start.line + 1, section.position.end.line).join("\n");
let hash = this.hashLatexSource(source);
hashes.push(hash);
}
}
return hashes;
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: SwiftlatexRenderPlugin;
constructor(app: App, plugin: SwiftlatexRenderPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Package Fetching URL')
.setDesc('default: https://texlive2.swiftlatex.com/, reload required to take effect')
.addText(text => text
.setValue(this.plugin.settings.package_url.toString())
.onChange(async (value) => {
this.plugin.settings.package_url = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Enable caching of PDFs')
.setDesc("PDFs rendered by this plugin will be kept in {config directory}/swiftlatex-render-cache/pdf-cache, where the config directory is .obsidian by default. The plugin will automatically keep track of used pdfs and remove any that aren't being used")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableCache)
.onChange(async (value) => {
this.plugin.settings.enableCache = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Invert dark colors in dark mode')
.setDesc('Invert dark colors in diagrams (e.g. axes, arrows) when in dark mode, so that they are visible.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.invertColorsInDarkMode)
.onChange(async (value) => {
this.plugin.settings.invertColorsInDarkMode = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Only render in Reading mode')
.setDesc('Codeblocks are rendered into LaTeX only in Reading mode, not in Preview mode, requires reload to take effect.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.onlyRenderInReadingMode)
.onChange(async (value) => {
this.plugin.settings.onlyRenderInReadingMode = value;
await this.plugin.saveSettings();
}));
}
}