nodebb-plugin-katex2 1.0.1 → 1.0.2

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.1",
3
+ "version": "1.0.2",
4
4
  "description": "KaTeX math rendering plugin for NodeBB",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -1,27 +1,77 @@
1
+ "use strict";
2
+
1
3
  const fs = require("fs");
2
4
  const path = require("path");
3
5
 
4
- const sourceDir = path.join(__dirname, "../node_modules/katex/dist");
5
- const targetDir = path.join(__dirname, "../static/katex");
6
+ function copyDir(src, dest) {
7
+ if (!fs.existsSync(dest)) {
8
+ fs.mkdirSync(dest, { recursive: true });
9
+ }
6
10
 
7
- // Создаем целевую директорию если её нет
8
- if (!fs.existsSync(targetDir)) {
9
- fs.mkdirSync(targetDir, { recursive: true });
10
- }
11
+ const entries = fs.readdirSync(src, { withFileTypes: true });
11
12
 
12
- // Копируем файлы
13
- function copyRecursive(src, dest) {
14
- if (fs.statSync(src).isDirectory()) {
15
- if (!fs.existsSync(dest)) {
16
- fs.mkdirSync(dest, { recursive: true });
13
+ for (const entry of entries) {
14
+ const srcPath = path.join(src, entry.name);
15
+ const destPath = path.join(dest, entry.name);
16
+
17
+ if (entry.isDirectory()) {
18
+ copyDir(srcPath, destPath);
19
+ } else {
20
+ fs.copyFileSync(srcPath, destPath);
17
21
  }
18
- fs.readdirSync(src).forEach((file) => {
19
- copyRecursive(path.join(src, file), path.join(dest, file));
20
- });
21
- } else {
22
- fs.copyFileSync(src, dest);
23
22
  }
24
23
  }
25
24
 
26
- copyRecursive(sourceDir, targetDir);
27
- console.log("KaTeX files copied successfully");
25
+ function findKatexDist() {
26
+ const pluginRoot = path.resolve(__dirname, "..");
27
+
28
+ // Вариант 1: Локальный node_modules плагина
29
+ const localPath = path.join(pluginRoot, "node_modules", "katex", "dist");
30
+ if (fs.existsSync(localPath)) {
31
+ console.log("[KaTeX] Found in local node_modules");
32
+ return localPath;
33
+ }
34
+
35
+ // Вариант 2: Hoisted в корень NodeBB (../../node_modules/katex)
36
+ const hoistedPath = path.join(
37
+ pluginRoot,
38
+ "..",
39
+ "..",
40
+ "node_modules",
41
+ "katex",
42
+ "dist",
43
+ );
44
+ if (fs.existsSync(hoistedPath)) {
45
+ console.log("[KaTeX] Found in hoisted node_modules");
46
+ return hoistedPath;
47
+ }
48
+
49
+ return null;
50
+ }
51
+
52
+ try {
53
+ console.log("[KaTeX Plugin] Running postinstall...");
54
+
55
+ const pluginRoot = path.resolve(__dirname, "..");
56
+ const katexSource = findKatexDist();
57
+ const katexDest = path.join(pluginRoot, "static", "katex");
58
+
59
+ if (!katexSource) {
60
+ console.error("[KaTeX] ERROR: katex not found!");
61
+ process.exit(1);
62
+ }
63
+
64
+ if (fs.existsSync(katexDest)) {
65
+ fs.rmSync(katexDest, { recursive: true, force: true });
66
+ }
67
+
68
+ console.log("[KaTeX] Copying from:", katexSource);
69
+ console.log("[KaTeX] Copying to:", katexDest);
70
+
71
+ copyDir(katexSource, katexDest);
72
+
73
+ console.log("[KaTeX] Postinstall completed!");
74
+ } catch (err) {
75
+ console.error("[KaTeX] Postinstall failed:", err);
76
+ process.exit(1);
77
+ }