Skip to content

Commit d419393

Browse files
committed
测试继续优化
1 parent 231885c commit d419393

3 files changed

Lines changed: 191 additions & 27 deletions

File tree

docs/javascripts/altium-loader.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(function () {
2+
const SCRIPT_ID = 'altium-embed-script';
3+
const SCRIPT_SRC = 'https://viewer.altium.com/client/static/js/embed.js';
4+
5+
function loadScript() {
6+
if (window.AltiumViewer || document.getElementById(SCRIPT_ID)) {
7+
return Promise.resolve();
8+
}
9+
return new Promise((resolve, reject) => {
10+
const script = document.createElement('script');
11+
script.id = SCRIPT_ID;
12+
script.src = SCRIPT_SRC;
13+
script.async = true;
14+
script.crossOrigin = 'anonymous';
15+
script.onload = () => resolve();
16+
script.onerror = () => reject(new Error('Failed to load Altium viewer script.'));
17+
document.head.appendChild(script);
18+
});
19+
}
20+
21+
function activateViewers(root) {
22+
const containers = root.querySelectorAll('.altium-iframe-viewer');
23+
if (!containers.length) {
24+
return;
25+
}
26+
27+
loadScript().catch((error) => {
28+
console.error('[Altium] Unable to load embed script:', error);
29+
});
30+
}
31+
32+
function handlePage(root) {
33+
const scope = root && root.querySelector ? root : document;
34+
activateViewers(scope);
35+
}
36+
37+
if (typeof document !== 'undefined') {
38+
document.addEventListener('DOMContentLoaded', () => handlePage(document));
39+
}
40+
41+
if (typeof document$ !== 'undefined') {
42+
document$.subscribe(() => handlePage(document));
43+
}
44+
})();

docs/javascripts/mathjax.js

Lines changed: 134 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,139 @@
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'
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+
});
1485
}
15-
};
1686

17-
document$.subscribe(() => {
18-
if (window.MathJax && typeof window.MathJax.typesetPromise === 'function') {
19-
window.MathJax.typesetPromise();
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);
20130
}
21-
if (window.pangu && typeof window.pangu.spacingPageBody === 'function') {
22-
window.pangu.spacingPageBody();
131+
132+
if (typeof document !== 'undefined') {
133+
document.addEventListener('DOMContentLoaded', handlePage);
23134
}
24-
});
25135

136+
if (typeof document$ !== 'undefined') {
137+
document$.subscribe(handlePage);
138+
}
139+
})();

mkdocs.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,17 @@ markdown_extensions:
273273
format: !!python/name:pymdownx.superfences.fence_code_format
274274

275275
extra_javascript:
276-
- https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js
277-
- javascripts/mathjax.js #https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/latest.min.js
278-
- https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6 #https://polyfill.io/v3/polyfill.min.js?features=es6
279-
- https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js
280-
- https://viewer.altium.com/client/static/js/embed.js # javascripts/embed.js
281-
- javascripts/adsense.js # AdSense广告脚本
282-
- javascripts/test-adsense.js # AdSense测试脚本(开发时使用)
276+
- javascripts/mathjax.js
277+
- javascripts/altium-loader.js
278+
- javascripts/adsense.js # AdSense script
279+
# - javascripts/test-adsense.js # AdSense test script (development only)
280+
281+
#---
282+
# - https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js
283+
# - javascripts/mathjax.js #https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/latest.min.js
284+
# - https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6 #https://polyfill.io/v3/polyfill.min.js?features=es6
285+
# - https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js
286+
# - https://viewer.altium.com/client/static/js/embed.js # javascripts/embed.js
287+
# - javascripts/adsense.js # AdSense广告脚本
288+
# - javascripts/test-adsense.js # AdSense测试脚本(开发时使用)
283289

0 commit comments

Comments
 (0)