@blocklet/pages-kit-block-studio 0.1.4 → 0.1.5

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.
@@ -239,12 +239,10 @@ function getCustomComponentPropertyType(type) {
239
239
  }
240
240
  const ALLOWED_PROPERTY_TYPES = ['string', 'multiline', 'url', 'json', 'yaml'];
241
241
  function generatePageDataTypes(state) {
242
- const pageTypes = Object.values(state.pages)
242
+ const pageTypeDefinitions = Object.values(state.pages)
243
243
  .map((page) => {
244
- // check if the page is a template page
245
244
  if (!page.isTemplate)
246
245
  return null;
247
- // 将 slug 转换为有效的 TypeScript 类型名
248
246
  const typeName = `Page${page.slug
249
247
  .replace(/^\//, '')
250
248
  .split('/')
@@ -295,8 +293,7 @@ ${properties}
295
293
  })
296
294
  .filter(Boolean)
297
295
  .join('\n');
298
- return `
299
- export interface ${typeName} {
296
+ const typeDefinition = `interface ${typeName} {
300
297
  title?: string;
301
298
  image?: string;
302
299
  description?: string;
@@ -304,29 +301,28 @@ export interface ${typeName} {
304
301
  ${sectionTypes}
305
302
  };
306
303
  }`;
304
+ return {
305
+ typeName,
306
+ typeDefinition,
307
+ };
307
308
  })
308
- .filter(Boolean)
309
- .join('\n');
310
- // 生成 PageData 类型的联合类型
311
- const pageUnionTypes = Object.values(state.pages)
312
- .map((page) => {
313
- if (!page.isTemplate)
314
- return null;
315
- const typeName = `Page${page.slug
316
- .replace(/^\//, '')
317
- .split('/')
318
- .map((s) => s.charAt(0).toUpperCase() + s.slice(1))
319
- .join('')
320
- .replace(/[^a-zA-Z0-9]/g, '')}Data`;
321
- return typeName;
322
- })
323
- .filter(Boolean)
324
- .join(' | ');
309
+ .filter(Boolean);
310
+ // Generate type definitions string
311
+ const typeDefinitions = pageTypeDefinitions.map((def) => `export ${def === null || def === void 0 ? void 0 : def.typeDefinition}`).join('\n\n');
312
+ // Generate string literals for each type
313
+ const typeStrings = pageTypeDefinitions
314
+ .map((def) => `export const ${def === null || def === void 0 ? void 0 : def.typeName}String = \`${def === null || def === void 0 ? void 0 : def.typeDefinition}\`;`)
315
+ .join('\n\n');
316
+ // Generate union type
317
+ const pageUnionTypes = pageTypeDefinitions.map((def) => def === null || def === void 0 ? void 0 : def.typeName).join(' | ');
325
318
  return `
326
- // 页面数据类型定义
327
- ${pageTypes}
319
+ // Page data type definitions
320
+ ${typeDefinitions}
321
+
322
+ // String versions of type definitions
323
+ ${typeStrings}
328
324
 
329
- // 所有页面数据类型的联合类型
325
+ // Union type of all page data types
330
326
  export type PageDataUnion = ${pageUnionTypes || 'never'};`;
331
327
  }
332
328
  export default generateWrapperCode;
@@ -17,13 +17,9 @@ import path, { basename, dirname } from 'path';
17
17
  import { pipeline } from 'stream/promises';
18
18
  import { joinURL } from 'ufo';
19
19
  import * as yaml from 'yaml';
20
- import { PAGES_KIT_BLOCK_STUDIO_DID } from '../constants';
20
+ import { PAGES_KIT_BLOCK_STUDIO_DID, METADATA_FILE_NAME, PREVIEW_IMAGE_DIR, NANOID_LENGTH, DEFAULT_BLOCK_ENTRY_FILES_PATTERN, } from '../constants';
21
+ export * from '../constants';
21
22
  export const logger = console;
22
- export const libDir = 'lib';
23
- export const METADATA_FILE_NAME = '@metadata.json';
24
- export const PREVIEW_IMAGE_DIR = '@preview-images';
25
- export const NANOID_LENGTH = 16;
26
- const DEFAULT_BLOCK_ENTRY_FILES_PATTERN = 'src/**/index.{ts,tsx,html}';
27
23
  if (!process.env.BLOCK_ENTRY_FILES_PATTERN) {
28
24
  process.env.BLOCK_ENTRY_FILES_PATTERN = DEFAULT_BLOCK_ENTRY_FILES_PATTERN;
29
25
  }
@@ -176,3 +172,11 @@ export const getBlockCode = (filePath) => {
176
172
  }
177
173
  return fs.readFileSync(codeFile, 'utf8');
178
174
  };
175
+ export const safeParse = (text) => {
176
+ try {
177
+ return JSON.parse(text);
178
+ }
179
+ catch (_a) {
180
+ return null;
181
+ }
182
+ };
@@ -4,3 +4,10 @@ export declare const PAGES_KIT_BLOCK_STUDIO_DID = "z2qa7rr3eUyVnWp2PCxEVARuUfLFh
4
4
  export declare const PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE = "page";
5
5
  export declare const MEDIA_KIT_DID = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
6
6
  export declare const MEDIA_KIT_RESOURCE_TYPE = "imgpack";
7
+ export declare const NEW_BLOCK_TEMPLATE_PATH: string;
8
+ export declare const NEW_BLOCK_TEMPLATE_METADATA_PATH: string;
9
+ export declare const libDir = "lib";
10
+ export declare const METADATA_FILE_NAME = "@metadata.json";
11
+ export declare const PREVIEW_IMAGE_DIR = "@preview-images";
12
+ export declare const NANOID_LENGTH = 16;
13
+ export declare const DEFAULT_BLOCK_ENTRY_FILES_PATTERN = "src/**/index.{ts,tsx,html}";