@prunellejs/content 0.0.0-stage → 0.7.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kaynooo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/llms.txt ADDED
@@ -0,0 +1,83 @@
1
+ # @prunellejs/content Reference
2
+
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
+
5
+ ## Imports
6
+
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
+
9
+ ## Defining a Collection (`defineCollection(options)`)
10
+
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.
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.
20
+ - Direct options: `highlight(code, lang)`, `components` (MDX-lite TSX components usable inside Markdown; a non-literal prop like `tone={someVar}` and an unregistered `<Foo />` tag throw naming the file, and `__proto__`, `constructor` and `prototype` props are dropped), `headingLinks` (`true` wraps `h2`–`h6` in `#id` anchor links), `resolveImage` (auto-wired with `@prunellejs/images`; pass `false` to disable; the first image gets `loading="eager"`, the others `lazy`, unless it returns a `loading`), `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. Requests arriving while a load of the same locale runs share it.
26
+ - `ttlMs` keeps the entries of a finished load for every request that long, per locale (default `0`; `Infinity` until `clearCache()`); a failed load is not kept.
27
+ - `load({ locale, signal })`: pass `signal` to `fetch()` or the database client. It aborts after `timeoutMs` (default `30000`) or on `clearCache()`, and the load then fails even if the loader ignores it. `ttlMs < 0` or a `timeoutMs` that is not a positive finite number throws at `defineCollection`.
28
+ - Call `col.clearCache()` to drop cached entries. In production a file collection trusts its first scan: a file added afterwards is seen after `clearCache()` (the CMS plugin calls it on every write).
29
+
30
+ ## Collection & Entry API
31
+
32
+ - `col.all(locale?)`: sorted `ContentEntry<TData>[]` (or `Promise<...>` for `AsyncCollection`).
33
+ - `col.get(slug, locale?)`: `ContentEntry<TData> | undefined` (O(1) direct lookup for file collections; `Promise<...>` for `AsyncCollection`).
34
+ - `col.byTag(tag, locale?)`: entries whose `data.tags` includes `tag`.
35
+ - `col.paths(prefix?, locale?)`: URL list for `export const prerender = true; export const paths = () => col.paths('/blog')`.
36
+ - `col.clearCache()`: drops cached entries.
37
+ - `ContentEntry<TData>`: `slug`, `locale`, `file`, `data`, `body`, `html`, `headings` (`{ level, id, text }[]`), `wordCount`, `readingMinutes`, and `await entry.render()` (memoized).
38
+
39
+ ## Plugin Setup (`content({ feeds?, collections? })`)
40
+
41
+ - 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). A `src/` file that calls `defineCollection` but fails to import is reported once with its error, and tried again when it changes.
42
+ - `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()`. A production server serves the built file of a file-backed feed (the static server's `ETag`/`304`); a loader-backed feed and `prunelle dev` keep the live route on `GET <path>` with its own `ETag`/`304`. Use `renderRss()` for synchronous file collections and `await renderRssAsync()` for either.
43
+
44
+ ## Snippets
45
+
46
+ ```ts
47
+ // src/content.ts — file-backed collection
48
+ import { defineCollection } from '@prunellejs/content'
49
+
50
+ export const posts = defineCollection({
51
+ dir: 'src/content/posts',
52
+ headingLinks: true,
53
+ render: async () => ({ highlight: (await import('./highlight')).highlight }),
54
+ })
55
+
56
+ // src/cms.ts — headless CMS collection
57
+ export const articles = defineCollection({
58
+ name: 'articles',
59
+ schema: articleSchema,
60
+ async load() {
61
+ const res = await fetch(`${process.env.CMS_URL}/items/articles?filter[status][_eq]=published`)
62
+ const { data } = await res.json() as { data: Array<{ id: string, slug: string, title: string, published_at: string, body: string }> }
63
+ return data.map(row => ({
64
+ id: row.id,
65
+ slug: row.slug,
66
+ data: { title: row.title, date: row.published_at },
67
+ body: row.body,
68
+ }))
69
+ },
70
+ })
71
+ ```
72
+
73
+ ```ts
74
+ // prunelle.config.ts
75
+ import { content } from '@prunellejs/content'
76
+ import { defineConfig } from 'prunelle/config'
77
+ import { posts } from './src/content'
78
+
79
+ export default defineConfig({
80
+ site: 'https://example.com',
81
+ plugins: [content({ feeds: [{ collection: posts, title: 'Blog', description: 'Posts', linkPrefix: '/blog' }] })],
82
+ })
83
+ ```
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@prunellejs/content",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.7.2",
4
+ "description": "Zero-dependency Bun.markdown collections, YAML frontmatter, TOC, and RSS plugin for Prunelle",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "src",
10
+ "llms.txt"
11
+ ],
12
+ "exports": {
13
+ ".": "./src/index.ts"
14
+ },
15
+ "engines": {
16
+ "bun": ">=1.4.0"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/Kayno0o/prunelle",
21
+ "directory": "packages/plugin-content"
22
+ },
23
+ "peerDependencies": {
24
+ "prunelle": "^0.7.2"
25
+ }
26
+ }
package/src/app.ts ADDED
@@ -0,0 +1,15 @@
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
+
3
+ import type { ContentImageResolver } from 'prunelle/config'
4
+ import { CMS_INLINE_ENABLED_SERVICE, CONTENT_IMAGE_RESOLVER_SERVICE, I18N_CONFIG_SERVICE } from 'prunelle/config'
5
+ import { getAppContext } from 'prunelle/internal/server'
6
+
7
+ export const currentApp = {
8
+ root: (): string | undefined => getAppContext()?.root,
9
+ site: (): string | undefined => getAppContext()?.site,
10
+ dev: (): boolean | undefined => getAppContext()?.dev,
11
+ defaultLocale: (): string | undefined => getAppContext()?.inject(I18N_CONFIG_SERVICE)?.defaultLocale,
12
+ locales: (): readonly string[] | undefined => getAppContext()?.inject(I18N_CONFIG_SERVICE)?.locales,
13
+ imageResolver: (): ContentImageResolver | undefined => getAppContext()?.inject(CONTENT_IMAGE_RESOLVER_SERVICE),
14
+ cmsInline: (): boolean => getAppContext()?.inject(CMS_INLINE_ENABLED_SERVICE) === true,
15
+ }
@@ -0,0 +1,67 @@
1
+ // Typed file-backed and loader-backed Markdown collections with caching, schema validation, localization, and lazy renderers.
2
+
3
+ import type {
4
+ AsyncCollection,
5
+ Collection,
6
+ CollectionOptions,
7
+ CollectionOptionsWithoutSchema,
8
+ CollectionOptionsWithSchema,
9
+ LoaderCollectionOptions,
10
+ LoaderCollectionOptionsWithoutSchema,
11
+ LoaderCollectionOptionsWithSchema,
12
+ } from './types'
13
+ import { createFileCollection } from './file-collection'
14
+ import { createLoaderCollection } from './loader-collection'
15
+
16
+ export {
17
+ DEFINE_COLLECTION_RE,
18
+ discoverAppCollections,
19
+ registeredCollections,
20
+ walkCollectionDefFiles,
21
+ } from './registry'
22
+
23
+ export {
24
+ COLLECTION_DIR_FOR_ROOT_KEY,
25
+ COLLECTION_META_KEY,
26
+ COLLECTION_OWNER_ROOT_KEY,
27
+ COLLECTION_VALIDATE_KEY,
28
+ } from './types'
29
+
30
+ export type {
31
+ AnyCollection,
32
+ AsyncCollection,
33
+ BaseCollectionOptions,
34
+ CodeHighlighter,
35
+ Collection,
36
+ CollectionLoadContext,
37
+ CollectionLoader,
38
+ CollectionOptions,
39
+ CollectionOptionsWithoutSchema,
40
+ CollectionOptionsWithSchema,
41
+ CollectionRenderConfig,
42
+ ContentEntry,
43
+ ContentHeading,
44
+ InternalCollection,
45
+ InternalCollectionMeta,
46
+ LoadedEntryInput,
47
+ LoaderCollectionOptions,
48
+ LoaderCollectionOptionsWithoutSchema,
49
+ LoaderCollectionOptionsWithSchema,
50
+ MarkdownComponent,
51
+ MarkdownImageResolver,
52
+ MarkdownImageResult,
53
+ RenderedMarkdown,
54
+ RenderMarkdownOptions,
55
+ } from './types'
56
+
57
+ export function defineCollection<TData>(options: CollectionOptionsWithSchema<TData>): Collection<TData>
58
+ export function defineCollection(options: CollectionOptionsWithoutSchema): Collection<Record<string, unknown>>
59
+ export function defineCollection<TData>(options: LoaderCollectionOptionsWithSchema<TData>): AsyncCollection<TData>
60
+ export function defineCollection(options: LoaderCollectionOptionsWithoutSchema): AsyncCollection<Record<string, unknown>>
61
+ export function defineCollection<TData = Record<string, unknown>>(
62
+ options: CollectionOptions<TData> | LoaderCollectionOptions<TData>,
63
+ ): Collection<TData> | AsyncCollection<TData> {
64
+ if (typeof options.load === 'function')
65
+ return createLoaderCollection(options as LoaderCollectionOptions<TData>)
66
+ return createFileCollection(options as CollectionOptions<TData>)
67
+ }