|
343 | 343 | const loadHLJSStyle = fetch(`https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/${style}.min.css`)
|
344 | 344 | .then((response) => response.text())
|
345 | 345 | .then((text) => {
|
346 |
| - const styleElement = document.createElement('style'); |
347 |
| - styleElement.textContent = scopeCss(text, '.' + styleClassName); |
348 |
| - document.head.appendChild(styleElement); |
| 346 | + insertStyle(scopeCss(text, '.' + styleClassName)); |
349 | 347 | });
|
350 | 348 |
|
351 | 349 | promises.push(loadHLJSNum);
|
|
356 | 354 | const loadMarkdownStyle = fetch('https://cdn.jsdelivr.net/gh/sindresorhus/[email protected]/github-markdown-light.min.css')
|
357 | 355 | .then((response) => response.text())
|
358 | 356 | .then((text) => {
|
359 |
| - const styleElement = document.createElement('style'); |
360 |
| - styleElement.textContent = text; |
361 |
| - document.head.appendChild(styleElement); |
| 357 | + insertStyle(text); |
362 | 358 | });
|
| 359 | + |
| 360 | + const loadKatexStyle = fetch('https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css') |
| 361 | + .then((response) => response.text()) |
| 362 | + .then((text) => { |
| 363 | + insertStyle(text.replaceAll('url(fonts/', 'url(https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/')); |
| 364 | + }); |
| 365 | + // TODO: is the 'defer' making sense? |
| 366 | + const loadKatex = typeof katex != "undefined" ? Promise.resolve() : loadScript('https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js', defer = true); |
| 367 | + const loadKatexAutoRender = loadKatex.then(() => typeof renderMathInElement != "undefined" ? Promise.resolve() : loadScript('https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js', defer = true)); |
| 368 | + |
363 | 369 | promises.push(loadMarked);
|
364 | 370 | promises.push(loadMarkdownStyle);
|
| 371 | + promises.push(loadKatexStyle); |
| 372 | + promises.push(loadKatexAutoRender); |
365 | 373 |
|
366 | 374 | if (type === 'ipynb') {
|
367 | 375 | const loadAnsiUp = typeof AnsiUp != "undefined" ? Promise.resolve() : loadScript('https://cdn.jsdelivr.net/gh/drudru/[email protected]/ansi_up.min.js');
|
|
372 | 380 | const ansi_up = new AnsiUp();
|
373 | 381 | // bind 'this' to fix 'TypeError: this.append_buffer is not a function'
|
374 | 382 | nb.ansi = ansi_up.ansi_to_html.bind(ansi_up);
|
| 383 | + // or: nb.ansi = (text) => { ansi_up.ansi_to_html(text); }; |
375 | 384 | });
|
376 | 385 | promises.push(loadNotebookjs);
|
377 | 386 | }
|
|
438 | 447 | targetDiv.querySelectorAll("pre code").forEach(codeTag => {
|
439 | 448 | hljs.highlightElement(codeTag);
|
440 | 449 | });
|
| 450 | + renderMathInElement(targetDiv.querySelector(".html-area"), { |
| 451 | + delimiters: [ |
| 452 | + { left: '$$', right: '$$', display: true }, |
| 453 | + { left: '$', right: '$', display: false }, |
| 454 | + { left: '\\(', right: '\\)', display: false }, |
| 455 | + { left: '\\[', right: '\\]', display: true }, |
| 456 | + ], |
| 457 | + throwOnError: false |
| 458 | + }); |
441 | 459 | }
|
442 | 460 |
|
443 | 461 | targetDiv.querySelector(".lds-ring").style.display = "none";
|
|
448 | 466 |
|
449 | 467 |
|
450 | 468 |
|
451 |
| -function loadScript(src) { |
| 469 | +function loadScript(src, defer = false) { |
452 | 470 | return new Promise((resolve, reject) => {
|
453 | 471 | const script = document.createElement('script');
|
454 | 472 | script.src = src;
|
| 473 | + script.defer = defer; |
455 | 474 | script.onload = resolve;
|
456 | 475 | script.onerror = reject;
|
457 | 476 | document.head.appendChild(script);
|
458 | 477 | });
|
459 | 478 | }
|
460 | 479 |
|
| 480 | +function insertStyle(text) { |
| 481 | + const styleElement = document.createElement('style'); |
| 482 | + styleElement.textContent = text; |
| 483 | + document.head.appendChild(styleElement); |
| 484 | +} |
| 485 | + |
461 | 486 | // https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
|
462 | 487 | function copyTextToClipboard(text) {
|
463 | 488 | if (!navigator.clipboard) {
|
|
0 commit comments