-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkedMathJaxWiki.js
140 lines (140 loc) · 5.02 KB
/
MarkedMathJaxWiki.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
"use strict";
/**
* MarkedMathJaxWiki
*
* It combines Marked, MathJax and highlight.js into an engine that renders
* markdown files with latex-embedded and does Wiki-like references
* replacement.
*
* Wiki references are nested in double brackets like:
* The [[reference]] can be...
* The reference can be displayed with diferent text like
* The [[reference | alternative text]] can be...
*
* To type Latex there are two ways: inline and display mode. To type
* inline use \\( to begin math mode and \\) to end math mode:
* When \\(x \gt 3\\) the value...
* To type in display mode use $$ to start and end math mode:
* $$a^2=b^2+c^2$$
*
* The code hihglighted start with (javascript example)
* ```javascript
* function plus(a,b) {
* return (a+b);
* }
* ```
*
* depends: parseURI.ts
*/
var MarkedMathJaxWikiContainer;
var MarkedMathJaxWikiPrefix = "";
var MarkedMathJaxWikiPathname = "";
var MarkedMathJaxWikiDefault = "";
var MarkedMathJaxWikiURL = "";
var MarkedMathJaxWikiAddLabel = false;
var MarkedMathJaxWikiOnLoad = [];
//const WikiReference = /(\[\[)([^\[\]]+)(\]\])/gm;
const WikiReference = /(\[\[)(([^\[\]]|\[(?!\[)|\](?!\]))+)(\]\])/gm;
function replaceWikiReference(mdstr) {
let result = "";
let last_match = 0;
let match;
let match_ref;
let ref, disp;
let cmd = "";
do {
match = WikiReference.exec(mdstr);
if (match) {
result += mdstr.substring(last_match, match.index);
last_match = match.index + match['0'].length;
match_ref = match['2'].split("|");
ref = match_ref[0].trim();
let colon = ref.indexOf(":");
if (colon >= 0) {
cmd = ref.substring(0, colon);
}
if (match_ref.length == 1) {
disp = match_ref[0].trim();
}
else if (match_ref.length == 2) {
disp = match_ref[1].trim();
}
else {
throw new Error("replaceWikiReference: invalid reference: " + match['0']);
}
let src;
switch (cmd) {
case "":
case "md":
if (ref.indexOf("://") == -1) {
ref = MarkedMathJaxWikiPathname +
"?url=" +
encodeURIComponent(MarkedMathJaxWikiPrefix +
ref.replace(/ /g, "_")) +
".md";
}
result += `[${disp}](${ref})`;
break;
case "js":
src = eval(ref);
result += "```javascript\n";
if (typeof src == 'function') {
result += src;
}
else {
result += ref + " = " + src;
}
result += "\n```\n";
break;
case "javascript":
MarkedMathJaxWikiOnLoad.push(ref);
result += "\n";
break;
}
}
} while (match);
result += mdstr.substring(last_match);
return result;
}
function MarkedMathJaxWikiListener() {
let md_prefix;
if (MarkedMathJaxWikiAddLabel) {
md_prefix = decodeURIComponent(MarkedMathJaxWikiURL.substring(MarkedMathJaxWikiURL.lastIndexOf("/") + 1, MarkedMathJaxWikiURL.lastIndexOf("."))).replace(/_/g, " ");
md_prefix = (md_prefix != "README") ? ("# " + md_prefix + "\n\n") : "";
}
else {
md_prefix = "";
}
MarkedMathJaxWikiContainer.innerHTML = marked.parse(md_prefix + replaceWikiReference(this.responseText));
document.title = document.getElementsByTagName('h1')[0].innerHTML;
for (let i = 0; i < MarkedMathJaxWikiOnLoad.length; i++) {
eval(MarkedMathJaxWikiOnLoad[i]);
}
MathJax.typeset();
hljs.highlightAll();
}
;
function MarkedMathJaxWiki(element, default_md, addlabel) {
MarkedMathJaxWikiContainer = (typeof element == 'string') ? document.getElementById(element) : element;
MarkedMathJaxWikiDefault = (default_md) ? default_md : "";
MarkedMathJaxWikiAddLabel = (addlabel) ? addlabel : false;
if (MarkedMathJaxWikiContainer) {
let parameters = parseURI();
MarkedMathJaxWikiPathname = parameters['$pathname'];
if ('url' in parameters) {
MarkedMathJaxWikiPrefix = parameters.url.substring(0, parameters.url.lastIndexOf("/") + 1);
MarkedMathJaxWikiURL = parameters.url;
}
else if (default_md) {
MarkedMathJaxWikiPrefix = default_md.substring(0, default_md.lastIndexOf("/") + 1);
MarkedMathJaxWikiURL = default_md;
}
else {
throw new URIError("MarkedMathJaxWiki: file parameter not found in URI or function call.");
}
let oReq = new XMLHttpRequest();
oReq.onload = MarkedMathJaxWikiListener;
oReq.open("get", MarkedMathJaxWikiURL, true);
oReq.send();
}
}