-
Notifications
You must be signed in to change notification settings - Fork 57
/
extension.js
120 lines (108 loc) · 5.83 KB
/
extension.js
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Stefan Goessner - 2016-20. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
const vscode = require('vscode');
// this method is called when extension is activated ..
exports.activate = function activate(context) {
let mdit = null; // markdown-it object ...
const path = require('path'),
fs = require('fs'),
cfg = (key) => vscode.workspace.getConfiguration('mdmath')[key],
infoMsg = (msg) => {
vscode.window.showInformationMessage(`Markdown+Math: ${msg}`);
},
errMsg = (msg) => {
vscode.window.showErrorMessage(`Markdown+Math: ${msg}`);
},
asHTML = (doc) => {
const usrcss = cfg('style');
return htmlTmpl(mdit.render(doc.getText()), usrcss.length ? usrcss : false);
},
clip = () => {
const doc = vscode.window.activeTextEditor
&& vscode.window.activeTextEditor.document;
vscode.env.clipboard.writeText(asHTML(doc))
.then(()=>infoMsg('Html copied to clipboard!'),
(err)=>errMsg('Html copying to clipboard failed: ' + err.message));
},
save = (arg) => {
try {
const doc = arg && arg.uri ? arg : vscode.window.activeTextEditor && vscode.window.activeTextEditor.document;
const uri = vscode.window.activeTextEditor.document.uri;
if (!doc)
errMsg('Saving html failed: invalid editor document!');
else if (doc.languageId !== 'markdown')
errMsg('Saving html failed: Active document is no markdown source document!');
else if (doc.isUntitled)
errMsg('Saving html failed: current untitled markdown document needs to be saved once first!');
else {
fs.writeFileSync(outputLocationOf(uri), asHTML(doc), 'utf8');
infoMsg(`Html saved to ${outputLocationOf(uri)} !`);
}
} catch (err) {
errMsg('Saving html failed: ' + err.message);
}
},
outputLocationOf = (uri) => {
const root = vscode.workspace.getWorkspaceFolder(uri),
parsed = path.parse(uri.fsPath),
savePath = cfg('savePath') // use https://code.visualstudio.com/updates/v1_31#_global-storage-path instead in future !
.replace('${file.name}', parsed.name)
.replace('${file.ext}', parsed.ext),
out = savePath.startsWith('/') ? path.resolve(root, `.${savePath}`) : path.resolve(parsed.dir, savePath);
return path.isAbsolute(out) ? out : path.resolve(parsed.dir, `./${parsed.name}.html`);
},
loadMacros = () => {
try {
const macroUrl = cfg('macroFile'),
macros = macroUrl.length ? fs.readFileSync(path.resolve(macroUrl),'utf8')
: JSON.stringify(cfg('macros'))
return JSON.parse(macros);
} catch (err) {
errMsg('Loading macros failed: ' + err.message);
}
},
delimiters = JSON.parse(JSON.stringify(cfg('delimiters'))) || 'dollars', // wondering why this ...
macros = loadMacros(); // ... JSON stuff is necessary ...
context.subscriptions.push(vscode.commands.registerCommand('extension.clipToHtml', clip));
context.subscriptions.push(vscode.commands.registerCommand('extension.saveToHtml', save));
if (cfg('autosave')) { // Initialize autosave functionality ... if user defined !
let watching = false;
const initWatching = () => {
if (!watching && vscode.window.activeTextEditor) {
const pattern = new vscode.RelativePattern(path.dirname(vscode.window.activeTextEditor.document.uri.fsPath), '**/*.md'),
watcher = vscode.workspace.createFileSystemWatcher(pattern,false,false,true);
watcher.onDidChange(save);
watcher.onDidCreate(save);
watching = true;
}
};
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(initWatching));
}
return {
extendMarkdownIt: function(md) {
const options = { engine: require('katex'),
delimiters };
if (macros) options.macros = macros;
return (mdit = md).use(require('markdown-it-texmath'), options);
}
}
}
// this method is called when extension is deactivated ..
exports.deactivate = function deactivate() {};
const htmlTmpl = (html,usrcss) => `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/3.0.1/github-markdown.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/texmath.css">
<link rel="stylesheet" href="https://gitcdn.xyz/repo/goessner/mdmath/master/css/vscode-texmath.css">
${usrcss ? `<link rel="stylesheet" href="${usrcss}">` : ''}
</head>
<body class="markdown-body">
${html}
</body></html>`.replace(/<img src="vscode-resource:/g,'<img src="');