@ibalzam/codejitsu-core 0.3.2 → 0.3.3

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.
@@ -4,28 +4,14 @@ export interface CollectionBlogConfig extends CommonBlogConfig {
4
4
  collectionName?: string;
5
5
  }
6
6
  /**
7
- * Astro Content Collections blog loader. Use this in Astro projects.
7
+ * Astro Content Collections blog loader. Use in any Astro project.
8
8
  *
9
- * Returns raw CollectionEntry objects (preserving `entry.data`, `entry.id`,
10
- * and the ability to call `render(entry)` from astro:content). Filtering and
11
- * sorting are applied:
12
- * - Drafts excluded (when `draftField` is set)
13
- * - Sorted newest first by `dateField`
14
- * - `getPublishedEntries()` further excludes future-dated entries
9
+ * Returns raw CollectionEntry objects with filtering applied (drafts excluded,
10
+ * sorted newest first by `dateField`). Preserves `entry.data`, `entry.id`,
11
+ * and the ability to call `render(entry)` for `<Content />`.
15
12
  *
16
- * The collection's actual entry type is `CollectionEntry<'<name>'>` from
17
- * `astro:content`. Pass it as the generic to get full typed `data`:
18
- *
19
- * ```ts
20
- * import type { CollectionEntry } from 'astro:content';
21
- * export const blog = createBlogFromCollection<CollectionEntry<'blog'>>({
22
- * collectionName: 'blog',
23
- * dateField: 'pubDate',
24
- * draftField: 'draft',
25
- * });
26
- * ```
27
- *
28
- * Dynamically imports `astro:content` at call time so the package stays
29
- * usable in non-Astro projects.
13
+ * Import only from inside Astro code (`src/lib/blog.ts`, page routes). Do not
14
+ * import from `astro.config.mjs` Astro CC isn't initialized at config time.
15
+ * Use `createBlog` (fs) for astro.config.
30
16
  */
31
17
  export declare function createBlogFromCollection<E extends BlogCollectionEntry = BlogCollectionEntry>(config?: CollectionBlogConfig): BlogCollectionAPI<E>;
@@ -1,3 +1,9 @@
1
+ // @ts-expect-error - 'astro:content' is a virtual module resolved by Astro at build time.
2
+ // Static import (not dynamic) so Vite/Astro processes the dependency correctly.
3
+ // This file is only safe to import from inside an Astro project. It lives at the
4
+ // `/blog/collection` subpath; sites that aren't Astro should import from `/blog`
5
+ // (which doesn't pull this in).
6
+ import { getCollection as astroGetCollection } from 'astro:content';
1
7
  import readingTime from 'reading-time';
