@acdh-oeaw/keystatic-lib 0.0.0 → 0.1.1

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/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import * as _keystatic_core_reader_github from '@keystatic/core/reader/github';
1
+ import * as url from 'url';
2
+ import * as _keystatic_core from '@keystatic/core';
2
3
  import { ComponentSchema, Collection, Singleton, Config } from '@keystatic/core';
4
+ import * as _keystatic_core_reader_github from '@keystatic/core/reader/github';
3
5
  import { ContentComponent } from '@keystatic/core/content-components';
6
+ import { MDXModule } from 'mdx/types';
4
7
 
5
8
  /**
6
9
  * Provide supported locales by augmenting the interface:
@@ -30,7 +33,7 @@ declare function createContentFieldOptions<TPath extends `/${string}/`>(assetPat
30
33
  transformFilename(originalFilename: string): string;
31
34
  };
32
35
  };
33
- declare function createComponent<TPath extends `/${string}/`, TComponent extends ContentComponent>(createComponentFactory: (assetPath: TPath, locale: Locale) => TComponent): (assetPath: TPath, locale: Locale) => TComponent;
36
+ declare function createComponent<TPath extends `/${string}/`, TLocale extends Locale, TComponent extends ContentComponent>(createComponentFactory: (assetPath: TPath, locale: TLocale) => TComponent): (assetPath: TPath, locale: TLocale) => TComponent;
34
37
  declare function createCollectionPaths<TPath extends `/${string}/`>(path: TPath, locale: Locale): {
35
38
  readonly assetPath: `/content/${string}${TPath}`;
36
39
  readonly contentPath: `./content/${string}${TPath}*/`;
@@ -39,15 +42,43 @@ declare function createSingletonPaths<TPath extends `/${string}/`>(path: TPath,
39
42
  readonly assetPath: `/content/${string}${TPath}`;
40
43
  readonly contentPath: `./content/${string}${TPath}`;
41
44
  };
42
- declare function createCollection<TPath extends `/${string}/`, TSchema extends Record<string, ComponentSchema>, TSlugField extends string>(path: TPath, createLocalisedCollectionFactory: (paths: ReturnType<typeof createCollectionPaths<TPath>>, locale: Locale) => Collection<TSchema, TSlugField>): (locale: Locale) => Collection<TSchema, TSlugField>;
43
- declare function createSingleton<TPath extends `/${string}/`, Schema extends Record<string, ComponentSchema>>(path: TPath, createLocalisedSingletonFactory: (paths: ReturnType<typeof createSingletonPaths<TPath>>, locale: Locale) => Singleton<Schema>): (locale: Locale) => Singleton<Schema>;
45
+ declare function createCollection<TPath extends `/${string}/`, TLocale extends Locale, TSchema extends Record<string, ComponentSchema>, TSlugField extends string>(path: TPath, createLocalisedCollectionFactory: (paths: ReturnType<typeof createCollectionPaths<TPath>>, locale: TLocale) => Collection<TSchema, TSlugField>): (locale: TLocale) => Collection<TSchema, TSlugField>;
46
+ declare function createSingleton<TPath extends `/${string}/`, TLocale extends Locale, TSchema extends Record<string, ComponentSchema>>(path: TPath, createLocalisedSingletonFactory: (paths: ReturnType<typeof createSingletonPaths<TPath>>, locale: TLocale) => Singleton<TSchema>): (locale: TLocale) => Singleton<TSchema>;
44
47
  declare function withI18nPrefix<TLabel extends string, TLocale extends Locale>(label: TLabel, locale: TLocale): `${TLocale}:${TLabel}`;
45
- type WithoutI18nPrefix<T extends string> = T extends `${Locale}:${infer U}` ? U : T;
48
+ type WithoutI18nPrefix<T extends string, TLocale extends Locale> = T extends `${TLocale}:${infer U}` ? U : T;
46
49
  declare function createReader<TCollections extends Record<string, Collection<Record<string, ComponentSchema>, string>>, TSingletons extends Record<string, Singleton<Record<string, ComponentSchema>>>>(config: Config<TCollections, TSingletons>, getGitHubReaderConfig?: () => {
47
50
  repo: `${string}/${string}`;
48
51
  pathPrefix?: string;
49
52
  ref?: string;
50
53
  token?: string;
51
54
  } | null): _keystatic_core_reader_github.Reader<TCollections, TSingletons>;
55
+ declare function createReaders<TCollections extends Record<string, Collection<Record<string, ComponentSchema>, string>>, TSingletons extends Record<string, Singleton<Record<string, ComponentSchema>>>, TLocale extends Locale, TMdxContent extends MDXModule>(config: Config<TCollections, TSingletons>, getMdxContent: (code: string, locale: TLocale, baseUrl: URL) => Promise<TMdxContent>): {
56
+ createCollectionResource: <TKeys extends WithoutI18nPrefix<keyof TCollections & string, TLocale>>(name: TKeys, locale: TLocale) => {
57
+ all: () => Promise<{
58
+ collection: TKeys;
59
+ id: string;
60
+ data: TCollections[`${TLocale}:${TKeys}`]["schema"] extends infer T extends Record<string, ComponentSchema> ? { [Key in keyof T]: TCollections[`${TLocale}:${TKeys}`]["slugField"] extends infer T_1 ? T_1 extends TCollections[`${TLocale}:${TKeys}`]["slugField"] ? T_1 extends Key ? TCollections[`${TLocale}:${TKeys}`]["schema"][Key] extends _keystatic_core.SlugFormField<any, any, any, infer SlugSerializedValue> ? SlugSerializedValue : _keystatic_core.ValueForReadingDeep<TCollections[`${TLocale}:${TKeys}`]["schema"][Key]> : _keystatic_core.ValueForReadingDeep<TCollections[`${TLocale}:${TKeys}`]["schema"][Key]> : never : never; } : never;
61
+ compile(code: string): Promise<TMdxContent>;
62
+ }[]>;
63
+ baseUrl: (id: string) => url.URL;
64
+ compile: (id: string, code: string) => Promise<TMdxContent>;
65
+ list: () => Promise<string[]>;
66
+ read: (id: string) => Promise<{
67
+ collection: TKeys;
68
+ id: string;
69
+ data: TCollections[`${TLocale}:${TKeys}`]["schema"] extends infer T extends Record<string, ComponentSchema> ? { [Key in keyof T]: TCollections[`${TLocale}:${TKeys}`]["slugField"] extends infer T_1 ? T_1 extends TCollections[`${TLocale}:${TKeys}`]["slugField"] ? T_1 extends Key ? TCollections[`${TLocale}:${TKeys}`]["schema"][Key] extends _keystatic_core.SlugFormField<any, any, any, infer SlugSerializedValue> ? SlugSerializedValue : _keystatic_core.ValueForReadingDeep<TCollections[`${TLocale}:${TKeys}`]["schema"][Key]> : _keystatic_core.ValueForReadingDeep<TCollections[`${TLocale}:${TKeys}`]["schema"][Key]> : never : never; } : never;
70
+ compile(code: string): Promise<TMdxContent>;
71
+ }>;
72
+ };
73
+ createSingletonResource: <TKeys extends WithoutI18nPrefix<keyof TSingletons & string, TLocale>>(name: TKeys, locale: TLocale) => {
74
+ baseUrl: () => url.URL;
75
+ compile: (code: string) => Promise<TMdxContent>;
76
+ read: () => Promise<{
77
+ singleton: TKeys;
78
+ data: TSingletons[`${TLocale}:${TKeys}`]["schema"] extends infer T extends Record<string, ComponentSchema> ? { readonly [Key in keyof T]: _keystatic_core.ValueForReadingDeep<TSingletons[`${TLocale}:${TKeys}`]["schema"][Key]>; } : never;
79
+ compile: (code: string) => Promise<TMdxContent>;
80
+ }>;
81
+ };
82
+ };
52
83
 
53
- export { type KeystaticConfig, type WithoutI18nPrefix, createAssetOptions, createCollection, createCollectionPaths, createComponent, createContentFieldOptions, createLabel, createReader, createSingleton, createSingletonPaths, withI18nPrefix };
84
+ export { type KeystaticConfig, type WithoutI18nPrefix, createAssetOptions, createCollection, createCollectionPaths, createComponent, createContentFieldOptions, createLabel, createReader, createReaders, createSingleton, createSingletonPaths, withI18nPrefix };
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import assert from 'node:assert/strict';
2
+ import { join } from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
1
4
  import { createReader as createReader$1 } from '@keystatic/core/reader';
2
5
  import { createGitHubReader } from '@keystatic/core/reader/github';
3
6
  import slugify from '@sindresorhus/slugify';
@@ -62,7 +65,77 @@ function createReader(config, getGitHubReaderConfig) {
62
65
  }
63
66
  return createReader$1(process.cwd(), config);
64
67
  }
68
+ function createReaders(config, getMdxContent) {
69
+ function createCollectionResource(name, locale) {
70
+ const reader = createReader(config);
71
+ const collectionName = withI18nPrefix(name, locale);
72
+ const collectionReader = reader.collections[collectionName];
73
+ const collectionConfig = reader.config.collections?.[collectionName];
74
+ assert(collectionConfig?.path);
75
+ function baseUrl(id) {
76
+ return pathToFileURL(join(process.cwd(), collectionConfig.path.replace(/\*+/, id)));
77
+ }
78
+ async function compile(id, code) {
79
+ return getMdxContent(code, locale, baseUrl(id));
80
+ }
81
+ function list() {
82
+ return collectionReader.list();
83
+ }
84
+ async function read(id) {
85
+ const data = await collectionReader.readOrThrow(id, { resolveLinkedFiles: true });
86
+ return {
87
+ collection: name,
88
+ id,
89
+ data,
90
+ compile(code) {
91
+ return compile(id, code);
92
+ }
93
+ };
94
+ }
95
+ async function all() {
96
+ const ids = await list();
97
+ return Promise.all(ids.map(read));
98
+ }
99
+ return {
100
+ all,
101
+ baseUrl,
102
+ compile,
103
+ list,
104
+ read
105
+ };
106
+ }
107
+ function createSingletonResource(name, locale) {
108
+ const reader = createReader(config);
109
+ const i18nName = withI18nPrefix(name, locale);
110
+ const singletonReader = reader.singletons[i18nName];
111
+ const singletonConfig = reader.config.singletons?.[i18nName];
112
+ assert(singletonConfig?.path);
113
+ function baseUrl() {
114
+ return pathToFileURL(join(process.cwd(), singletonConfig.path));
115
+ }
116
+ async function compile(code) {
117
+ return getMdxContent(code, locale, baseUrl());
118
+ }
119
+ async function read() {
120
+ const data = await singletonReader.readOrThrow({ resolveLinkedFiles: true });
121
+ return {
122
+ singleton: name,
123
+ data,
124
+ compile
125
+ };
126
+ }
127
+ return {
128
+ baseUrl,
129
+ compile,
130
+ read
131
+ };
132
+ }
133
+ return {
134
+ createCollectionResource,
135
+ createSingletonResource
136
+ };
137
+ }
65
138
 
66
- export { createAssetOptions, createCollection, createCollectionPaths, createComponent, createContentFieldOptions, createLabel, createReader, createSingleton, createSingletonPaths, withI18nPrefix };
139
+ export { createAssetOptions, createCollection, createCollectionPaths, createComponent, createContentFieldOptions, createLabel, createReader, createReaders, createSingleton, createSingletonPaths, withI18nPrefix };
67
140
  //# sourceMappingURL=index.js.map
68
141
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["createComponent","createLocalReader"],"mappings":";;;;;AAmBO,SAAS,WAAA,CAAY,OAAe,MAAgB,EAAA;AAC1D,EAAO,OAAA,CAAA,EAAG,KAAK,CAAA,EAAA,EAAK,MAAM,CAAA,CAAA,CAAA,CAAA;AAC3B,CAAA;AAEO,SAAS,mBAAgD,IAAa,EAAA;AAC5E,EAAO,OAAA;AAAA,IACN,SAAA,EAAW,kBAAkB,IAA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;AACD,EAAO,OAAA,SAAS,0BAA0B,MAAgB,EAAA;AACzD,IAAM,MAAA,KAAA,GAAQ,qBAAsB,CAAA,IAAA,EAAM,MAAM,CAAA,CAAA;AAChD,IAAO,OAAA,gCAAA,CAAiC,OAAO,MAAM,CAAA,CAAA;AAAA,GACtD,CAAA;AACD,CAAA;AAEO,SAAS,eAAA,CAIf,MACA,+BAIC,EAAA;AACD,EAAO,OAAA,SAAS,yBAAyB,MAAgB,EAAA;AACxD,IAAM,MAAA,KAAA,GAAQ,oBAAqB,CAAA,IAAA,EAAM,MAAM,CAAA,CAAA;AAC/C,IAAO,OAAA,+BAAA,CAAgC,OAAO,MAAM,CAAA,CAAA;AAAA,GACrD,CAAA;AACD,CAAA;AAEO,SAAS,cAAA,CACf,OACA,MACC,EAAA;AACD,EAAO,OAAA,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,CAAA;AAC1B,CAAA;AAIO,SAAS,YAAA,CAIf,QACA,qBAMC,EAAA;AACD,EAAA,MAAM,eAAe,qBAAwB,IAAA,CAAA;AAE7C,EAAA,IAAI,gBAAgB,IAAM,EAAA;AACzB,IAAO,OAAA,kBAAA,CAAmB,QAAQ,YAAY,CAAA,CAAA;AAAA,GAC/C;AAEA,EAAA,OAAOC,cAAkB,CAAA,OAAA,CAAQ,GAAI,EAAA,EAAG,MAAM,CAAA,CAAA;AAC/C","file":"index.js","sourcesContent":["import type { Collection, ComponentSchema, Config, Singleton } from \"@keystatic/core\";\nimport type { ContentComponent } from \"@keystatic/core/content-components\";\nimport { createReader as createLocalReader } from \"@keystatic/core/reader\";\nimport { createGitHubReader } from \"@keystatic/core/reader/github\";\nimport slugify from \"@sindresorhus/slugify\";\n\n/**\n * Provide supported locales by augmenting the interface:\n *\n * ```ts\n * export interface KeystaticConfig {\n * locales: \"de\" | \"en\"\n * }\n * ```\n */\nexport interface KeystaticConfig {}\n\ntype Locale = KeystaticConfig extends { locales: string } ? KeystaticConfig[\"locales\"] : string;\n\nexport function createLabel(label: string, locale: Locale) {\n\treturn `${label} (${locale})`;\n}\n\nexport function createAssetOptions<TPath extends `/${string}/`>(path: TPath) {\n\treturn {\n\t\tdirectory: `./public/assets${path}` as const,\n\t\tpublicPath: `/assets${path}` as const,\n\t\ttransformFilename(originalFilename: string) {\n\t\t\treturn slugify(originalFilename, { preserveCharacters: [\".\"] });\n\t\t},\n\t};\n}\n\nexport function createContentFieldOptions<TPath extends `/${string}/`>(assetPath: TPath) {\n\tconst assetPaths = createAssetOptions(assetPath);\n\tconst headingLevels = [2, 3, 4, 5] as const;\n\n\treturn {\n\t\theading: headingLevels,\n\t\timage: assetPaths,\n\t};\n}\n\nexport function createComponent<TPath extends `/${string}/`, TComponent extends ContentComponent>(\n\tcreateComponentFactory: (assetPath: TPath, locale: Locale) => TComponent,\n) {\n\treturn function createComponent(assetPath: TPath, locale: Locale) {\n\t\treturn createComponentFactory(assetPath, locale);\n\t};\n}\n\nexport function createCollectionPaths<TPath extends `/${string}/`>(path: TPath, locale: Locale) {\n\treturn {\n\t\tassetPath: `/content/${locale}${path}`,\n\t\tcontentPath: `./content/${locale}${path}*/`,\n\t} as const;\n}\n\nexport function createSingletonPaths<TPath extends `/${string}/`>(path: TPath, locale: Locale) {\n\treturn {\n\t\tassetPath: `/content/${locale}${path}`,\n\t\tcontentPath: `./content/${locale}${path}`,\n\t} as const;\n}\n\nexport function createCollection<\n\tTPath extends `/${string}/`,\n\tTSchema extends Record<string, ComponentSchema>,\n\tTSlugField extends string,\n>(\n\tpath: TPath,\n\tcreateLocalisedCollectionFactory: (\n\t\tpaths: ReturnType<typeof createCollectionPaths<TPath>>,\n\t\tlocale: Locale,\n\t) => Collection<TSchema, TSlugField>,\n) {\n\treturn function createLocalisedCollection(locale: Locale) {\n\t\tconst paths = createCollectionPaths(path, locale);\n\t\treturn createLocalisedCollectionFactory(paths, locale);\n\t};\n}\n\nexport function createSingleton<\n\tTPath extends `/${string}/`,\n\tSchema extends Record<string, ComponentSchema>,\n>(\n\tpath: TPath,\n\tcreateLocalisedSingletonFactory: (\n\t\tpaths: ReturnType<typeof createSingletonPaths<TPath>>,\n\t\tlocale: Locale,\n\t) => Singleton<Schema>,\n) {\n\treturn function createLocalisedSingleton(locale: Locale) {\n\t\tconst paths = createSingletonPaths(path, locale);\n\t\treturn createLocalisedSingletonFactory(paths, locale);\n\t};\n}\n\nexport function withI18nPrefix<TLabel extends string, TLocale extends Locale>(\n\tlabel: TLabel,\n\tlocale: TLocale,\n) {\n\treturn `${locale}:${label}` as const;\n}\n\nexport type WithoutI18nPrefix<T extends string> = T extends `${Locale}:${infer U}` ? U : T;\n\nexport function createReader<\n\tTCollections extends Record<string, Collection<Record<string, ComponentSchema>, string>>,\n\tTSingletons extends Record<string, Singleton<Record<string, ComponentSchema>>>,\n>(\n\tconfig: Config<TCollections, TSingletons>,\n\tgetGitHubReaderConfig?: () => {\n\t\trepo: `${string}/${string}`;\n\t\tpathPrefix?: string;\n\t\tref?: string;\n\t\ttoken?: string;\n\t} | null,\n) {\n\tconst gitHubConfig = getGitHubReaderConfig?.();\n\n\tif (gitHubConfig != null) {\n\t\treturn createGitHubReader(config, gitHubConfig);\n\t}\n\n\treturn createLocalReader(process.cwd(), config);\n}\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["createComponent","createLocalReader"],"mappings":";;;;;;;;AAwBO,SAAS,WAAA,CAAY,OAAe,MAAgB,EAAA;AAC1D,EAAO,OAAA,CAAA,EAAG,KAAK,CAAA,EAAA,EAAK,MAAM,CAAA,CAAA,CAAA,CAAA;AAC3B,CAAA;AAEO,SAAS,mBAAgD,IAAa,EAAA;AAC5E,EAAO,OAAA;AAAA,IACN,SAAA,EAAW,kBAAkB,IAAI,CAAA,CAAA;AAAA,IACjC,UAAA,EAAY,UAAU,IAAI,CAAA,CAAA;AAAA,IAC1B,kBAAkB,gBAA0B,EAAA;AAC3C,MAAA,OAAO,QAAQ,gBAAkB,EAAA,EAAE,oBAAoB,CAAC,G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MAAA,eAAA,GAAkB,MAAO,CAAA,UAAA,CAAW,QAAQ,CAAA,CAAA;AAClD,IAAA,MAAM,eAAkB,GAAA,MAAA,CAAO,MAAO,CAAA,UAAA,GAAa,QAAQ,CAAA,CAAA;AAE3D,IAAA,MAAA,CAAO,iBAAiB,IAAI,CAAA,CAAA;AAE5B,IAAA,SAAS,OAAU,GAAA;AAClB,MAAA,OAAO,cAAc,IAAK,CAAA,OAAA,CAAQ,KAAO,EAAA,eAAA,CAAiB,IAAK,CAAC,CAAA,CAAA;AAAA,KACjE;AAEA,IAAA,eAAe,QAAQ,IAAc,EAAA;AACpC,MAAA,OAAO,aAAc,CAAA,IAAA,EAAM,MAAQ,EAAA,OAAA,EAAS,CAAA,CAAA;AAAA,KAC7C;AAEA,IAAA,eAAe,IAAO,GAAA;AACrB,MAAA,MAAM,OAAO,MAAM,eAAA,CAAgB,YAAY,EAAE,kBAAA,EAAoB,MAAM,CAAA,CAAA;AAE3E,MAAO,OAAA;AAAA,QACN,SAAW,EAAA,IAAA;AAAA,QACX,IAAA;AAAA,QACA,OAAA;AAAA,OACD,CAAA;AAAA,KACD;AAEA,IAAO,OAAA;AAAA,MACN,OAAA;AAAA,MACA,OAAA;AAAA,MACA,IAAA;AAAA,KACD,CAAA;AAAA,GACD;AAEA,EAAO,OAAA;AAAA,IACN,wBAAA;AAAA,IACA,uBAAA;AAAA,GACD,CAAA;AACD","file":"index.js","sourcesContent":["import assert from \"node:assert/strict\";\nimport { join } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\n\nimport type { Collection, ComponentSchema, Config, Singleton } from \"@keystatic/core\";\nimport type { ContentComponent } from \"@keystatic/core/content-components\";\nimport { createReader as createLocalReader } from \"@keystatic/core/reader\";\nimport { createGitHubReader } from \"@keystatic/core/reader/github\";\nimport slugify from \"@sindresorhus/slugify\";\nimport type { MDXModule } from \"mdx/types\";\n\n/**\n * Provide supported locales by augmenting the interface:\n *\n * ```ts\n * export interface KeystaticConfig {\n * locales: \"de\" | \"en\"\n * }\n * ```\n */\nexport interface KeystaticConfig {}\n\ntype Locale = KeystaticConfig extends { locales: string } ? KeystaticConfig[\"locales\"] : string;\n\nexport function createLabel(label: string, locale: Locale) {\n\treturn `${label} (${locale})`;\n}\n\nexport function createAssetOptions<TPath extends `/${string}/`>(path: TPath) {\n\treturn {\n\t\tdirectory: `./public/assets${path}` as const,\n\t\tpublicPath: `/assets${path}` as const,\n\t\ttransformFilename(originalFilename: string) {\n\t\t\treturn slugify(originalFilename, { preserveCharacters: [\".\"] });\n\t\t},\n\t};\n}\n\nexport function createContentFieldOptions<TPath extends `/${string}/`>(assetPath: TPath) {\n\tconst assetPaths = createAssetOptions(assetPath);\n\tconst headingLevels = [2, 3, 4, 5] as const;\n\n\treturn {\n\t\theading: headingLevels,\n\t\timage: assetPaths,\n\t};\n}\n\nexport function createComponent<\n\tTPath extends `/${string}/`,\n\tTLocale extends Locale,\n\tTComponent extends ContentComponent,\n>(createComponentFactory: (assetPath: TPath, locale: TLocale) => TComponent) {\n\treturn function createComponent(assetPath: TPath, locale: TLocale) {\n\t\treturn createComponentFactory(assetPath, locale);\n\t};\n}\n\nexport function createCollectionPaths<TPath extends `/${string}/`>(path: TPath, locale: Locale) {\n\treturn {\n\t\tassetPath: `/content/${locale}${path}`,\n\t\tcontentPath: `./content/${locale}${path}*/`,\n\t} as const;\n}\n\nexport function createSingletonPaths<TPath extends `/${string}/`>(path: TPath, locale: Locale) {\n\treturn {\n\t\tassetPath: `/content/${locale}${path}`,\n\t\tcontentPath: `./content/${locale}${path}`,\n\t} as const;\n}\n\nexport function createCollection<\n\tTPath extends `/${string}/`,\n\tTLocale extends Locale,\n\tTSchema extends Record<string, ComponentSchema>,\n\tTSlugField extends string,\n>(\n\tpath: TPath,\n\tcreateLocalisedCollectionFactory: (\n\t\tpaths: ReturnType<typeof createCollectionPaths<TPath>>,\n\t\tlocale: TLocale,\n\t) => Collection<TSchema, TSlugField>,\n) {\n\treturn function createLocalisedCollection(locale: TLocale) {\n\t\tconst paths = createCollectionPaths(path, locale);\n\t\treturn createLocalisedCollectionFactory(paths, locale);\n\t};\n}\n\nexport function createSingleton<\n\tTPath extends `/${string}/`,\n\tTLocale extends Locale,\n\tTSchema extends Record<string, ComponentSchema>,\n>(\n\tpath: TPath,\n\tcreateLocalisedSingletonFactory: (\n\t\tpaths: ReturnType<typeof createSingletonPaths<TPath>>,\n\t\tlocale: TLocale,\n\t) => Singleton<TSchema>,\n) {\n\treturn function createLocalisedSingleton(locale: TLocale) {\n\t\tconst paths = createSingletonPaths(path, locale);\n\t\treturn createLocalisedSingletonFactory(paths, locale);\n\t};\n}\n\nexport function withI18nPrefix<TLabel extends string, TLocale extends Locale>(\n\tlabel: TLabel,\n\tlocale: TLocale,\n) {\n\treturn `${locale}:${label}` as const;\n}\n\nexport type WithoutI18nPrefix<\n\tT extends string,\n\tTLocale extends Locale,\n> = T extends `${TLocale}:${infer U}` ? U : T;\n\nexport function createReader<\n\tTCollections extends Record<string, Collection<Record<string, ComponentSchema>, string>>,\n\tTSingletons extends Record<string, Singleton<Record<string, ComponentSchema>>>,\n>(\n\tconfig: Config<TCollections, TSingletons>,\n\tgetGitHubReaderConfig?: () => {\n\t\trepo: `${string}/${string}`;\n\t\tpathPrefix?: string;\n\t\tref?: string;\n\t\ttoken?: string;\n\t} | null,\n) {\n\tconst gitHubConfig = getGitHubReaderConfig?.();\n\n\tif (gitHubConfig != null) {\n\t\treturn createGitHubReader(config, gitHubConfig);\n\t}\n\n\treturn createLocalReader(process.cwd(), config);\n}\n\nexport function createReaders<\n\tTCollections extends Record<string, Collection<Record<string, ComponentSchema>, string>>,\n\tTSingletons extends Record<string, Singleton<Record<string, ComponentSchema>>>,\n\tTLocale extends Locale,\n\tTMdxContent extends MDXModule,\n>(\n\tconfig: Config<TCollections, TSingletons>,\n\tgetMdxContent: (code: string, locale: TLocale, baseUrl: URL) => Promise<TMdxContent>,\n) {\n\tfunction createCollectionResource<\n\t\tTKeys extends WithoutI18nPrefix<keyof TCollections & string, TLocale>,\n\t>(name: TKeys, locale: TLocale) {\n\t\tconst reader = createReader(config);\n\t\tconst collectionName = withI18nPrefix(name, locale);\n\n\t\tconst collectionReader = reader.collections[collectionName];\n\t\tconst collectionConfig = reader.config.collections?.[collectionName];\n\n\t\tassert(collectionConfig?.path);\n\n\t\tfunction baseUrl(id: string) {\n\t\t\treturn pathToFileURL(join(process.cwd(), collectionConfig!.path!.replace(/\\*+/, id)));\n\t\t}\n\n\t\tasync function compile(id: string, code: string) {\n\t\t\treturn getMdxContent(code, locale, baseUrl(id));\n\t\t}\n\n\t\tfunction list() {\n\t\t\treturn collectionReader.list();\n\t\t}\n\n\t\tasync function read(id: string) {\n\t\t\tconst data = await collectionReader.readOrThrow(id, { resolveLinkedFiles: true });\n\n\t\t\treturn {\n\t\t\t\tcollection: name,\n\t\t\t\tid,\n\t\t\t\tdata,\n\t\t\t\tcompile(code: string) {\n\t\t\t\t\treturn compile(id, code);\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tasync function all() {\n\t\t\tconst ids = await list();\n\n\t\t\treturn Promise.all(ids.map(read));\n\t\t}\n\n\t\treturn {\n\t\t\tall,\n\t\t\tbaseUrl,\n\t\t\tcompile,\n\t\t\tlist,\n\t\t\tread,\n\t\t};\n\t}\n\n\tfunction createSingletonResource<\n\t\tTKeys extends WithoutI18nPrefix<keyof TSingletons & string, TLocale>,\n\t>(name: TKeys, locale: TLocale) {\n\t\tconst reader = createReader(config);\n\t\tconst i18nName = withI18nPrefix(name, locale);\n\n\t\tconst singletonReader = reader.singletons[i18nName];\n\t\tconst singletonConfig = reader.config.singletons?.[i18nName];\n\n\t\tassert(singletonConfig?.path);\n\n\t\tfunction baseUrl() {\n\t\t\treturn pathToFileURL(join(process.cwd(), singletonConfig!.path!));\n\t\t}\n\n\t\tasync function compile(code: string) {\n\t\t\treturn getMdxContent(code, locale, baseUrl());\n\t\t}\n\n\t\tasync function read() {\n\t\t\tconst data = await singletonReader.readOrThrow({ resolveLinkedFiles: true });\n\n\t\t\treturn {\n\t\t\t\tsingleton: name,\n\t\t\t\tdata,\n\t\t\t\tcompile,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tbaseUrl,\n\t\t\tcompile,\n\t\t\tread,\n\t\t};\n\t}\n\n\treturn {\n\t\tcreateCollectionResource,\n\t\tcreateSingletonResource,\n\t};\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acdh-oeaw/keystatic-lib",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,7 +16,8 @@
16
16
  "pnpm": "9.x"
17
17
  },
