@prunellejs/content 0.1.1 → 0.5.2
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/llms.txt +38 -11
- package/package.json +2 -2
- package/src/app.ts +3 -1
- package/src/collection.ts +64 -530
- package/src/components.ts +14 -5
- package/src/entry.ts +170 -0
- package/src/file-collection.ts +302 -0
- package/src/index.ts +71 -25
- package/src/loader-collection.ts +263 -0
- package/src/markdown.ts +15 -3
- package/src/registry.ts +135 -0
- package/src/rss.ts +40 -20
- package/src/service.ts +88 -0
- package/src/tracking.ts +80 -0
- package/src/types.ts +198 -0
package/llms.txt
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
# @prunellejs/content Reference
|
|
2
2
|
|
|
3
|
-
> Zero-dependency `Bun.markdown` + `Bun.YAML` typed Markdown collections, Standard Schema
|
|
3
|
+
> Zero-dependency `Bun.markdown` + `Bun.YAML` typed Markdown collections (local files or headless CMS loaders), Standard Schema validation, lazy Shiki/TSX rendering, TOC, localized content, and RSS feeds (`0 KB` client JS).
|
|
4
4
|
|
|
5
5
|
## Imports
|
|
6
6
|
|
|
7
|
-
- `@prunellejs/content`: `content`, `defineCollection`, `parseFrontmatter`, `renderMarkdown`, `renderRss`; types `Collection`, `CollectionOptions`, `CollectionOptionsWithSchema`, `CollectionOptionsWithoutSchema`, `CollectionRenderConfig`, `ContentEntry`, `ContentHeading`, `RenderedMarkdown`, `RenderMarkdownOptions`, `CodeHighlighter`, `MarkdownComponent`, `MarkdownImageResolver`, `MarkdownImageResult`, `RssFeedConfig`, `ContentFeed`.
|
|
7
|
+
- `@prunellejs/content`: `content`, `defineCollection`, `parseFrontmatter`, `renderMarkdown`, `renderRss`, `renderRssAsync`; types `Collection`, `AsyncCollection`, `AnyCollection`, `BaseCollectionOptions`, `CollectionOptions`, `CollectionOptionsWithSchema`, `CollectionOptionsWithoutSchema`, `LoaderCollectionOptions`, `LoaderCollectionOptionsWithSchema`, `LoaderCollectionOptionsWithoutSchema`, `CollectionLoadContext`, `CollectionLoader`, `LoadedEntryInput`, `CollectionRenderConfig`, `ContentEntry`, `ContentHeading`, `RenderedMarkdown`, `RenderMarkdownOptions`, `CodeHighlighter`, `MarkdownComponent`, `MarkdownImageResolver`, `MarkdownImageResult`, `RssFeedConfig`, `ContentFeed`.
|
|
8
8
|
|
|
9
9
|
## Defining a Collection (`defineCollection(options)`)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
- `
|
|
13
|
-
- `
|
|
11
|
+
Choose one source:
|
|
12
|
+
- **File-backed (`dir`)**: path to `.md`/`.markdown` files (relative to project root or absolute). Returns synchronous `Collection<TData>`.
|
|
13
|
+
- **Loader-backed (`load`)**: `async (ctx?: { locale?: string }) => LoadedEntryInput[]` returning `{ slug, data, body?, locale?, id? }[]` from any headless CMS, REST/GraphQL API, or database. Returns `AsyncCollection<TData>` whose `all()`, `get()`, `byTag()`, and `paths()` return Promises.
|
|
14
|
+
|
|
15
|
+
Shared options:
|
|
16
|
+
- `name`: optional collection identifier for diagnostics, CMS, and search (defaults to `dir` basename or `'collection'`).
|
|
17
|
+
- `schema`: optional Standard Schema v1 (`Zod`, `Valibot`, `ArkType`, custom) validating frontmatter / loaded `data` into `entry.data` (file-backed is synchronous; loader-backed supports both sync and async validators).
|
|
18
|
+
- `localized`: `true` reads `<dir>/<locale>/<slug>.md` for file collections, or passes `{ locale }` to `load()` and filters entries whose `entry.locale` is set for loaded collections; `defaultLocale` defaults to the app's i18n default or `'en'`; `locales` lists known locales for `prunelle check`, search, and CMS.
|
|
14
19
|
- `render`: lazy async factory `async () => ({ highlight, components, headingLinks, resolveImage, trusted })` so index pages, config, and RSS never import Shiki or TSX components until `await entry.render()` runs.
|
|
15
|
-
- Direct options
|
|
20
|
+
- Direct options: `highlight(code, lang)`, `components` (MDX-lite TSX components usable inside Markdown), `headingLinks` (`true` wraps `h2`–`h6` in `#id` anchor links), `resolveImage` (auto-wired with `@prunellejs/images`; pass `false` to disable), `trusted` (default `false`; HTML is sanitized unless `true`), `includeDrafts` (defaults to `true` in `prunelle dev` / `testApp({ dev: true })` and `false` in `prunelle build` / production; RSS feeds always leave drafts out), `sort(a, b)` (default newest `date` first, then `slug`).
|
|
21
|
+
|
|
22
|
+
## Caching (Loader-Backed Collections)
|
|
23
|
+
|
|
24
|
+
- During `prunelle build` / prerendering: `load()` is cached per `(build, locale)` so it runs **once** across `paths()`, prerendered pages, and feeds.
|
|
25
|
+
- At runtime: `load()` is cached per request (`currentRequest()?.ctx`), so layout, `load()`, and page share one fetch while the next request gets fresh data.
|
|
26
|
+
- Call `col.clearCache()` to drop cached entries.
|
|
16
27
|
|
|
17
28
|
## Collection & Entry API
|
|
18
29
|
|
|
19
|
-
- `col.all(locale?)`: sorted `ContentEntry<TData>[]
|
|
20
|
-
- `col.get(slug, locale?)`: O(1) lookup
|
|
30
|
+
- `col.all(locale?)`: sorted `ContentEntry<TData>[]` (or `Promise<...>` for `AsyncCollection`).
|
|
31
|
+
- `col.get(slug, locale?)`: `ContentEntry<TData> | undefined` (O(1) direct lookup for file collections; `Promise<...>` for `AsyncCollection`).
|
|
21
32
|
- `col.byTag(tag, locale?)`: entries whose `data.tags` includes `tag`.
|
|
22
33
|
- `col.paths(prefix?, locale?)`: URL list for `export const prerender = true; export const paths = () => col.paths('/blog')`.
|
|
23
34
|
- `col.clearCache()`: drops cached entries.
|
|
@@ -25,13 +36,13 @@
|
|
|
25
36
|
|
|
26
37
|
## Plugin Setup (`content({ feeds?, collections? })`)
|
|
27
38
|
|
|
28
|
-
- Register `content(...)` in `prunelle.config.ts` to auto-discover collections for `prunelle check`
|
|
29
|
-
- `feeds`: array of `RssFeedConfig` (`{ path? ('/rss.xml'), title, description, site? (inherits PrunelleConfig.site), collection, locale?, linkPrefix?, mapItem? }`). Emitted as static public files in `build()` and served with `ETag`/`304` on `GET <path>` in `server()`.
|
|
39
|
+
- Register `content(...)` in `prunelle.config.ts` to auto-discover file-backed and loader-backed collections for `prunelle check`, `prunelle dev` file watching, and `CONTENT_COLLECTIONS_SERVICE` (provided in `build`, `afterPrerender`, `server`, and `check` for downstream plugins).
|
|
40
|
+
- `feeds`: array of `RssFeedConfig` (`{ path? ('/rss.xml'), title, description, site? (inherits PrunelleConfig.site), collection, locale?, linkPrefix?, mapItem? }`), supporting both file-backed and loader-backed collections. Emitted as static public files in `build()` and served with `ETag`/`304` on `GET <path>` in `server()`. Use `renderRss()` for synchronous file collections and `await renderRssAsync()` for either.
|
|
30
41
|
|
|
31
42
|
## Snippets
|
|
32
43
|
|
|
33
44
|
```ts
|
|
34
|
-
// src/content.ts
|
|
45
|
+
// src/content.ts — file-backed collection
|
|
35
46
|
import { defineCollection } from '@prunellejs/content'
|
|
36
47
|
|
|
37
48
|
export const posts = defineCollection({
|
|
@@ -39,6 +50,22 @@ export const posts = defineCollection({
|
|
|
39
50
|
headingLinks: true,
|
|
40
51
|
render: async () => ({ highlight: (await import('./highlight')).highlight }),
|
|
41
52
|
})
|
|
53
|
+
|
|
54
|
+
// src/cms.ts — headless CMS collection
|
|
55
|
+
export const articles = defineCollection({
|
|
56
|
+
name: 'articles',
|
|
57
|
+
schema: articleSchema,
|
|
58
|
+
async load() {
|
|
59
|
+
const res = await fetch(`${process.env.CMS_URL}/items/articles?filter[status][_eq]=published`)
|
|
60
|
+
const { data } = await res.json() as { data: Array<{ id: string, slug: string, title: string, published_at: string, body: string }> }
|
|
61
|
+
return data.map(row => ({
|
|
62
|
+
id: row.id,
|
|
63
|
+
slug: row.slug,
|
|
64
|
+
data: { title: row.title, date: row.published_at },
|
|
65
|
+
body: row.body,
|
|
66
|
+
}))
|
|
67
|
+
},
|
|
68
|
+
})
|
|
42
69
|
```
|
|
43
70
|
|
|
44
71
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Zero-dependency Bun.markdown collections, YAML frontmatter, TOC, and RSS plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"directory": "packages/plugin-content"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"prunelle": "^0.
|
|
24
|
+
"prunelle": "^0.5.2"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/app.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The app a collection renders for. Collections are read from route modules, not through the plugin, so each read asks prunelle which app the current request or build belongs to.
|
|
2
2
|
|
|
3
3
|
import type { ContentImageResolver } from 'prunelle/config'
|
|
4
|
-
import { CONTENT_IMAGE_RESOLVER_SERVICE, I18N_CONFIG_SERVICE } from 'prunelle/config'
|
|
4
|
+
import { CMS_INLINE_ENABLED_SERVICE, CONTENT_IMAGE_RESOLVER_SERVICE, I18N_CONFIG_SERVICE } from 'prunelle/config'
|
|
5
5
|
import { getAppContext } from 'prunelle/internal/server'
|
|
6
6
|
|
|
7
7
|
export const currentApp = {
|
|
@@ -9,5 +9,7 @@ export const currentApp = {
|
|
|
9
9
|
site: (): string | undefined => getAppContext()?.site,
|
|
10
10
|
dev: (): boolean | undefined => getAppContext()?.dev,
|
|
11
11
|
defaultLocale: (): string | undefined => getAppContext()?.inject(I18N_CONFIG_SERVICE)?.defaultLocale,
|
|
12
|
+
locales: (): readonly string[] | undefined => getAppContext()?.inject(I18N_CONFIG_SERVICE)?.locales,
|
|
12
13
|
imageResolver: (): ContentImageResolver | undefined => getAppContext()?.inject(CONTENT_IMAGE_RESOLVER_SERVICE),
|
|
14
|
+
cmsInline: (): boolean => getAppContext()?.inject(CMS_INLINE_ENABLED_SERVICE) === true,
|
|
13
15
|
}
|