-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
184 lines (166 loc) · 5.63 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
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
let vscode = require("vscode");
const editTemplate = require("./commands/edittemplate");
const newTemplate = require("./commands/newtemplate");
const newFileFromTemplate = require("./commands/newfilefromtemplate");
const kt = require("katex");
const tm = require("markdown-it-texmath").use(kt);
const path = require("path");
const fs = require("fs");
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
/**
* Create a new Template from file
*/
let disposable = vscode.commands.registerCommand(
"extension.newTemplateFromFile",
newTemplate.newTemplateFromFile
);
context.subscriptions.push(disposable);
/**
* Create a new Template
*/
disposable = vscode.commands.registerCommand(
"extension.newTemplate",
newTemplate.newTemplate
);
/**
* Edit a template
*/
disposable = vscode.commands.registerCommand(
"extension.editTemplate",
editTemplate
);
context.subscriptions.push(disposable);
/**
* Create a new file from template
*/
disposable = vscode.commands.registerCommand(
"extension.newFileFromTemplate",
newFileFromTemplate
);
context.subscriptions.push(disposable);
(cfg = key => vscode.workspace.getConfiguration("mdmath")[key]),
(infoMsg = msg => {
vscode.window.showInformationMessage(`Markdown + Math: ${msg}`);
}),
(errMsg = msg => {
vscode.window.showErrorMessage(`Markdown + Math: ${msg}`);
}),
(asHTML = () => {
const doc =
vscode.window.activeTextEditor &&
vscode.window.activeTextEditor.document,
usrcss = cfg("style");
if (!doc || doc.languageId !== "markdown")
return infoMsg("Active document is no markdown source document!");
if (!mdit)
return infoMsg(
"Corresponding markdown preview document needs to be opened at least once!"
);
return htmlTmpl(
mdit.render(doc.getText()),
usrcss.length ? usrcss : false
);
}),
(clip = () => {
vscode.env.clipboard.writeText(asHTML()).then(
() => infoMsg("Html copied to clipboard!"),
err => errMsg("Html copying to clipboard failed: " + err.message)
);
}),
(save = uri => {
try {
const fs = require("fs");
uri = uri || vscode.window.activeTextEditor.document.uri;
fs.writeFileSync(outputLocationOf(uri.fsPath), asHTML(), "utf8");
infoMsg(`Html saved to ${outputLocationOf(uri.fsPath)} !`);
} catch (err) {
errMsg("Saving html failed: " + err.message);
}
}),
(outputLocationOf = fsPath => {
const root = vscode.workspace.getWorkspaceFolder(fsPath),
parsed = path.parse(fsPath),
savePath = cfg("savePath")
.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 macroDef = JSON.stringify(cfg("macros")),
macroUrl = cfg("macroFile"),
macros = macroUrl.length
? fs.readFileSync(path.resolve(macroUrl), "utf8")
: macroDef;
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 ...
(options =
Object.keys(macros).length !== 0
? { delimiters, macros }
: { delimiters });
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) {
return (mdit = md).use(tm, options);
}
};
}
exports.activate = activate;
function deactivated() {}
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/2.10.0/github-markdown.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-9eLZqc9ds8eNjO3TmqPeYcDj8n+Qfa4nuSiGYa6DjLNcv9BtN69ZIulL9+8CqC9Y" crossorigin="anonymous">
<link rel="stylesheet" href="https://gitcdn.xyz/repo/goessner/mdmath/master/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>
${html}
</body></html>`.replace("vscode-resource:", "");
module.exports = {
activate,
deactivated
};