18
18
  "dependencies": {
19
- "@sindresorhus/slugify": "^2.2.1"
19
+ "@sindresorhus/slugify": "^2.2.1",
20
+ "@types/mdx": "^2.0.13"
20
21
  },
21
22
  "peerDependencies": {
22
23
  "@keystatic/core": "0.x"
@@ -42,8 +43,7 @@
42
43
  "simple-git-hooks": "^2.11.1",
43
44
  "tsup": "^8.2.4",
44
45
  "tsx": "^4.19.0",
45
- "typescript": "^5.6.2",
46
- "uvu": "^0.5.6"
46
+ "typescript": "^5.6.2"
47
47
  },
48
48
  "commitlint": {
49
49
  "extends": "@acdh-oeaw/commitlint-config"
@@ -62,7 +62,7 @@
62
62
  "pre-push": "pnpm run validate"
63
63
  },
64
64
  "scripts": {
65
- "build": "tsup",
65
+ "build": "tsup ./src/index.ts",
66
66
  "changeset:add": "changeset add",
67
67
  "changeset:status": "changeset status --verbose",
68
68
  "ci:release": "changeset publish",
@@ -72,7 +72,7 @@
72
72
  "lint:check": "eslint . --cache",
73
73
  "lint:fix": "pnpm run lint:check --fix",
74
74
  "setup": "is-ci || simple-git-hooks",
75
- "test": "NODE_OPTIONS=\"--import tsx\" uvu",
75
+ "test": "tsx --test --test-reporter spec",
76
76
  "types:check": "tsc --noEmit",
77
77
  "validate": "run-p format:check lint:check types:check test"
78
78
  }
