@next-core/brick-playground 1.9.9 → 1.9.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/brick-playground",
3
- "version": "1.9.9",
3
+ "version": "1.9.10",
4
4
  "homepage": "https://github.com/easyops-cn/next-core/tree/v3/packages/brick-playground",
5
5
  "license": "GPL-3.0",
6
6
  "repository": {
@@ -43,5 +43,5 @@
43
43
  "monaco-editor-webpack-plugin": "^7.0.1",
44
44
  "webpack-dev-server": "^4.15.0"
45
45
  },
46
- "gitHead": "1f4fb165de7f1f1a9dd2de5586748b5d08d21d7c"
46
+ "gitHead": "e1b52c586f1442e38829bee673d9af1623540d4e"
47
47
  }
@@ -3,6 +3,9 @@ import { existsSync } from "node:fs";
3
3
  import { readdir, readFile } from "node:fs/promises";
4
4
  import walk from "./walk.js";
5
5
 
6
+ const REGEX_EXAMPLES_IN_MARKDOWN =
7
+ /(?:^###\s+(.+?)(?:\s+\{.*\})?\n[\s\S]*?)?^(```+)(html|yaml)(\s.*)?\n([\s\S]*?)\2/gm;
8
+
6
9
  /**
7
10
  *
8
11
  * @param {string} bricksDir
@@ -49,6 +52,23 @@ export default async function getExamples(bricksDir) {
49
52
  if (existsSync(srcPath)) {
50
53
  await walk(srcPath, visitExamples, [dir.name]);
51
54
  }
55
+ const docsDir = path.join(bricksDir, dir.name, "docs");
56
+ const examplesInMarkdown = await getExamplesInMarkdown(docsDir);
57
+ /** @type {string | undefined} */
58
+ let lastHeading;
59
+ for (const item of examplesInMarkdown) {
60
+ const stack = [dir.name, item.name];
61
+ const heading = item.heading ?? lastHeading;
62
+ lastHeading = heading;
63
+ if (heading) {
64
+ stack.push(heading.trim().toLowerCase());
65
+ }
66
+ const key = getDeduplicatedKey(stack.join("/"), exampleMap);
67
+ exampleMap.set(key, {
68
+ mode: item.mode,
69
+ [item.mode]: item.code,
70
+ });
71
+ }
52
72
  }
53
73
  })
54
74
  );
@@ -61,3 +81,57 @@ export default async function getExamples(bricksDir) {
61
81
  ...exampleMap.get(key),
62
82
  }));
63
83
  }
84
+
85
+ /**
86
+ * @param {string} docsDir
87
+ */
88
+ async function getExamplesInMarkdown(docsDir) {
89
+ if (!existsSync(docsDir)) {
90
+ return [];
91
+ }
92
+ const docs = await readdir(docsDir);
93
+ return (
94
+ await Promise.all(
95
+ docs.map(async (filename) => {
96
+ const examplesInMarkdown = [];
97
+ if (filename.endsWith(".md")) {
98
+ const filePath = path.join(docsDir, filename);
99
+ const content = await readFile(filePath, "utf-8");
100
+ /** @type {null|(string | undefined)[]} */
101
+ let matches;
102
+ while (
103
+ (matches = REGEX_EXAMPLES_IN_MARKDOWN.exec(content)) !== null
104
+ ) {
105
+ const [, heading, , mode, meta, code] = matches;
106
+ const metaParts = meta.trim().split(/\s+/);
107
+ if (metaParts.includes("preview")) {
108
+ examplesInMarkdown.push({
109
+ name: path.basename(filename, ".md").split(".").pop(),
110
+ heading,
111
+ mode,
112
+ meta,
113
+ code,
114
+ });
115
+ }
116
+ }
117
+ }
118
+ return examplesInMarkdown;
119
+ })
120
+ )
121
+ ).flat();
122
+ }
123
+
124
+ /**
125
+ * @param {string} key
126
+ * @param {Map<string, unknown>} map
127
+ * @returns {string}
128
+ */
129
+ function getDeduplicatedKey(key, map) {
130
+ let count = 2;
131
+ let cursor = key;
132
+ while (map.has(cursor)) {
133
+ cursor = `${key} (${count})`;
134
+ count++;
135
+ }
136
+ return cursor;
137
+ }