2
8
  function getTodayUTC() {
3
9
  const now = new Date();
@@ -20,29 +26,15 @@ function asDate(value) {
20
26
  return null;
21
27
  }
22
28
  /**
23
- * Astro Content Collections blog loader. Use this in Astro projects.
29
+ * Astro Content Collections blog loader. Use in any Astro project.
24
30
  *
25
- * Returns raw CollectionEntry objects (preserving `entry.data`, `entry.id`,
26
- * and the ability to call `render(entry)` from astro:content). Filtering and
27
- * sorting are applied:
28
- * - Drafts excluded (when `draftField` is set)
29
- * - Sorted newest first by `dateField`
30
- * - `getPublishedEntries()` further excludes future-dated entries
31
+ * Returns raw CollectionEntry objects with filtering applied (drafts excluded,
32
+ * sorted newest first by `dateField`). Preserves `entry.data`, `entry.id`,
33
+ * and the ability to call `render(entry)` for `<Content />`.
31
34
  *
32
- * The collection's actual entry type is `CollectionEntry<'<name>'>` from
33
- * `astro:content`. Pass it as the generic to get full typed `data`:
34
- *
35
- * ```ts
36
- * import type { CollectionEntry } from 'astro:content';
37
- * export const blog = createBlogFromCollection<CollectionEntry<'blog'>>({
38
- * collectionName: 'blog',
39
- * dateField: 'pubDate',
40
- * draftField: 'draft',
41
- * });
42
- * ```
43
- *
44
- * Dynamically imports `astro:content` at call time so the package stays
45
- * usable in non-Astro projects.
35
+ * Import only from inside Astro code (`src/lib/blog.ts`, page routes). Do not
36
+ * import from `astro.config.mjs` Astro CC isn't initialized at config time.
37
+ * Use `createBlog` (fs) for astro.config.
46
38
  */
47
39
  export function createBlogFromCollection(config = {}) {
48
40
  const collectionName = config.collectionName ?? 'blog';
@@ -50,20 +42,8 @@ export function createBlogFromCollection(config = {}) {
50
42
  const categories = config.categories ?? [];
51
43
  const dateField = config.dateField ?? 'date';
52
44
  const draftField = config.draftField ?? null;
53
- async function getCollection() {
54
- let mod;
55
- try {
56
- // @ts-expect-error - 'astro:content' is a virtual module resolved by Astro at build time.
57
- mod = await import('astro:content');
58
- }
59
- catch (err) {
60
- throw new Error(`createBlogFromCollection() requires Astro and a configured content collection ` +
61
- `named '${collectionName}'. Original error: ${err instanceof Error ? err.message : String(err)}`);
62
- }
63
- return mod.getCollection(collectionName);
64
- }
65
45
  async function readAll() {
66
- const all = await getCollection();
46
+ const all = (await astroGetCollection(collectionName));
67
47
  const filtered = draftField ? all.filter((e) => !e.data[draftField]) : all;
68
48
  return filtered.sort((a, b) => {
69
49
  const da = asDate(a.data[dateField])?.valueOf() ?? 0;
@@ -1,3 +1,9 @@
1
+ // @ts-expect-error - 'astro:content' is a virtual module resolved by Astro at build time.
2
+ // Static import (not dynamic) so Vite/Astro processes the dependency correctly.
3
+ // This file is only safe to import from inside an Astro project. It lives at the
4
+ // `/blog/collection` subpath; sites that aren't Astro should import from `/blog`
5
+ // (which doesn't pull this in).
6
+ import { getCollection as astroGetCollection } from 'astro:content';
1
7
  import readingTime from 'reading-time';
2
8
  import type {
3
9
  BlogCategory,
@@ -33,29 +39,15 @@ function asDate(value: unknown): Date | null {
33
39
  }
34
40
 
35
41
  /**
36
- * Astro Content Collections blog loader. Use this in Astro projects.
42
+ * Astro Content Collections blog loader. Use in any Astro project.
37
43
  *
38
- * Returns raw CollectionEntry objects (preserving `entry.data`, `entry.id`,
39
- * and the ability to call `render(entry)` from astro:content). Filtering and
40
- * sorting are applied:
41
- * - Drafts excluded (when `draftField` is set)
42
- * - Sorted newest first by `dateField`
43
- * - `getPublishedEntries()` further excludes future-dated entries
44
+ * Returns raw CollectionEntry objects with filtering applied (drafts excluded,
45
+ * sorted newest first by `dateField`). Preserves `entry.data`, `entry.id`,
46
+ * and the ability to call `render(entry)` for `<Content />`.
44
47
  *
45
- * The collection's actual entry type is `CollectionEntry<'<name>'>` from
46
- * `astro:content`. Pass it as the generic to get full typed `data`:
47
- *
48
- * ```ts
49
- * import type { CollectionEntry } from 'astro:content';
50
- * export const blog = createBlogFromCollection<CollectionEntry<'blog'>>({
51
- * collectionName: 'blog',
52
- * dateField: 'pubDate',
53
- * draftField: 'draft',
54
- * });
55
- * ```
56
- *
57
- * Dynamically imports `astro:content` at call time so the package stays
58
- * usable in non-Astro projects.
48
+ * Import only from inside Astro code (`src/lib/blog.ts`, page routes). Do not
49
+ * import from `astro.config.mjs` Astro CC isn't initialized at config time.
50
+ * Use `createBlog` (fs) for astro.config.
59
51
  */
60
52
  export function createBlogFromCollection<E extends BlogCollectionEntry = BlogCollectionEntry>(
61
53
  config: CollectionBlogConfig = {}
@@ -66,22 +58,8 @@ export function createBlogFromCollection<E extends BlogCollectionEntry = BlogCol
66
58
  const dateField = config.dateField ?? 'date';
67
59
  const draftField = config.draftField ?? null;
68
60
 
69
- async function getCollection(): Promise<E[]> {
70
- let mod: { getCollection: (name: string) => Promise<E[]> };
71
- try {
72
- // @ts-expect-error - 'astro:content' is a virtual module resolved by Astro at build time.
73
- mod = await import('astro:content');
74
- } catch (err) {
75
- throw new Error(
76
- `createBlogFromCollection() requires Astro and a configured content collection ` +
77
- `named '${collectionName}'. Original error: ${err instanceof Error ? err.message : String(err)}`
78
- );
79
- }
80
- return mod.getCollection(collectionName);
81
- }
82
-
83
61
  async function readAll(): Promise<E[]> {
84
- const all = await getCollection();
62
+ const all = (await astroGetCollection(collectionName)) as E[];
85
63
  const filtered = draftField ? all.filter((e) => !e.data[draftField]) : all;
86
64
  return filtered.sort((a, b) => {
87
65
  const da = asDate(a.data[dateField])?.valueOf() ?? 0;
@@ -1,5 +1,3 @@
1
1
  export * from './types.js';
2
2
  export { createBlog } from './fs.js';
3
3
  export type { FsBlogConfig } from './fs.js';
4
- export { createBlogFromCollection } from './collection.js';
5
- export type { CollectionBlogConfig } from './collection.js';
@@ -1,3 +1,8 @@
1
+ // fs (gray-matter) blog loader — safe to import from anywhere (including
2
+ // astro.config.mjs and non-Astro projects).
3
+ //
4
+ // For the Astro Content Collections variant, import from
5
+ // `@ibalzam/codejitsu-core/blog/collection` (separate subpath because it
6
+ // statically imports `astro:content`, which is only available inside Astro).
1
7
  export * from './types.js';
2
8
  export { createBlog } from './fs.js';
3
- export { createBlogFromCollection } from './collection.js';
@@ -1,5 +1,10 @@
1
+ // fs (gray-matter) blog loader — safe to import from anywhere (including
2
+ // astro.config.mjs and non-Astro projects).
3
+ //
4
+ // For the Astro Content Collections variant, import from
5
+ // `@ibalzam/codejitsu-core/blog/collection` (separate subpath because it
6
+ // statically imports `astro:content`, which is only available inside Astro).
7
+
1
8
  export * from './types.js';
2
9
  export { createBlog } from './fs.js';
3
10
  export type { FsBlogConfig } from './fs.js';
4
- export { createBlogFromCollection } from './collection.js';
5
- export type { CollectionBlogConfig } from './collection.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ibalzam/codejitsu-core",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "description": "Shared core for Codejitsu Astro sites — reusable code and Claude-facing instructions for blog, SEO, images, deploy, and llms.txt.",
6
6
  "keywords": [
@@ -43,6 +43,10 @@
43
43
  "types": "./modules/blog/src/index.d.ts",
44
44
  "default": "./modules/blog/src/index.js"
45
45
  },
46
+ "./blog/collection": {
47
+ "types": "./modules/blog/src/collection.d.ts",
48
+ "default": "./modules/blog/src/collection.js"
49
+ },
46
50
  "./seo": {
47
51
  "types": "./modules/seo/src/index.d.ts",
48
52
  "default": "./modules/seo/src/index.js"