-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
239 lines (217 loc) · 5.94 KB
/
loader.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
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
((window, document) =>
{
if (window['hljsLoader'])
{
return;
}
/**
* Fill the map of aliases to the language's canonical name
*/
function generateAliasMap()
{
const packedMap = '1c abnf accesslog actionscript,as ada angelscript,asc apache%conf applescript,osascript arcade arduino arm!asm asciidoc,adoc aspectj autohotkey,ahk autoit avrasm awk axapta,x++ basic bnf brainfuck,bf cal capnp!roto ceylon clean,dcl,icl clojure,clj,edn clojure-repl cmake coffee!script,cson,iced coq cos,cls crm!sh,pcmk cr!ystal csp d dart delphi,dfm,dpr,pas!cal django,jinja dns,bind,zone docker!file dos,bat,cmd dsconfig dts dust,dst ebnf elixir,ex!s elm erb erl!ang erlang-repl excel,xls!x fix flix fortran,f90,f95 fs!harp,f# gams,gms gauss,gss gcode,nc gherkin,feature glsl gml golo gradle graphql,gql groovy haml handlebars,hbs,htmlbars haskell,hs haxe,hx hsp http%s hy%lang inform7,i7 irpf90 isbl jboss-cli,wildfly-cli julia julia-repl,jldoctest kotlin,kt!s lasso%script,ls latex,tex ldif leaf lisp livecodeserver livescript,ls llvm lsl mathematica,mma,wl matlab maxima mel m!ercury,moo mips!asm mizar mojolicious monkey moon!script n1ql nestedtext,nt nginx%conf nim nix%os node-repl nsis ocaml,ml openscad,scad oxygene parser3 pf pgsql,postgres!ql pony powershell,ps!1,pwsh processing,pde profile prolog properties proto!buf puppet,pp purebasic,pb!i q,k!db qml,qt re!asonml rib roboconf,graph,instances routeros,mikrotik rsl ruleslanguage sas scala scheme,scm sci!lab smali smalltalk,st sml,ml sqf sql stan%funcs stata,ado,do step!21,p21,stp styl!us subunit taggerscript tap tcl,tk thrift tp twig,craftcms vala vbs!cript vbscript-html v!erilog,sv!h vhdl vim wasm wren x86asm xl,tao xq!uery,xpath,xqm zep!hir';
map = {};
for (let str of packedMap.replace(/(\w+)(?:%|!(\w+))/g, '$1$2,$1').split(' '))
{
let aliases = str.split(',');
for (let alias of aliases)
{
map[alias] = aliases[0];
}
}
}
/**
* @param {string} path Path to the script, relative to the root URL and minus the .min.js suffix
* @param {!Function} onload Callback for the "load" event
*/
function createScript(path, onload)
{
let script = /** @type {!HTMLScriptElement} */ (document.createElement('script'));
// Language files can be loaded asynchronously if highlight.js has already been loaded
script.async = hljsLoaded;
script.defer = true;
script.onload = onload;
script.src = url + path + '.min.js';
if (nonce)
{
script.nonce = nonce;
}
document.head.append(script);
}
/**
* Return the canonical language for given code element (empty if no language found)
*
* @param {!Element} element
* @return {string}
*/
function getLangFromElement(element)
{
let m = /\blang(?:uage)?-(\w+)/.exec(element.className.toLowerCase());
if (m && !map)
{
// Delay generating the map until we actually need it
generateAliasMap();
}
return (m) ? (map[m[1]] || '') : '';
}
/**
* Highlight all code blocks in current document
*/
function highlightAll()
{
highlightBlocks(document.body);
}
/**
* Highlight given code element
*
* @param {!Element} element
*/
function highlightElement(element)
{
loadHljs();
loadLang(getLangFromElement(element));
if (window['hljs'])
{
window['hljs']['highlightElement'](element);
}
}
/**
* Highlight all code elements under given root element
*
* @param {!Element} root
*/
function highlightBlocks(root)
{
let elements = root.querySelectorAll('pre>code:not(.hljs)');
if (elements.length)
{
observerStop();
for (let element of elements)
{
highlightElement(element);
}
observerStart();
}
}
/**
* Load highlight.js and schedule a document-wide highlighting if not already available
*/
function loadHljs()
{
if (hljsLoaded)
{
return;
}
hljsLoaded = true;
if (style !== 'none')
{
let link = /** @type {!HTMLLinkElement} */ (document.createElement('link'));
link.rel = 'stylesheet';
link.href = url + 'styles/' + style + '.min.css';
document.head.append(link);
}
createScript('highlight', () =>
{
if (options)
{
window['hljs']['configure'](JSON.parse(options));
}
highlightAll();
});
}
/**
* Load given extra language and schedule a document-wide highlighting
*
* @param {string} lang
*/
function loadLang(lang)
{
if (skip[lang])
{
return;
}
skip[lang] = 1;
createScript('languages/' + lang, highlightAll);
}
function observerStart()
{
if (!observeTarget)
{
return;
}
let target = document.querySelector(observeTarget);
if (!target)
{
return;
}
if (!observer)
{
observer = new MutationObserver(
(mutations) => mutations.forEach(
(mutation) =>
{
if (!mutation.addedNodes.length)
{
return;
}
mutation.addedNodes.forEach(
(node) => {
if (node instanceof Element)
{
highlightBlocks(node);
}
}
);
}
)
);
}
observer.observe(target, { 'childList': true, 'subtree': true });
}
function observerStop()
{
if (observer)
{
observer.disconnect();
}
}
function init()
{
highlightAll();
observerStart();
}
// Initialize global variables
let currentScript = document.currentScript,
config = currentScript.dataset,
hljsLoaded = false,
map,
nonce = currentScript.nonce,
observeTarget = config['hljsObserve'],
observer,
options = config['hljsOptions'],
skip = {'':1},
style = config['hljsStyle'] || 'default',
url = config['hljsUrl'] || 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.0/build/';
// Prepare to highlight all code blocks as soon as possible
if (document.readyState === 'complete')
{
init();
}
else
{
window.addEventListener('load', init);
}
window['hljsLoader'] = {
'disconnect': () =>
{
observerStop();
observeTarget = '';
},
'highlightBlocks': highlightBlocks,
'observe': (target) =>
{
observeTarget = target;
observerStart();
}
};
})(window, document);