@content-collections/core 0.7.3 → 0.8.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.
package/README.md CHANGED
@@ -17,7 +17,7 @@ Transform your content into type-safe data collections. Eliminate the need for m
17
17
  - **Simple to use**:
18
18
  No need to manually fetch and parse your content anymore. Just import it and start using Content Collections. It provides a simple API, allowing you to concentrate on building your app.
19
19
 
20
- - **Tansformation**:
20
+ - **Transformation**:
21
21
  Content Collections allows you to transform your content before it enters your app. You can use it to modify your content, join two collections or even fetch data from a server.
22
22
 
23
23
  ## Installation
package/dist/index.d.ts CHANGED
@@ -3,17 +3,34 @@ export * from 'zod';
3
3
 
4
4
  type CacheFn = <TInput, TOutput>(input: TInput, compute: (input: TInput) => Promise<TOutput> | TOutput) => Promise<TOutput>;
5
5
 
6
+ declare const importSymbol: unique symbol;
7
+ type Import<T> = {
8
+ [importSymbol]: true;
9
+ path: string;
10
+ name?: string;
11
+ };
12
+ type GetTypeOfImport<T> = T extends Import<infer U> ? U : never;
13
+ declare function createDefaultImport<T>(path: string): Import<T>;
14
+ declare function createNamedImport<T>(name: string, path: string): Import<T>;
15
+
6
16
  type Parsers = typeof parsers;
7
17
  type Parser = keyof typeof parsers;
8
18
  declare function parseYaml(content: string): any;
9
19
  declare function frontmatterParser(fileContent: string): {
10
20
  content: string;
11
21
  };
22
+ declare function frontmatterOnlyParser(fileContent: string): {
23
+ [key: string]: any;
24
+ };
12
25
  declare const parsers: {
13
26
  readonly frontmatter: {
14
27
  readonly hasContent: true;
15
28
  readonly parse: typeof frontmatterParser;
16
29
  };
30
+ readonly "frontmatter-only": {
31
+ readonly hasContent: false;
32
+ readonly parse: typeof frontmatterOnlyParser;
33
+ };
17
34
  readonly json: {
18
35
  readonly hasContent: false;
19
36
  readonly parse: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
@@ -86,7 +103,10 @@ type InvalidReturnType<TMessage extends string, TObject> = {
86
103
  [InvalidReturnTypeSymbol]: TMessage;
87
104
  object: TObject;
88
105
  };
89
- declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, TDocument> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
106
+ type ResolveImports<TTransformResult> = TTransformResult extends Import<any> ? GetTypeOfImport<TTransformResult> : TTransformResult extends Array<infer U> ? Array<ResolveImports<U>> : TTransformResult extends (...args: any[]) => any ? TTransformResult : TTransformResult extends object ? {
107
+ [K in keyof TTransformResult]: ResolveImports<TTransformResult[K]>;
108
+ } : TTransformResult;
109
+ declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, ResolveImports<TDocument>> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
90
110
  type Cache = "memory" | "file" | "none";
91
111
  type Configuration<TCollections extends Array<AnyCollection>> = {
92
112
  collections: TCollections;
@@ -261,4 +281,4 @@ declare function createBuilder(configurationPath: string, options?: Options, emi
261
281
  }>;
262
282
  type Builder = Awaited<ReturnType<typeof createBuilder>>;
263
283
 
264
- export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, defineCollection, defineConfig };
284
+ export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, createDefaultImport, createNamedImport, defineCollection, defineConfig };
package/dist/index.js CHANGED
@@ -111,8 +111,8 @@ import { parse, stringify } from "yaml";
111
111
  function parseYaml(content) {
112
112
  return parse(content.trim());
113
113
  }
114
- function frontmatterParser(fileContent) {
115
- const { data, content } = matter(fileContent, {
114
+ function frontmatter(fileContent) {
115
+ return matter(fileContent, {
116
116
  engines: {
117
117
  yaml: {
118
118
  parse: parseYaml,
@@ -120,16 +120,27 @@ function frontmatterParser(fileContent) {
120
120
  }
121
121
  }
122
122
  });
123
+ }
124
+ function frontmatterParser(fileContent) {
125
+ const { data, content } = frontmatter(fileContent);
123
126
  return {
124
127
  ...data,
125
128
  content: content.trim()
126
129
  };
127
130
  }
131
+ function frontmatterOnlyParser(fileContent) {
132
+ const { data } = frontmatter(fileContent);
133
+ return data;
134
+ }
128
135
  var parsers = {
129
136
  frontmatter: {
130
137
  hasContent: true,
131
138
  parse: frontmatterParser
132
139
  },
140
+ ["frontmatter-only"]: {
141
+ hasContent: false,
142
+ parse: frontmatterOnlyParser
143
+ },
133
144
  json: {
134
145
  hasContent: false,
135
146
  parse: JSON.parse
@@ -349,6 +360,27 @@ import { z as z2 } from "zod";
349
360
  // src/serializer.ts
350
361
  import serializeJs from "serialize-javascript";
351
362
  import z from "zod";
363
+
364
+ // src/import.ts
365
+ var importSymbol = Symbol("import");
366
+ function isImport(value) {
367
+ return value && value[importSymbol];
368
+ }
369
+ function createDefaultImport(path10) {
370
+ return {
371
+ [importSymbol]: true,
372
+ path: path10
373
+ };
374
+ }
375
+ function createNamedImport(name, path10) {
376
+ return {
377
+ [importSymbol]: true,
378
+ path: path10,
379
+ name
380
+ };
381
+ }
382
+
383
+ // src/serializer.ts
352
384
  var literalSchema = z.union([
353
385
  // json
354
386
  z.string(),
@@ -367,13 +399,39 @@ var schema = z.lazy(
367
399
  );
368
400
  var extension = "js";
369
401
  var serializableSchema = z.record(schema);
402
+ function createImport(imp, variableName) {
403
+ const variableDeclaration = imp.name ? `{ ${imp.name} as ${variableName} }` : variableName;
404
+ return `import ${variableDeclaration} from "${imp.path}";
405
+ `;
406
+ }
370
407
  function serialize(value) {
371
- const serializedValue = serializeJs(value, {
408
+ let serializedValue = "";
409
+ let counter = 0;
410
+ function handleImports(item) {
411
+ if (item instanceof Object) {
412
+ Object.entries(item).forEach(([key, value2]) => {
413
+ if (isImport(value2)) {
414
+ counter++;
415
+ const variableName = `__v_${counter}`;
416
+ serializedValue += createImport(value2, variableName);
417
+ item[key] = variableName;
418
+ } else if (value2 instanceof Object) {
419
+ handleImports(value2);
420
+ }
421
+ });
422
+ }
423
+ }
424
+ value.forEach(handleImports);
425
+ serializedValue += "\n";
426
+ const js = serializeJs(value, {
372
427
  space: 2,
373
428
  unsafe: true,
374
429
  ignoreFunction: true
430
+ }).replace(/"__v_(\d+)"/g, (_, index) => {
431
+ return `__v_${index}`;
375
432
  });
376
- return `export default ${serializedValue};`;
433
+ serializedValue += "export default " + js;
434
+ return serializedValue;
377
435
  }
378
436
 
379
437
  // src/transformer.ts
@@ -1194,6 +1252,8 @@ export {
1194
1252
  ConfigurationReloadError,
1195
1253
  TransformError,
1196
1254
  createBuilder,
1255
+ createDefaultImport,
1256
+ createNamedImport,
1197
1257
  defineCollection,
1198
1258
  defineConfig
1199
1259
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-collections/core",
3
- "version": "0.7.3",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -24,11 +24,11 @@
24
24
  "@types/picomatch": "^3.0.1",
25
25
  "@types/pluralize": "^0.0.33",
26
26
  "@types/serialize-javascript": "^5.0.4",
27
- "@vitest/coverage-v8": "^2.1.3",
27
+ "@vitest/coverage-v8": "^2.1.5",
28
28
  "tsup": "^8.2.4",
29
- "tsx": "^4.1.1",
29
+ "tsx": "^4.19.2",
30
30
  "typescript": "^5.5.4",
31
- "vitest": "^2.1.3"
31
+ "vitest": "^2.1.5"
32
32
  },
33
33
  "dependencies": {
34
34
  "@parcel/watcher": "^2.4.1",