@shevky/core 0.0.1 → 0.0.3

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.
@@ -0,0 +1,117 @@
1
+ import { i18n } from "@shevky/base";
2
+ import { ContentRegistry } from "../registries/contentRegistry.js";
3
+ import { MenuItem } from "../lib/menuItem.js";
4
+
5
+ /**
6
+ * @typedef {{ key: string, label: string, url: string, order: number }} MenuEntry
7
+ */
8
+
9
+ export class MenuEngine {
10
+ /**
11
+ * @type {ContentRegistry}
12
+ */
13
+ #_contentRegistry;
14
+
15
+ /**
16
+ * @type {{ buildContentUrl: (canonical: string | null | undefined, lang: string, slug: string) => string }}
17
+ */
18
+ #_metaEngine;
19
+
20
+ /**
21
+ * @type {Record<string, MenuEntry[]>}
22
+ */
23
+ #_cache = {};
24
+
25
+ /**
26
+ * @param {ContentRegistry} contentRegistry
27
+ * @param {{ buildContentUrl: (canonical: string | null | undefined, lang: string, slug: string) => string }} metaEngine
28
+ */
29
+ constructor(contentRegistry, metaEngine) {
30
+ this.#_contentRegistry = contentRegistry;
31
+ this.#_metaEngine = metaEngine;
32
+ }
33
+
34
+ async build() {
35
+ if (this.#_contentRegistry.count === 0) {
36
+ return;
37
+ }
38
+
39
+ for (const file of this.#_contentRegistry.files) {
40
+ const item = new MenuItem(file);
41
+ if (!item.isEligable) {
42
+ continue;
43
+ }
44
+
45
+ if (!Array.isArray(this.#_cache[item.lang])) {
46
+ this.#_cache[item.lang] = [];
47
+ }
48
+
49
+ const url = this.#_metaEngine.buildContentUrl(
50
+ item.url,
51
+ item.lang,
52
+ item.slug,
53
+ );
54
+ this.#_cache[item.lang].push({ ...item.toObject(), url });
55
+ }
56
+
57
+ Object.keys(this.#_cache).forEach((lang) => {
58
+ this.#_cache[lang].sort((a, b) => {
59
+ if (a.order === b.order) {
60
+ return a.label.localeCompare(b.label, lang);
61
+ }
62
+
63
+ return a.order - b.order;
64
+ });
65
+ });
66
+ }
67
+
68
+ /**
69
+ * @param {string} lang
70
+ * @param {string | null} activeKey
71
+ */
72
+ getMenuData(lang, activeKey) {
73
+ const baseItems = this.#_cache[lang] ?? [];
74
+ const normalizedActiveKey =
75
+ typeof activeKey === "string" && activeKey.trim().length > 0
76
+ ? activeKey.trim()
77
+ : "";
78
+ const hasExplicitMatch = normalizedActiveKey
79
+ ? baseItems.some((item) => item.key === normalizedActiveKey)
80
+ : false;
81
+ const resolvedActiveKey = hasExplicitMatch
82
+ ? normalizedActiveKey
83
+ : (baseItems[0]?.key ?? "");
84
+ const items = baseItems.map((item) => ({
85
+ ...item,
86
+ label: i18n.t(lang, `menu.${item.key}`, item.label ?? item.key),
87
+ isActive: item.key === resolvedActiveKey,
88
+ }));
89
+
90
+ return { items, activeKey: resolvedActiveKey };
91
+ }
92
+
93
+ /**
94
+ * @param {{id?: unknown, slug?:unknown} | null | undefined} frontMatter
95
+ */
96
+ resolveActiveMenuKey(frontMatter) {
97
+ if (!frontMatter) {
98
+ return null;
99
+ }
100
+
101
+ if (
102
+ typeof frontMatter.id === "string" &&
103
+ frontMatter.id.trim().length > 0
104
+ ) {
105
+ return frontMatter.id.trim();
106
+ }
107
+
108
+ if (
109
+ typeof frontMatter.slug === "string" &&
110
+ frontMatter.slug.trim().length > 0
111
+ ) {
112
+ return frontMatter.slug.trim();
113
+ }
114
+
115
+ return null;
116
+ }
117
+ }