|
1 | | -(function () { |
2 | | - const MATHJAX_SRC = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js'; |
3 | | - const PANGU_SRC = (typeof document !== 'undefined') ? new URL('javascripts/pangu.min.js', document.baseURI).href : '/javascripts/pangu.min.js'; |
4 | | - const pendingScripts = new Map(); |
5 | | - let mathjaxReady; |
6 | | - |
7 | | - window.MathJax = { |
8 | | - tex: { |
9 | | - inlineMath: [["\(", "\)"]], |
10 | | - displayMath: [["\[", "\]"]], |
11 | | - processEscapes: true, |
12 | | - processEnvironments: true |
13 | | - }, |
14 | | - options: { |
15 | | - ignoreHtmlClass: ".*|", |
16 | | - processHtmlClass: "arithmatex" |
17 | | - }, |
18 | | - chtml: { |
19 | | - fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/output/chtml/fonts/woff-v2' |
20 | | - } |
21 | | - }; |
22 | | - |
23 | | - function loadScript(id, src, attrs) { |
24 | | - if (pendingScripts.has(id)) { |
25 | | - return pendingScripts.get(id); |
26 | | - } |
27 | | - |
28 | | - if (document.getElementById(id)) { |
29 | | - return Promise.resolve(); |
30 | | - } |
31 | | - |
32 | | - const promise = new Promise((resolve, reject) => { |
33 | | - const script = document.createElement('script'); |
34 | | - script.id = id; |
35 | | - script.src = src; |
36 | | - script.async = true; |
37 | | - |
38 | | - if (attrs) { |
39 | | - Object.keys(attrs).forEach((key) => { |
40 | | - if (key === 'defer' || key === 'async') { |
41 | | - script[key] = Boolean(attrs[key]); |
42 | | - } else { |
43 | | - script.setAttribute(key, attrs[key]); |
44 | | - } |
45 | | - }); |
46 | | - } |
47 | | - |
48 | | - script.onload = () => resolve(); |
49 | | - script.onerror = () => reject(new Error('Failed to load script: ' + src)); |
50 | | - document.head.appendChild(script); |
51 | | - }); |
52 | | - |
53 | | - pendingScripts.set(id, promise); |
54 | | - return promise; |
55 | | - } |
56 | | - |
57 | | - function ensureMathJax(root) { |
58 | | - if (!root.querySelector('.arithmatex')) { |
59 | | - return; |
60 | | - } |
61 | | - |
62 | | - if (window.MathJax && typeof window.MathJax.typesetPromise === 'function') { |
63 | | - window.MathJax.typesetPromise(); |
64 | | - return; |
65 | | - } |
66 | | - |
67 | | - if (!mathjaxReady) { |
68 | | - mathjaxReady = loadScript('mathjax-cdn-script', MATHJAX_SRC, { defer: true, crossorigin: 'anonymous' }) |
69 | | - .then(() => { |
70 | | - if (window.MathJax && window.MathJax.startup && window.MathJax.startup.promise) { |
71 | | - return window.MathJax.startup.promise; |
72 | | - } |
73 | | - return undefined; |
74 | | - }) |
75 | | - .catch((error) => { |
76 | | - console.error('[MathJax] Unable to load:', error); |
77 | | - }); |
78 | | - } |
79 | | - |
80 | | - mathjaxReady?.then(() => { |
81 | | - if (window.MathJax && typeof window.MathJax.typesetPromise === 'function') { |
82 | | - window.MathJax.typesetPromise(); |
83 | | - } |
84 | | - }); |
| 1 | +window.MathJax = { |
| 2 | + tex: { |
| 3 | + inlineMath: [["\\(", "\\)"]], |
| 4 | + displayMath: [["\\[", "\\]"]], |
| 5 | + processEscapes: true, |
| 6 | + processEnvironments: true |
| 7 | + }, |
| 8 | + options: { |
| 9 | + ignoreHtmlClass: ".*|", |
| 10 | + processHtmlClass: "arithmatex" |
| 11 | + }, |
| 12 | + chtml: { |
| 13 | + fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/output/chtml/fonts/woff-v2' |
85 | 14 | } |
| 15 | +}; |
86 | 16 |
|
87 | | - function applyPangu(root) { |
88 | | - const target = root.querySelector('.md-content'); |
89 | | - if (!target || !window.pangu) { |
90 | | - return; |
91 | | - } |
92 | | - |
93 | | - if (typeof window.pangu.spacingElement === 'function') { |
94 | | - window.pangu.spacingElement(target); |
95 | | - } else if (typeof window.pangu.spacingPageBody === 'function') { |
96 | | - window.pangu.spacingPageBody(); |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - let panguLoading; |
101 | | - |
102 | | - function ensurePangu(root) { |
103 | | - if (document.documentElement && document.documentElement.lang && document.documentElement.lang.indexOf('zh') !== 0) { |
104 | | - return; |
105 | | - } |
106 | | - |
107 | | - if (!root.querySelector('.md-content')) { |
108 | | - return; |
109 | | - } |
110 | | - |
111 | | - if (window.pangu) { |
112 | | - applyPangu(root); |
113 | | - return; |
114 | | - } |
115 | | - |
116 | | - if (!panguLoading) { |
117 | | - panguLoading = loadScript('pangu-script', PANGU_SRC, { defer: true }) |
118 | | - .catch((error) => { |
119 | | - console.error('[Pangu] Unable to load:', error); |
120 | | - }); |
121 | | - } |
122 | | - |
123 | | - panguLoading?.then(() => applyPangu(root)); |
124 | | - } |
125 | | - |
126 | | - function handlePage() { |
127 | | - const root = document; |
128 | | - ensureMathJax(root); |
129 | | - ensurePangu(root); |
| 17 | +document$.subscribe(() => { |
| 18 | + if (window.MathJax && typeof window.MathJax.typesetPromise === 'function') { |
| 19 | + window.MathJax.typesetPromise(); |
130 | 20 | } |
131 | | - |
132 | | - if (typeof document !== 'undefined') { |
133 | | - document.addEventListener('DOMContentLoaded', handlePage); |
| 21 | + if (window.pangu && typeof window.pangu.spacingPageBody === 'function') { |
| 22 | + window.pangu.spacingPageBody(); |
134 | 23 | } |
| 24 | +}); |
135 | 25 |
|
136 | | - if (typeof document$ !== 'undefined') { |
137 | | - document$.subscribe(handlePage); |
138 | | - } |
139 | | -})(); |
0 commit comments