package/readme.md CHANGED
@@ -16,7 +16,7 @@ provide the languages supported in your project via typescript module augmentati
16
16
  // ./types/keystatic.d.ts
17
17
 
18
18
  declare module "@acdh-oeaw/keystatic-lib" {
19
- export interface Config {
19
+ export interface KeystaticConfig {
20
20
  locales: "de" | "en";
21
21
  }
22
22
  }
@@ -119,3 +119,85 @@ export default config({
119
119
  },
120
120
  });
121
121
  ```
122
+
123
+ ### read and render entries
124
+
125
+ in astro:
126
+
127
+ ```astro
128
+ ---
129
+ // ./src/pages/[locale]/[id].astro
130
+
131
+ import { createCollectionResource } from "@acdh-ch/keystatic-lib";
132
+ import type { GetStaticPathsResult } from "astro";
133
+
134
+ import { locales } from "@/config/i18n.config";
135
+ import Layout from "@/layouts/page-layout.astro";
136
+
137
+ export async function getStaticPaths() {
138
+ return (
139
+ await Promise.all(
140
+ locales.map(async (locale) => {
141
+ const ids = await createCollectionResource("pages", locale).list();
142
+ return ids.map((id) => {
143
+ return { params: { id, locale } };
144
+ });
145
+ }),
146
+ )
147
+ ).flat() satisfies GetStaticPathsResult;
148
+ }
149
+
150
+ const { id, locale } = Astro.params;
151
+
152
+ const page = await createCollectionResource("pages", locale).read(id);
153
+ const { content, image, title } = page.data;
154
+ const { default: Content } = await page.compile(content);
155
+ ---
156
+
157
+ <Layout>
158
+ <main>
159
+ <Content />
160
+ </main>
161
+ </Layout>
162
+ ```
163
+
164
+ in next.js:
165
+
166
+ ```tsx
167
+ // ./app/[locale]/[id]/page.tsx
168
+
169
+ import { createCollectionResource } from "@acdh-ch/keystatic-lib";
170
+
171
+ import type { Locale } from "@/config/i18n.config";
172
+
173
+ interface PageProps {
174
+ params: {
175
+ id: string;
176
+ locale: Locale;
177
+ };
178
+ }
179
+
180
+ export const dynamicParams = false;
181
+
182
+ export async function generateStaticParams(props: PageProps) {
183
+ const { locale } = props.params;
184
+ const ids = await createCollectionResource("pages", locale).list();
185
+ return ids.map((id) => {
186
+ return { id };
187
+ });
188
+ }
189
+
190
+ export default async function Page(props: PageProps) {
191
+ const { id, locale } = props.params;
192
+
193
+ const page = await createCollectionResource("pages", locale).read(id);
194
+ const { title, image, content } = page.data;
195
+ const { default: Content } = await page.compile(content);
196
+
197
+ return (
198
+ <main>
199
+ <Content />
200
+ </main>
201
+ );
202
+ }
203
+ ```