@blocklet/meta 1.7.6 → 1.7.7

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,78 @@
1
+ const get = require('lodash/get');
2
+ const normalizePathPrefix = require('@abtnode/util/lib/normalize-path-prefix');
3
+
4
+ /**
5
+ * @param {*} navigation src
6
+ * @param {*} blocklet
7
+ * @param {*} prefix prefix of link
8
+ * @param {*} level 1 or 2. primary menu or secondary menu
9
+ */
10
+ const parseNavigation = (navigation, blocklet, prefix = '/', level = 1) => {
11
+ const result = [];
12
+
13
+ (navigation || []).forEach((nav) => {
14
+ if (!nav.child) {
15
+ const item = {
16
+ title: nav.title,
17
+ };
18
+
19
+ if (nav.link) {
20
+ item.link = normalizePathPrefix(`${prefix}${nav.link || '/'}`);
21
+ }
22
+
23
+ if (level === 1) {
24
+ const list = parseNavigation(nav.items, blocklet, prefix, 2);
25
+ if (list.length) {
26
+ item.items = list;
27
+ }
28
+ }
29
+
30
+ result.push(item);
31
+ return;
32
+ }
33
+
34
+ if (!blocklet) {
35
+ return;
36
+ }
37
+
38
+ // parse child
39
+ const child = (blocklet.children || []).find((x) => [x.meta.name, x.meta.did].includes(nav.child));
40
+ if (!child) {
41
+ return;
42
+ }
43
+ const childTitle = child.meta.title || child.meta.name;
44
+
45
+ const childNavigation = get(child, 'meta.navigation', []);
46
+ if (!childNavigation.length) {
47
+ // child does not declares menu
48
+ result.push({
49
+ title: nav.title || childTitle,
50
+ link: normalizePathPrefix(`${prefix}${child.mountPoint || '/'}`),
51
+ });
52
+ } else if (childNavigation.length === 1) {
53
+ // child declares one menu
54
+ result.push({
55
+ title: nav.title || childNavigation[0].title || childTitle,
56
+ link: normalizePathPrefix(`${prefix}${child.mountPoint}${childNavigation[0].link || '/'}`),
57
+ });
58
+ } else {
59
+ // child declares multiple menus
60
+ if (level === 1) { // eslint-disable-line
61
+ // primary menu
62
+ const item = {
63
+ title: nav.title || child.meta.title || child.meta.name,
64
+ items: parseNavigation(childNavigation, null, normalizePathPrefix(`${prefix}${child.mountPoint}`), 2),
65
+ };
66
+ result.push(item);
67
+ } else {
68
+ // secondary menu
69
+ const list = parseNavigation(childNavigation, null, normalizePathPrefix(`${prefix}${child.mountPoint}`), 2);
70
+ result.push(...list);
71
+ }
72
+ }
73
+ });
74
+
75
+ return result;
76
+ };
77
+
78
+ module.exports = parseNavigation;
package/lib/schema.js CHANGED
@@ -161,8 +161,7 @@ const childrenSchema = Joi.object({
161
161
  services: Joi.array().items(serviceSchema).unique('name'),
162
162
  })
163
163
  )
164
- .optional()
165
- .default([]),
164
+ .optional(),
166
165
  mountPoint: Joi.string().trim().min(1), // added in 1.2.3
167
166
  services: Joi.array().items(serviceSchema).unique('name'), // added in 1.2.3
168
167
  }).custom((value) => {
@@ -196,6 +195,22 @@ const titleSchema = Joi.string()
196
195
  });
197
196
  const descriptionSchema = Joi.string().trim().min(3).max(160);
198
197
 
198
+ const navigationItemSchema = Joi.object({
199
+ title: Joi.string().required(),
200
+ link: Joi.string(),
201
+ child: Joi.string(), // child name or child did
202
+ });
203
+
204
+ const navigationSchema = Joi.array().items(
205
+ navigationItemSchema.append({
206
+ items: Joi.array().items(navigationItemSchema),
207
+ })
208
+ );
209
+
210
+ const themeSchema = Joi.object({
211
+ background: Joi.string(),
212
+ });
213
+
199
214
  const createBlockletSchema = (
200
215
  baseDir,
201
216
  { ensureMain = false, ensureFiles = false, ensureDist = false, ...schemaOptions } = {}
@@ -397,6 +412,10 @@ const createBlockletSchema = (
397
412
 
398
413
  // blocklet component support
399
414
  children: Joi.array().items(childrenSchema).optional().default([]),
415
+
416
+ // navigation & theme
417
+ navigation: navigationSchema,
418
+ theme: themeSchema,
400
419
  })
401
420
  .rename('public_url', 'publicUrl', { ignoreUndefined: true, override: true })
402
421
  .rename('admin_url', 'adminUrl', { ignoreUndefined: true, override: true })
@@ -415,4 +434,6 @@ module.exports = {
415
434
  distSchema,
416
435
  titleSchema,
417
436
  descriptionSchema,
437
+ navigationSchema,
438
+ themeSchema,
418
439
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.7.6",
6
+ "version": "1.7.7",
7
7
  "description": "Library to parse/validate/fix blocklet meta",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,7 +18,8 @@
18
18
  "author": "wangshijun <wangshijun2020@gmail.com> (http://github.com/wangshijun)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.7.6",
21
+ "@abtnode/constant": "1.7.7",
22
+ "@abtnode/util": "1.7.7",
22
23
  "@arcblock/did": "^1.16.0",
23
24
  "@arcblock/did-ext": "^1.16.0",
24
25
  "@arcblock/did-util": "^1.16.0",
@@ -44,5 +45,5 @@
44
45
  "devDependencies": {
45
46
  "jest": "^27.4.5"
46
47
  },
47
- "gitHead": "47a9dbd6ea74419ff586336824ebb9b2fe7694aa"
48
+ "gitHead": "619db37ea7a91c64a9bf30836a55c70d55325e73"
48
49
  }