-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
64 lines (56 loc) · 2.46 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
'use strict';
const { mathToSvg } = require('@justforfun-click/mathjax/js/mathToSvg');
const vscode = require('vscode');
const asciiMathTemplateSnippet = new vscode.SnippetString('<img src="https://math.azureedge.net/$/$0"/>');
const latexMathTemplateSnipped = new vscode.SnippetString('<img src="https://math.azureedge.net/$$/$0"/>');
var mathTemplateSnippet = asciiMathTemplateSnippet;
const imgRex = /<img .*?\bsrc\s*=\s*"https:\/\/math\.justforfun\.click\/(\$\$?\/[^"]*)"[^>]*>/s;
const imgStyleRex = /\bstyle\s*=\s*"([^"]*)"/s;
function render(md, options) {
md.renderer.rules.html_block = md.renderer.rules.html_inline = (tokens, idx, options, env, self) => {
var token = tokens[idx];
var content = token.content;
while (true) {
var imgMatches = content.match(imgRex);
if (!imgMatches) {
break;
}
var imgContent = imgMatches[0];
var svgContent = mathToSvg(decodeURI(imgMatches[1])) || "";
var imgStyleMatches = imgContent.match(imgStyleRex);
if (imgStyleMatches) {
var styleContent = imgStyleMatches[1];
if (styleContent.indexOf("width:") >= 0 || styleContent.indexOf("height:") >= 0) {
svgContent = svgContent.replace(/width="[^"]*"/, "");
svgContent = svgContent.replace(/height="[^"]*"/, "");
}
var styleStr = ' style="';
var stylePos = svgContent.indexOf(styleStr);
stylePos = svgContent.indexOf('"', stylePos + styleStr.length);
svgContent = svgContent.substring(0, stylePos) + ";" + styleContent + svgContent.substring(stylePos);
}
content = content.replace(imgContent, svgContent);
}
return content;
};
}
exports.activate = function activate(context) {
vscode.commands.registerCommand('math-to-svg.insert-math', () => {
var editor = vscode.window.activeTextEditor;
switch (vscode.workspace.getConfiguration('math-to-svg')['notation'].toLowerCase()) {
default:
case "asciimath":
mathTemplateSnippet = asciiMathTemplateSnippet;
break;
case "latex":
mathTemplateSnippet = latexMathTemplateSnipped;
break;
}
editor.insertSnippet(mathTemplateSnippet);
});
return {
extendMarkdownIt: function (md) {
return md.use(render);
}
};
};