qumra-engine 2.0.53 → 2.0.55

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.
@@ -1,6 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const nunjucks_1 = require("nunjucks");
4
+ const escapeAttr = (str) => String(str ?? "")
5
+ .replace(/&/g, "&")
6
+ .replace(/</g, "&lt;")
7
+ .replace(/>/g, "&gt;")
8
+ .replace(/"/g, "&quot;")
9
+ .replace(/'/g, "&#39;");
10
+ const textOnly = (str, limit = 160) => {
11
+ const s = String(str ?? "")
12
+ .replace(/<[^>]*>/g, "") // شيل أي HTML
13
+ .replace(/\s+/g, " ") // سطر واحد
14
+ .trim();
15
+ return limit > 0 && s.length > limit ? s.slice(0, limit - 1).trim() + "…" : s;
16
+ };
17
+ const safeUrl = (str) => {
18
+ const s = String(str ?? "").trim();
19
+ if (!s)
20
+ return "";
21
+ if (/^https?:\/\//i.test(s))
22
+ return s;
23
+ if (s.startsWith("/"))
24
+ return s; // مسار داخلي
25
+ return ""; // غير مسموح
26
+ };
4
27
  exports.default = new (class SeoExtension {
5
28
  constructor() {
6
29
  this.tags = ["seo"];
@@ -11,31 +34,66 @@ exports.default = new (class SeoExtension {
11
34
  return new nodes.CallExtension(this, "run", null);
12
35
  }
13
36
  run({ ctx }) {
14
- const escapeHtml = (str) => String(str || "")
15
- .replace(/&/g, "&amp;")
16
- .replace(/</g, "&lt;")
17
- .replace(/>/g, "&gt;")
18
- .replace(/"/g, "&quot;");
19
- const seo = ctx.context.seo || {};
20
- const page = ctx.context.page || {};
21
- const store = ctx.context.store || {};
22
- const pageUrl = seo.canonical || "";
23
- const pageTitle = seo.title || page.title || "Store";
24
- const pageDescription = seo.description || page.description || "";
25
- const pageImage = seo.image || "";
26
- const favicon = seo.logo || "/favicon.ico"; // الأولوية للوجو
27
- let tags = `
28
- <title>${escapeHtml(pageTitle)}</title>
29
- ${favicon ? `<link rel="icon" type="image/png" href="${escapeHtml(favicon)}">` : ""}
30
- ${pageDescription ? `<meta name="description" content="${escapeHtml(pageDescription)}">` : ""}
31
- ${pageUrl ? `<link rel="canonical" href="${escapeHtml(pageUrl)}">` : ""}
32
- <meta property="og:title" content="${escapeHtml(pageTitle)}">
33
- ${pageDescription ? `<meta property="og:description" content="${escapeHtml(pageDescription)}">` : ""}
34
- ${pageImage ? `<meta property="og:image" content="${escapeHtml(pageImage)}">` : ""}
35
- ${pageUrl ? `<meta property="og:url" content="${escapeHtml(pageUrl)}">` : ""}
36
- <meta property="og:type" content="website">
37
- ${pageImage ? `<meta name="twitter:card" content="summary_large_image">` : `<meta name="twitter:card" content="summary">`}
38
- `;
39
- return new nunjucks_1.runtime.SafeString(tags.trim());
37
+ const c = ctx.context || {};
38
+ const seo = c.seo || {};
39
+ const page = c.page || {};
40
+ const store = c.store || {};
41
+ // قيم أساسية
42
+ const siteName = store?.name || "Store";
43
+ const titleRaw = seo.title || page.title || siteName;
44
+ const descRaw = seo.description || page.description || "";
45
+ const pageTitle = textOnly(titleRaw, 70); // يفضّل ≤ 60–70
46
+ const pageDesc = textOnly(descRaw, 160);
47
+ const canonical = safeUrl(seo.canonical || page.canonical || "");
48
+ const ogImage = safeUrl(seo.image || page.image || "");
49
+ const favicon = safeUrl(seo.logo || "/favicon.ico");
50
+ const noindex = !!seo.noindex;
51
+ // بناء الوسوم
52
+ const parts = [];
53
+ // Title
54
+ parts.push(`<title>${escapeAttr(pageTitle)}</title>`);
55
+ // Canonical
56
+ if (canonical)
57
+ parts.push(`<link rel="canonical" href="${escapeAttr(canonical)}">`);
58
+ // Favicon
59
+ if (favicon) {
60
+ parts.push(`<link rel="icon" href="${escapeAttr(favicon)}">`);
61
+ parts.push(`<link rel="shortcut icon" href="${escapeAttr(favicon)}">`);
62
+ }
63
+ // Meta description
64
+ if (pageDesc)
65
+ parts.push(`<meta name="description" content="${escapeAttr(pageDesc)}">`);
66
+ // Robots
67
+ if (noindex) {
68
+ parts.push(`<meta name="robots" content="noindex, nofollow">`);
69
+ parts.push(`<meta property="og:robots" content="noindex, nofollow">`);
70
+ }
71
+ // Open Graph
72
+ parts.push(`<meta property="og:type" content="website">`);
73
+ parts.push(`<meta property="og:title" content="${escapeAttr(pageTitle)}">`);
74
+ if (pageDesc)
75
+ parts.push(`<meta property="og:description" content="${escapeAttr(pageDesc)}">`);
76
+ if (canonical)
77
+ parts.push(`<meta property="og:url" content="${escapeAttr(canonical)}">`);
78
+ if (siteName)
79
+ parts.push(`<meta property="og:site_name" content="${escapeAttr(siteName)}">`);
80
+ if (ogImage)
81
+ parts.push(`<meta property="og:image" content="${escapeAttr(ogImage)}">`);
82
+ // Twitter
83
+ parts.push(`<meta name="twitter:card" content="${ogImage ? "summary_large_image" : "summary"}">`);
84
+ parts.push(`<meta name="twitter:title" content="${escapeAttr(pageTitle)}">`);
85
+ if (pageDesc)
86
+ parts.push(`<meta name="twitter:description" content="${escapeAttr(pageDesc)}">`);
87
+ if (ogImage)
88
+ parts.push(`<meta name="twitter:image" content="${escapeAttr(ogImage)}">`);
89
+ // Itemprop (Google)
90
+ parts.push(`<meta itemprop="name" content="${escapeAttr(pageTitle)}">`);
91
+ if (pageDesc)
92
+ parts.push(`<meta itemprop="description" content="${escapeAttr(pageDesc)}">`);
93
+ if (ogImage)
94
+ parts.push(`<meta itemprop="image" content="${escapeAttr(ogImage)}">`);
95
+ // إخراج منظّف
96
+ const html = parts.join("\n");
97
+ return new nunjucks_1.runtime.SafeString(html);
40
98
  }
41
99
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qumra-engine",
3
- "version": "2.0.53",
3
+ "version": "2.0.55",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {