nodebb-plugin-katex2 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-katex2",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "KaTeX math rendering plugin for NodeBB",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -15,7 +15,7 @@
15
15
 
16
16
  // === Debounce таймеры ===
17
17
  let renderTimer = null;
18
- const RENDER_DELAY = 100; // Задержка перед рендерингом (мс)
18
+ const RENDER_DELAY = 150; // Задержка перед рендерингом (мс)
19
19
 
20
20
  // === Регулярное выражение для быстрой проверки ===
21
21
  const MATH_PATTERN = /\$\$|\\\[|\\\(/;
@@ -37,6 +37,18 @@
37
37
  };
38
38
  }
39
39
 
40
+ /**
41
+ * Проверка полной загрузки KaTeX
42
+ */
43
+ function isKatexReady() {
44
+ return (
45
+ typeof window.katex !== "undefined" &&
46
+ typeof window.katex.render === "function" &&
47
+ typeof window.renderMathInElement === "function" &&
48
+ typeof window.katex.__parse !== "undefined" // внутренняя проверка
49
+ );
50
+ }
51
+
40
52
  /**
41
53
  * Проверка наличия формул в элементе
42
54
  * @param {HTMLElement} element
@@ -134,6 +146,32 @@
134
146
  });
135
147
  }
136
148
 
149
+ /**
150
+ * Ожидание готовности KaTeX с таймаутом
151
+ */
152
+ function waitForKatex(maxAttempts = 50, interval = 50) {
153
+ return new Promise((resolve, reject) => {
154
+ let attempts = 0;
155
+
156
+ const check = () => {
157
+ if (isKatexReady()) {
158
+ resolve();
159
+ return;
160
+ }
161
+
162
+ attempts++;
163
+ if (attempts >= maxAttempts) {
164
+ reject(new Error("KaTeX did not initialize in time"));
165
+ return;
166
+ }
167
+
168
+ setTimeout(check, interval);
169
+ };
170
+
171
+ check();
172
+ });
173
+ }
174
+
137
175
  /**
138
176
  * Загрузка библиотеки KaTeX
139
177
  * @returns {Promise}
@@ -158,10 +196,14 @@
158
196
  // Правильный путь согласно plugin.json staticDirs
159
197
  const basePath = "/assets/plugins/nodebb-plugin-katex2/katex/";
160
198
 
199
+ await loadScript(basePath + "katex.min.js");
200
+
201
+ // 3. Ждем инициализации katex
202
+ await waitForKatex();
203
+
161
204
  // Параллельная загрузка всех ресурсов
162
205
  await Promise.all([
163
206
  loadCSS(basePath + "katex.min.css"),
164
- loadScript(basePath + "katex.min.js"),
165
207
  loadScript(basePath + "contrib/auto-render.min.js"),
166
208
  ]);
167
209