qumra-engine 2.0.54 → 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,45 +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) =>
15
- // String(str || "")
16
- // .replace(/&/g, "&amp;")
17
- // .replace(/</g, "&lt;")
18
- // .replace(/>/g, "&gt;")
19
- // .replace(/"/g, "&quot;");
20
- const getTextOnly = (str) => String(str || "").replace(/<[^>]*>/g, "");
21
- const seo = ctx.context.seo || {};
22
- const page = ctx.context.page || {};
23
- const store = ctx.context.store || {};
24
- const pageUrl = seo.canonical || "";
25
- const pageTitle = seo.title || page.title || "Store";
26
- const pageDescription = seo.description || page.description || "";
27
- const pageImage = seo.image || "";
28
- const favicon = seo.logo || "/favicon.ico"; // الأولوية للوجو
29
- let tags = `
30
- <title>${getTextOnly(pageTitle)}</title>
31
- ${favicon
32
- ? `<link rel="icon" type="image/png" href="${getTextOnly(favicon)}">`
33
- : ""}
34
- ${pageDescription
35
- ? `<meta name="description" content="${getTextOnly(pageDescription)}">`
36
- : ""}
37
- ${pageUrl ? `<link rel="canonical" href="${getTextOnly(pageUrl)}">` : ""}
38
- <meta property="og:title" content="${getTextOnly(pageTitle)}">
39
- ${pageDescription
40
- ? `<meta property="og:description" content="${getTextOnly(pageDescription)}">`
41
- : ""}
42
- ${pageImage
43
- ? `<meta property="og:image" content="${getTextOnly(pageImage)}">`
44
- : ""}
45
- ${pageUrl
46
- ? `<meta property="og:url" content="${getTextOnly(pageUrl)}">`
47
- : ""}
48
- <meta property="og:type" content="website">
49
- ${pageImage
50
- ? `<meta name="twitter:card" content="summary_large_image">`
51
- : `<meta name="twitter:card" content="summary">`}
52
- `;
53
- 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);
54
98
  }
55
99
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qumra-engine",
3
- "version": "2.0.54",
3
+ "version": "2.0.55",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {