@content-collections/core 0.4.0 → 0.5.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 +20 -28
- package/dist/index.d.ts +9 -1
- package/dist/index.js +41 -17
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -22,35 +22,16 @@ Transform your content into type-safe data collections. Eliminate the need for m
|
|
|
22
22
|
|
|
23
23
|
## Installation
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
Content Collections offers a variety of adapters that seamlessly integrate with popular web frameworks. The installation process depends on the chosen adapter:
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```json
|
|
34
|
-
{
|
|
35
|
-
"compilerOptions": {
|
|
36
|
-
// ...
|
|
37
|
-
"paths": {
|
|
38
|
-
"content-collections": ["./.content-collections/generated"]
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
```
|
|
27
|
+
- [Next.js](https://www.content-collections.dev/docs/quickstart/next)
|
|
28
|
+
- [Qwik](https://www.content-collections.dev/docs/quickstart/qwik)
|
|
29
|
+
- [Remix (Vite)](https://www.content-collections.dev/docs/quickstart/remix-vite)
|
|
30
|
+
- [Solid Start](https://www.content-collections.dev/docs/quickstart/solid)
|
|
31
|
+
- [Svelte Kit](https://www.content-collections.dev/docs/quickstart/svelte-kit)
|
|
32
|
+
- [Vite](https://www.content-collections.dev/docs/quickstart/vite)
|
|
43
33
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
```json
|
|
47
|
-
{
|
|
48
|
-
"scripts": {
|
|
49
|
-
"dev": "concurrently 'content-collections watch' 'build-scripts dev'",
|
|
50
|
-
"build": "content-collections build && build-scripts build"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
```
|
|
34
|
+
If your framework is not listed, you can still use Content Collections by using the [CLI](https://www.content-collections.dev/docs/quickstart/cli). Please open a ticket if you want to see your framework listed.
|
|
54
35
|
|
|
55
36
|
## Usage
|
|
56
37
|
|
|
@@ -108,7 +89,14 @@ Transform your content into type-safe data collections. Eliminate the need for m
|
|
|
108
89
|
}
|
|
109
90
|
```
|
|
110
91
|
|
|
111
|
-
|
|
92
|
+
Please note that the example above shows only the very basics of Content Collections and it does not cover content transformation.
|
|
93
|
+
Content Collections does not transform content like markdown or mdx by default.
|
|
94
|
+
But it has packages which can do that for you:
|
|
95
|
+
|
|
96
|
+
- [@content-collections/markdown](https://www.content-collections.dev/docs/samples/markdown)
|
|
97
|
+
- [@content-collections/mdx](https://www.content-collections.dev/docs/samples/mdx)
|
|
98
|
+
|
|
99
|
+
If you want to see more examples and use cases, please refer to the [documentation](https://content-collections.dev/docs).
|
|
112
100
|
|
|
113
101
|
## Sponsors
|
|
114
102
|
|
|
@@ -119,6 +107,10 @@ For a more detailed guide, please refer to the [documentation](https://content-c
|
|
|
119
107
|
<img alt="supastarter" src="./assets/sponsors/supastarter/light.svg" height="64">
|
|
120
108
|
</picture>
|
|
121
109
|
</a>
|
|
110
|
+
|
|
111
|
+
<a href="https://cloudogu.com">
|
|
112
|
+
<img src="./assets/sponsors/cloudogu.png" alt="Cloudogu GmbH" height="64">
|
|
113
|
+
</a>
|
|
122
114
|
|
|
123
115
|
### [Become a sponsor](https://github.com/sponsors/sdorra)
|
|
124
116
|
|
package/dist/index.d.ts
CHANGED
|
@@ -52,6 +52,10 @@ type Schema<TParser extends Parser | undefined, TShape extends ZodRawShape> = z$
|
|
|
52
52
|
type Context = {
|
|
53
53
|
documents<TCollection extends AnyCollection>(collection: TCollection): Array<Schema<TCollection["parser"], TCollection["schema"]>>;
|
|
54
54
|
cache: CacheFn;
|
|
55
|
+
collection: {
|
|
56
|
+
name: string;
|
|
57
|
+
directory: string;
|
|
58
|
+
};
|
|
55
59
|
};
|
|
56
60
|
type Z = typeof z$1;
|
|
57
61
|
type CollectionRequest<TName extends string, TShape extends ZodRawShape, TParser, TSchema, TTransformResult, TDocument> = {
|
|
@@ -62,6 +66,7 @@ type CollectionRequest<TName extends string, TShape extends ZodRawShape, TParser
|
|
|
62
66
|
transform?: (data: TSchema, context: Context) => TTransformResult;
|
|
63
67
|
directory: string;
|
|
64
68
|
include: string | string[];
|
|
69
|
+
exclude?: string | string[];
|
|
65
70
|
onSuccess?: (documents: Array<TDocument>) => void | Promise<void>;
|
|
66
71
|
};
|
|
67
72
|
type Collection<TName extends string, TShape extends ZodRawShape, TParser extends Parser, TSchema, TTransformResult, TDocument> = Omit<CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>, "schema"> & {
|
|
@@ -86,6 +91,9 @@ type AnyConfiguration = Configuration<Array<AnyCollection>>;
|
|
|
86
91
|
declare function defineConfig<TConfig extends AnyConfiguration>(config: TConfig): TConfig;
|
|
87
92
|
|
|
88
93
|
type Modification = "create" | "update" | "delete";
|
|
94
|
+
type Document = {
|
|
95
|
+
_meta: Meta;
|
|
96
|
+
};
|
|
89
97
|
type CollectionFile = {
|
|
90
98
|
data: {
|
|
91
99
|
content?: string;
|
|
@@ -192,4 +200,4 @@ declare function createBuilder(configurationPath: string, options?: Options): Pr
|
|
|
192
200
|
}>;
|
|
193
201
|
type Builder = Awaited<ReturnType<typeof createBuilder>>;
|
|
194
202
|
|
|
195
|
-
export { AnyCollection, AnyConfiguration, Builder, BuilderEvents, CollectError, Collection, CollectionRequest, Configuration, ConfigurationError, Context, GetTypeByName, Meta, Modification, Schema, TransformError, createBuilder, defineCollection, defineConfig };
|
|
203
|
+
export { AnyCollection, AnyConfiguration, Builder, BuilderEvents, CollectError, Collection, CollectionRequest, Configuration, ConfigurationError, Context, Document, GetTypeByName, Meta, Modification, Schema, TransformError, createBuilder, defineCollection, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -45,7 +45,7 @@ import path from "path";
|
|
|
45
45
|
// package.json
|
|
46
46
|
var package_default = {
|
|
47
47
|
name: "@content-collections/core",
|
|
48
|
-
version: "0.
|
|
48
|
+
version: "0.5.0",
|
|
49
49
|
type: "module",
|
|
50
50
|
main: "dist/index.js",
|
|
51
51
|
types: "./dist/index.d.ts",
|
|
@@ -68,25 +68,25 @@ var package_default = {
|
|
|
68
68
|
postpack: "rm -f ./README.md"
|
|
69
69
|
},
|
|
70
70
|
devDependencies: {
|
|
71
|
-
"@types/micromatch": "^4.0.
|
|
71
|
+
"@types/micromatch": "^4.0.7",
|
|
72
72
|
"@types/node": "^20.9.0",
|
|
73
73
|
"@types/pluralize": "^0.0.33",
|
|
74
|
-
"@vitest/coverage-v8": "^1.
|
|
74
|
+
"@vitest/coverage-v8": "^1.5.0",
|
|
75
75
|
tsup: "^7.2.0",
|
|
76
76
|
tsx: "^4.1.1",
|
|
77
|
-
typescript: "^5.
|
|
78
|
-
vitest: "^1.
|
|
77
|
+
typescript: "^5.4.5",
|
|
78
|
+
vitest: "^1.5.0"
|
|
79
79
|
},
|
|
80
80
|
dependencies: {
|
|
81
|
-
"@parcel/watcher": "^2.
|
|
81
|
+
"@parcel/watcher": "^2.4.1",
|
|
82
82
|
camelcase: "^8.0.0",
|
|
83
|
-
esbuild: "^0.
|
|
83
|
+
esbuild: "^0.20.2",
|
|
84
84
|
"fast-glob": "^3.3.2",
|
|
85
85
|
"gray-matter": "^4.0.3",
|
|
86
86
|
micromatch: "^4.0.5",
|
|
87
87
|
pluralize: "^8.0.0",
|
|
88
|
-
yaml: "^2.
|
|
89
|
-
zod: "^3.22.
|
|
88
|
+
yaml: "^2.4.1",
|
|
89
|
+
zod: "^3.22.5"
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
92
|
|
|
@@ -245,12 +245,23 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
245
245
|
return null;
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
|
+
function createIgnorePattern(collection) {
|
|
249
|
+
if (collection.exclude) {
|
|
250
|
+
if (Array.isArray(collection.exclude)) {
|
|
251
|
+
return collection.exclude;
|
|
252
|
+
} else {
|
|
253
|
+
return [collection.exclude];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return void 0;
|
|
257
|
+
}
|
|
248
258
|
async function resolveCollection(collection) {
|
|
249
259
|
const collectionDirectory = path2.join(baseDirectory, collection.directory);
|
|
250
260
|
const filePaths = await fg(collection.include, {
|
|
251
261
|
cwd: collectionDirectory,
|
|
252
262
|
onlyFiles: true,
|
|
253
|
-
absolute: false
|
|
263
|
+
absolute: false,
|
|
264
|
+
ignore: createIgnorePattern(collection)
|
|
254
265
|
});
|
|
255
266
|
const promises = filePaths.map(
|
|
256
267
|
(filePath) => collectFile(collection, filePath)
|
|
@@ -315,11 +326,18 @@ async function createJavaScriptFile(directory, configuration) {
|
|
|
315
326
|
content += "export { " + collections.join(", ") + " };\n";
|
|
316
327
|
await fs2.writeFile(path3.join(directory, "index.js"), content, "utf-8");
|
|
317
328
|
}
|
|
329
|
+
function createImportPath(directory, target) {
|
|
330
|
+
let importPath = path3.posix.join(...path3.relative(directory, target).split(path3.sep));
|
|
331
|
+
if (!importPath.startsWith(".")) {
|
|
332
|
+
importPath = "./" + importPath;
|
|
333
|
+
}
|
|
334
|
+
return importPath;
|
|
335
|
+
}
|
|
318
336
|
async function createTypeDefinitionFile(directory, configuration) {
|
|
319
337
|
if (!configuration.generateTypes) {
|
|
320
338
|
return;
|
|
321
339
|
}
|
|
322
|
-
const importPath =
|
|
340
|
+
const importPath = createImportPath(directory, configuration.path);
|
|
323
341
|
let content = `import configuration from "${importPath}";
|
|
324
342
|
import { GetTypeByName } from "@content-collections/core";
|
|
325
343
|
`;
|
|
@@ -431,18 +449,22 @@ function createTransformer(emitter, cacheManager) {
|
|
|
431
449
|
documents: (await Promise.all(promises)).filter(isDefined)
|
|
432
450
|
};
|
|
433
451
|
}
|
|
434
|
-
function createContext(collections, cache) {
|
|
452
|
+
function createContext(collections, collection, cache) {
|
|
435
453
|
return {
|
|
436
|
-
documents: (
|
|
437
|
-
const resolved = collections.find((c) => c.name ===
|
|
454
|
+
documents: (collection2) => {
|
|
455
|
+
const resolved = collections.find((c) => c.name === collection2.name);
|
|
438
456
|
if (!resolved) {
|
|
439
457
|
throw new TransformError(
|
|
440
458
|
"Configuration",
|
|
441
|
-
`Collection ${
|
|
459
|
+
`Collection ${collection2.name} not found, do you have registered it in your configuration?`
|
|
442
460
|
);
|
|
443
461
|
}
|
|
444
462
|
return resolved.documents.map((doc) => doc.document);
|
|
445
463
|
},
|
|
464
|
+
collection: {
|
|
465
|
+
name: collection.name,
|
|
466
|
+
directory: collection.directory
|
|
467
|
+
},
|
|
446
468
|
cache: cache.cacheFn
|
|
447
469
|
};
|
|
448
470
|
}
|
|
@@ -454,7 +476,7 @@ function createTransformer(emitter, cacheManager) {
|
|
|
454
476
|
collection.name,
|
|
455
477
|
doc.document._meta.path
|
|
456
478
|
);
|
|
457
|
-
const context = createContext(collections, cache);
|
|
479
|
+
const context = createContext(collections, collection, cache);
|
|
458
480
|
try {
|
|
459
481
|
const document = await collection.transform(doc.document, context);
|
|
460
482
|
docs.push({
|
|
@@ -540,7 +562,9 @@ function createSynchronizer(readCollectionFile, collections, baseDirectory = "."
|
|
|
540
562
|
relativePath
|
|
541
563
|
};
|
|
542
564
|
}).filter(({ collection, relativePath }) => {
|
|
543
|
-
return micromatch.isMatch(relativePath, collection.include
|
|
565
|
+
return micromatch.isMatch(relativePath, collection.include, {
|
|
566
|
+
ignore: collection.exclude
|
|
567
|
+
});
|
|
544
568
|
});
|
|
545
569
|
}
|
|
546
570
|
function deleted(filePath) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@content-collections/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -16,25 +16,25 @@
|
|
|
16
16
|
"README.md"
|
|
17
17
|
],
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@types/micromatch": "^4.0.
|
|
19
|
+
"@types/micromatch": "^4.0.7",
|
|
20
20
|
"@types/node": "^20.9.0",
|
|
21
21
|
"@types/pluralize": "^0.0.33",
|
|
22
|
-
"@vitest/coverage-v8": "^1.
|
|
22
|
+
"@vitest/coverage-v8": "^1.5.0",
|
|
23
23
|
"tsup": "^7.2.0",
|
|
24
24
|
"tsx": "^4.1.1",
|
|
25
|
-
"typescript": "^5.
|
|
26
|
-
"vitest": "^1.
|
|
25
|
+
"typescript": "^5.4.5",
|
|
26
|
+
"vitest": "^1.5.0"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@parcel/watcher": "^2.
|
|
29
|
+
"@parcel/watcher": "^2.4.1",
|
|
30
30
|
"camelcase": "^8.0.0",
|
|
31
|
-
"esbuild": "^0.
|
|
31
|
+
"esbuild": "^0.20.2",
|
|
32
32
|
"fast-glob": "^3.3.2",
|
|
33
33
|
"gray-matter": "^4.0.3",
|
|
34
34
|
"micromatch": "^4.0.5",
|
|
35
35
|
"pluralize": "^8.0.0",
|
|
36
|
-
"yaml": "^2.
|
|
37
|
-
"zod": "^3.22.
|
|
36
|
+
"yaml": "^2.4.1",
|
|
37
|
+
"zod": "^3.22.5"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup src/index.ts --format esm --dts -d dist",
|