@mandolin/jsdoc-theme-hia 0.1.0

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,262 @@
1
+ "use strict";
2
+
3
+ function getHiaMetadata(doclet) {
4
+ return doclet && doclet.hia && typeof doclet.hia === "object"
5
+ ? doclet.hia
6
+ : null;
7
+ }
8
+
9
+ function getDocletI18n(doclet) {
10
+ const metadata = getHiaMetadata(doclet);
11
+
12
+ if (!metadata || !metadata.i18n || typeof metadata.i18n !== "object") {
13
+ return null;
14
+ }
15
+
16
+ return metadata.i18n;
17
+ }
18
+
19
+ function normalizeLocaleList(value) {
20
+ if (Array.isArray(value)) {
21
+ return value.filter((locale) => typeof locale === "string" && locale.trim());
22
+ }
23
+
24
+ return typeof value === "string" && value.trim() ? [value] : [];
25
+ }
26
+
27
+ function addLocaleCandidate(candidates, locale) {
28
+ if (typeof locale !== "string" || !locale.trim()) {
29
+ return;
30
+ }
31
+
32
+ const normalized = locale.trim();
33
+
34
+ if (!candidates.includes(normalized)) {
35
+ candidates.push(normalized);
36
+ }
37
+
38
+ const baseLocale = normalized.split("-")[0];
39
+
40
+ if (baseLocale && baseLocale !== normalized && !candidates.includes(baseLocale)) {
41
+ candidates.push(baseLocale);
42
+ }
43
+ }
44
+
45
+ function buildLocaleCandidates(locale, i18n, options = {}) {
46
+ const candidates = [];
47
+ const fallbackLocales = [
48
+ ...normalizeLocaleList(options.fallbackLocale),
49
+ ...normalizeLocaleList(options.fallbackLocales),
50
+ ...normalizeLocaleList(i18n && i18n.fallbackLocale),
51
+ ...normalizeLocaleList(i18n && i18n.fallbackLocales)
52
+ ];
53
+ const defaultLocales = [
54
+ ...normalizeLocaleList(options.defaultLocale),
55
+ ...normalizeLocaleList(i18n && i18n.defaultLocale)
56
+ ];
57
+
58
+ addLocaleCandidate(candidates, locale);
59
+
60
+ for (const fallbackLocale of fallbackLocales) {
61
+ addLocaleCandidate(candidates, fallbackLocale);
62
+ }
63
+
64
+ for (const defaultLocale of defaultLocales) {
65
+ addLocaleCandidate(candidates, defaultLocale);
66
+ }
67
+
68
+ return candidates;
69
+ }
70
+
71
+ function getI18nFields(i18n) {
72
+ return i18n && i18n.fields && typeof i18n.fields === "object"
73
+ ? i18n.fields
74
+ : {};
75
+ }
76
+
77
+ function getTextI18nField(doclet, fieldPath) {
78
+ const i18n = getDocletI18n(doclet);
79
+ const fields = getI18nFields(i18n);
80
+ const field = fields[fieldPath];
81
+
82
+ return field && typeof field === "object" ? field : null;
83
+ }
84
+
85
+ function createLocalizedResult(locale, resolvedLocale, text, source) {
86
+ return {
87
+ requestedLocale: locale,
88
+ resolvedLocale: resolvedLocale || "doclet",
89
+ usedFallback: Boolean(locale && resolvedLocale && resolvedLocale !== locale),
90
+ entry: {
91
+ locale: resolvedLocale || locale || "",
92
+ text: String(text || ""),
93
+ block: "",
94
+ source
95
+ }
96
+ };
97
+ }
98
+
99
+ function getLocalizedFieldEntry(doclet, fieldPath, locale, options = {}) {
100
+ const i18n = getDocletI18n(doclet);
101
+ const field = getTextI18nField(doclet, fieldPath);
102
+ const defaultText = options.defaultText || "";
103
+
104
+ if (i18n && field) {
105
+ const localizedText = field.localizedText && typeof field.localizedText === "object"
106
+ ? field.localizedText
107
+ : {};
108
+ const resolution = field.resolutions && typeof field.resolutions === "object"
109
+ ? field.resolutions[locale]
110
+ : null;
111
+ const candidates = [];
112
+
113
+ if (resolution && resolution.resolvedLocale) {
114
+ addLocaleCandidate(candidates, resolution.resolvedLocale);
115
+ }
116
+
117
+ for (const candidate of buildLocaleCandidates(locale, i18n, options)) {
118
+ addLocaleCandidate(candidates, candidate);
119
+ }
120
+
121
+ for (const candidate of candidates) {
122
+ if (Object.prototype.hasOwnProperty.call(localizedText, candidate)) {
123
+ return createLocalizedResult(locale, candidate, localizedText[candidate], `field:${fieldPath}`);
124
+ }
125
+ }
126
+
127
+ if (typeof field.text === "string") {
128
+ return createLocalizedResult(locale, "doclet", field.text, `field:${fieldPath}`);
129
+ }
130
+ }
131
+
132
+ return createLocalizedResult(locale, "doclet", defaultText, "doclet");
133
+ }
134
+
135
+ function getLocalizedEntry(doclet, locale, options = {}) {
136
+ const i18n = getDocletI18n(doclet);
137
+ const descriptionText = doclet.description || doclet.classdesc || "";
138
+
139
+ if (!i18n) {
140
+ return createLocalizedResult(locale, "doclet", descriptionText, "doclet");
141
+ }
142
+
143
+ const fieldResult = getLocalizedFieldEntry(doclet, "description", locale, {
144
+ ...options,
145
+ defaultText: descriptionText
146
+ });
147
+
148
+ if (fieldResult.entry.text) {
149
+ return fieldResult;
150
+ }
151
+
152
+ const localized = i18n.localized && typeof i18n.localized === "object"
153
+ ? i18n.localized
154
+ : {};
155
+
156
+ for (const candidate of buildLocaleCandidates(locale, i18n, options)) {
157
+ if (localized[candidate]) {
158
+ return {
159
+ requestedLocale: locale,
160
+ resolvedLocale: candidate,
161
+ usedFallback: candidate !== locale,
162
+ entry: localized[candidate]
163
+ };
164
+ }
165
+ }
166
+
167
+ return createLocalizedResult(locale, "doclet", descriptionText, "doclet");
168
+ }
169
+
170
+ function collectPageI18n(doclets) {
171
+ const locales = [];
172
+ let defaultLocale = "";
173
+ let fallbackLocale = "";
174
+ let mode = "";
175
+
176
+ for (const doclet of doclets || []) {
177
+ const i18n = getDocletI18n(doclet);
178
+
179
+ if (!i18n) {
180
+ continue;
181
+ }
182
+
183
+ defaultLocale = defaultLocale || i18n.defaultLocale || "";
184
+ fallbackLocale = fallbackLocale || normalizeLocaleList(i18n.fallbackLocale)[0] || "";
185
+ mode = mode || i18n.mode || "";
186
+
187
+ for (const locale of i18n.locales || []) {
188
+ if (!locales.includes(locale)) {
189
+ locales.push(locale);
190
+ }
191
+ }
192
+ }
193
+
194
+ return {
195
+ enabled: locales.length > 0,
196
+ defaultLocale: defaultLocale || locales[0] || "en",
197
+ fallbackLocale: fallbackLocale || defaultLocale || locales[0] || "en",
198
+ locales,
199
+ mode: mode || "runtimeSwitch"
200
+ };
201
+ }
202
+
203
+ function summarizeHiaMetadata(doclet) {
204
+ const metadata = getHiaMetadata(doclet);
205
+
206
+ if (!metadata) {
207
+ return {
208
+ hasHia: false,
209
+ microPlugins: [],
210
+ sourceReferenceCount: 0,
211
+ sourceDefinedIn: null,
212
+ sourcePrimaryBlock: null,
213
+ sourcePreviewEnabled: false,
214
+ sourceLinkEnabled: false,
215
+ locales: [],
216
+ defaultLocale: "",
217
+ fallbackLocale: "",
218
+ i18nMode: "",
219
+ i18nFieldCount: 0,
220
+ missingLocales: []
221
+ };
222
+ }
223
+
224
+ const source = metadata.source || {};
225
+ const references = Array.isArray(source.references) ? source.references : [];
226
+ const i18n = metadata.i18n || {};
227
+ const fields = getI18nFields(i18n);
228
+ const primaryBlock = source.primaryBlock || null;
229
+ const definedIn = source.definedIn || null;
230
+
231
+ return {
232
+ hasHia: true,
233
+ microPlugins: metadata.microPlugins || [],
234
+ sourceReferenceCount: references.length,
235
+ sourceDefinedIn: definedIn,
236
+ sourcePrimaryBlock: primaryBlock,
237
+ sourcePreviewEnabled: Boolean(
238
+ (primaryBlock && primaryBlock.preview && primaryBlock.preview.enabled !== false) ||
239
+ (source.preview && source.preview.enabled)
240
+ ),
241
+ sourceLinkEnabled: Boolean(
242
+ (definedIn && definedIn.link && definedIn.link.enabled) ||
243
+ (source.link && source.link.enabled)
244
+ ),
245
+ locales: Array.isArray(i18n.locales) ? i18n.locales : [],
246
+ defaultLocale: i18n.defaultLocale || "",
247
+ fallbackLocale: normalizeLocaleList(i18n.fallbackLocale)[0] || "",
248
+ i18nMode: i18n.mode || "",
249
+ i18nFieldCount: Object.keys(fields).length,
250
+ missingLocales: Array.isArray(i18n.missingLocales) ? i18n.missingLocales : []
251
+ };
252
+ }
253
+
254
+ module.exports = {
255
+ collectPageI18n,
256
+ getDocletI18n,
257
+ getHiaMetadata,
258
+ getLocalizedFieldEntry,
259
+ getLocalizedEntry,
260
+ getTextI18nField,
261
+ summarizeHiaMetadata
262
+ };