@prunellejs/content 0.0.0-stage → 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/llms.txt +54 -0
- package/package.json +24 -4
- package/src/app.ts +13 -0
- package/src/collection.ts +534 -0
- package/src/components.ts +524 -0
- package/src/frontmatter.ts +43 -0
- package/src/index.ts +169 -0
- package/src/markdown.ts +171 -0
- package/src/rss.ts +85 -0
- package/src/sanitize.ts +309 -0
- package/README.md +0 -3
package/src/index.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// @prunellejs/content — Zero-dependency Bun-native Markdown collections, nested YAML frontmatter (Bun.YAML),
|
|
2
|
+
// Shiki/custom highlight hook, MDX-lite embedded TSX components, localized collections, TOC, and RSS (`0 KB` client JS).
|
|
3
|
+
|
|
4
|
+
import type { PrunellePlugin } from 'prunelle/config'
|
|
5
|
+
import type { Collection, InternalCollection } from './collection'
|
|
6
|
+
import type { ContentFeed, RssFeedConfig } from './rss'
|
|
7
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
8
|
+
import { resolve } from 'node:path'
|
|
9
|
+
import { definePlugin, isInside } from 'prunelle/config'
|
|
10
|
+
import { currentApp } from './app'
|
|
11
|
+
import {
|
|
12
|
+
COLLECTION_DIR_FOR_ROOT_KEY,
|
|
13
|
+
COLLECTION_OWNER_ROOT_KEY,
|
|
14
|
+
COLLECTION_VALIDATE_KEY,
|
|
15
|
+
COLLECTION_VERSION_KEY,
|
|
16
|
+
DEFINE_COLLECTION_RE,
|
|
17
|
+
discoverAppCollections,
|
|
18
|
+
|
|
19
|
+
registeredCollections,
|
|
20
|
+
walkCollectionDefFiles,
|
|
21
|
+
} from './collection'
|
|
22
|
+
import { etagMatches, renderRss } from './rss'
|
|
23
|
+
|
|
24
|
+
export { defineCollection } from './collection'
|
|
25
|
+
export type {
|
|
26
|
+
CodeHighlighter,
|
|
27
|
+
Collection,
|
|
28
|
+
CollectionOptions,
|
|
29
|
+
CollectionOptionsWithoutSchema,
|
|
30
|
+
CollectionOptionsWithSchema,
|
|
31
|
+
CollectionRenderConfig,
|
|
32
|
+
ContentEntry,
|
|
33
|
+
ContentHeading,
|
|
34
|
+
MarkdownComponent,
|
|
35
|
+
MarkdownImageResolver,
|
|
36
|
+
MarkdownImageResult,
|
|
37
|
+
RenderedMarkdown,
|
|
38
|
+
RenderMarkdownOptions,
|
|
39
|
+
} from './collection'
|
|
40
|
+
export { parseFrontmatter } from './frontmatter'
|
|
41
|
+
export { renderMarkdown } from './markdown'
|
|
42
|
+
export { renderRss } from './rss'
|
|
43
|
+
export type { ContentFeed, RssFeedConfig } from './rss'
|
|
44
|
+
|
|
45
|
+
const STATIC_COLLECTION_DIR_RE = /\bdefineCollection\s*(?:<[^>]*>)?\s*\(\s*\{[\s\S]*?\bdir\s*:\s*(['"`])([^'"`]+)\1/g
|
|
46
|
+
|
|
47
|
+
function discoverStaticCollectionDirs(root: string): string[] {
|
|
48
|
+
const absRoot = resolve(root)
|
|
49
|
+
const srcDir = resolve(absRoot, 'src')
|
|
50
|
+
const found: string[] = []
|
|
51
|
+
for (const file of walkCollectionDefFiles(srcDir)) {
|
|
52
|
+
let text: string
|
|
53
|
+
try {
|
|
54
|
+
text = readFileSync(file, 'utf8')
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
if (!DEFINE_COLLECTION_RE.test(text))
|
|
60
|
+
continue
|
|
61
|
+
STATIC_COLLECTION_DIR_RE.lastIndex = 0
|
|
62
|
+
let m: RegExpExecArray | null
|
|
63
|
+
while ((m = STATIC_COLLECTION_DIR_RE.exec(text)) !== null) {
|
|
64
|
+
const dir = resolve(absRoot, m[2]!)
|
|
65
|
+
if (existsSync(dir) && isInside(absRoot, dir, { orSame: true }))
|
|
66
|
+
found.push(dir)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return found
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function content<const TDataList extends readonly unknown[] = readonly unknown[]>(options?: {
|
|
73
|
+
feeds?: { [K in keyof TDataList]: RssFeedConfig<TDataList[K]> } | undefined
|
|
74
|
+
collections?: readonly Collection<unknown>[] | undefined
|
|
75
|
+
}): PrunellePlugin
|
|
76
|
+
export function content(options: { feeds?: readonly ContentFeed[] | undefined, collections?: readonly Collection<unknown>[] | undefined } = {}): PrunellePlugin {
|
|
77
|
+
const feeds = options.feeds ?? []
|
|
78
|
+
const extraCollections = options.collections ?? []
|
|
79
|
+
|
|
80
|
+
function dirOf(c: InternalCollection, root?: string): string {
|
|
81
|
+
return c[COLLECTION_DIR_FOR_ROOT_KEY]?.(root) ?? c.dir
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function activeCollections(root?: string): InternalCollection[] {
|
|
85
|
+
const out = new Set<InternalCollection>()
|
|
86
|
+
for (const f of feeds)
|
|
87
|
+
out.add(f.collection as InternalCollection)
|
|
88
|
+
for (const c of extraCollections)
|
|
89
|
+
out.add(c as InternalCollection)
|
|
90
|
+
const absRoot = root !== undefined ? resolve(root) : currentApp.root()
|
|
91
|
+
for (const c of registeredCollections.values()) {
|
|
92
|
+
const owner = c[COLLECTION_OWNER_ROOT_KEY]
|
|
93
|
+
if (owner !== undefined && absRoot !== undefined && owner !== absRoot)
|
|
94
|
+
continue
|
|
95
|
+
const dir = dirOf(c, absRoot)
|
|
96
|
+
if (!existsSync(dir))
|
|
97
|
+
continue
|
|
98
|
+
if (absRoot === undefined || isInside(absRoot, dir, { orSame: true }))
|
|
99
|
+
out.add(c)
|
|
100
|
+
}
|
|
101
|
+
return [...out]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return definePlugin({
|
|
105
|
+
name: '@prunellejs/content',
|
|
106
|
+
after: ['@prunellejs/i18n', '@prunellejs/images'],
|
|
107
|
+
watchPaths: (root) => {
|
|
108
|
+
const dirs = new Set<string>()
|
|
109
|
+
for (const c of activeCollections(root))
|
|
110
|
+
dirs.add(dirOf(c, root))
|
|
111
|
+
for (const dir of discoverStaticCollectionDirs(root))
|
|
112
|
+
dirs.add(dir)
|
|
113
|
+
return [...dirs]
|
|
114
|
+
},
|
|
115
|
+
requires(ctx) {
|
|
116
|
+
return feeds
|
|
117
|
+
.filter(feed => (feed.site ?? ctx.site) === undefined)
|
|
118
|
+
.map(feed => `the feed at ${feed.path ?? '/rss.xml'} has no \`site\`, so its links are relative and feed readers cannot open them: set \`site\` in \`prunelle.config.ts\` (or on the feed).`)
|
|
119
|
+
},
|
|
120
|
+
async check(ctx) {
|
|
121
|
+
await discoverAppCollections(ctx.root)
|
|
122
|
+
const seen = new Set<string>()
|
|
123
|
+
for (const c of activeCollections(ctx.root)) {
|
|
124
|
+
const validate = c[COLLECTION_VALIDATE_KEY]
|
|
125
|
+
if (validate === undefined)
|
|
126
|
+
continue
|
|
127
|
+
for (const err of validate(ctx.root)) {
|
|
128
|
+
if (seen.has(err))
|
|
129
|
+
continue
|
|
130
|
+
seen.add(err)
|
|
131
|
+
ctx.error(err)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
build(ctx) {
|
|
136
|
+
for (const feed of feeds) {
|
|
137
|
+
const rel = (feed.path ?? '/rss.xml').replace(/^\/+/, '')
|
|
138
|
+
ctx.emitPublic(rel, renderRss({ ...feed, site: feed.site ?? ctx.site } as RssFeedConfig<unknown>))
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
server(api) {
|
|
142
|
+
for (const feed of feeds) {
|
|
143
|
+
const path = feed.path ?? '/rss.xml'
|
|
144
|
+
let memo: { xml: string, etag: string, version: number } | undefined
|
|
145
|
+
api.route('GET', path, (ctx) => {
|
|
146
|
+
const versionFn = (feed.collection as unknown as Record<symbol, (() => number) | undefined>)[COLLECTION_VERSION_KEY]
|
|
147
|
+
const version = versionFn ? versionFn() : -1
|
|
148
|
+
if (api.dev || memo === undefined || memo.version !== version) {
|
|
149
|
+
const xml = renderRss({ ...feed, site: feed.site ?? api.site } as RssFeedConfig<unknown>)
|
|
150
|
+
const etag = `"${Bun.hash(xml).toString(36)}"`
|
|
151
|
+
memo = { xml, etag, version }
|
|
152
|
+
}
|
|
153
|
+
if (etagMatches(ctx.request.headers.get('if-none-match'), memo.etag)) {
|
|
154
|
+
return new Response(null, {
|
|
155
|
+
status: 304,
|
|
156
|
+
headers: { etag: memo.etag },
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
return new Response(memo.xml, {
|
|
160
|
+
headers: {
|
|
161
|
+
'content-type': 'application/rss+xml; charset=utf-8',
|
|
162
|
+
'etag': memo.etag,
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
}
|
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// renderMarkdown: Bun's native Markdown renderer plus highlight hook, embedded components, heading ids and TOC.
|
|
2
|
+
|
|
3
|
+
import type { ContentHeading, MarkdownImageResolver, MarkdownImageResult, RenderMarkdownOptions } from './index'
|
|
4
|
+
import { escAttr } from 'prunelle/server'
|
|
5
|
+
import { currentApp } from './app'
|
|
6
|
+
import { expandComponents } from './components'
|
|
7
|
+
import { sanitizeMarkdownHtml } from './sanitize'
|
|
8
|
+
|
|
9
|
+
function slugifyHeading(text: string): string {
|
|
10
|
+
return text
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.normalize('NFKD')
|
|
13
|
+
.replace(/[\u0300-\u036F]/g, '')
|
|
14
|
+
.replace(/<[^>]+>/g, '')
|
|
15
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
16
|
+
.replace(/^-+|-+$/g, '') || 'section'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function stripTags(html: string): string {
|
|
20
|
+
return html
|
|
21
|
+
.replace(/<[^>]+>/g, '')
|
|
22
|
+
.replaceAll('<', '<')
|
|
23
|
+
.replaceAll('>', '>')
|
|
24
|
+
.replaceAll('"', '"')
|
|
25
|
+
.replaceAll(''', '\'')
|
|
26
|
+
.replaceAll('&', '&')
|
|
27
|
+
.trim()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function findDefaultImageResolver(file: string | undefined): MarkdownImageResolver | undefined {
|
|
31
|
+
if (!file)
|
|
32
|
+
return undefined
|
|
33
|
+
return currentApp.imageResolver()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const FENCE_RE = /^([`~]{3,})([^\n]*)\n([\s\S]*?)^\1[ \t]*$/gm
|
|
37
|
+
let slotBatchId = 0
|
|
38
|
+
|
|
39
|
+
function renderMarkdownInternal(
|
|
40
|
+
body: string,
|
|
41
|
+
options: RenderMarkdownOptions,
|
|
42
|
+
inheritedSlotPrefixes?: ReadonlySet<string>,
|
|
43
|
+
): {
|
|
44
|
+
html: string
|
|
45
|
+
headings: ContentHeading[]
|
|
46
|
+
wordCount: number
|
|
47
|
+
readingMinutes: number
|
|
48
|
+
} {
|
|
49
|
+
const slots: string[] = []
|
|
50
|
+
// Cryptographically unguessable per-render slot prefix so untrusted markdown cannot forge or duplicate component slots.
|
|
51
|
+
const slotPrefix = `b${++slotBatchId}-${crypto.randomUUID().slice(0, 12)}-`
|
|
52
|
+
const allowedSlotPrefixes = new Set<string>(inheritedSlotPrefixes)
|
|
53
|
+
allowedSlotPrefixes.add(slotPrefix)
|
|
54
|
+
let processed = body
|
|
55
|
+
|
|
56
|
+
// 1. Extract fenced code blocks before component expansion so `<Tag>` inside ```code``` is never treated as a component
|
|
57
|
+
if (options.highlight !== undefined || options.components !== undefined) {
|
|
58
|
+
processed = processed.replace(FENCE_RE, (full, _fence: string, info: string, code: string) => {
|
|
59
|
+
if (options.highlight !== undefined) {
|
|
60
|
+
const lang = info.trim().split(/\s+/)[0] || undefined
|
|
61
|
+
const idx = slots.length
|
|
62
|
+
slots.push(options.highlight(code.replace(/\n$/, ''), lang))
|
|
63
|
+
return `\n\n<div data-pru-md-slot="${slotPrefix}${idx}"></div>\n\n`
|
|
64
|
+
}
|
|
65
|
+
// Protect code fences from component expansion when only components are configured
|
|
66
|
+
const idx = slots.length
|
|
67
|
+
slots.push(Bun.markdown.html(full, { tables: true, strikethrough: true, tasklists: true, autolinks: true }))
|
|
68
|
+
return `\n\n<div data-pru-md-slot="${slotPrefix}${idx}"></div>\n\n`
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 2. Expand embedded TSX components
|
|
73
|
+
if (options.components !== undefined) {
|
|
74
|
+
processed = expandComponents(
|
|
75
|
+
processed,
|
|
76
|
+
options.components,
|
|
77
|
+
options,
|
|
78
|
+
slots,
|
|
79
|
+
slotPrefix,
|
|
80
|
+
(innerMd, innerOpts) => renderMarkdownInternal(innerMd, innerOpts, allowedSlotPrefixes),
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 3. Render remaining Markdown via native Zig Bun.markdown.html
|
|
85
|
+
let html = Bun.markdown.html(processed, {
|
|
86
|
+
tables: true,
|
|
87
|
+
strikethrough: true,
|
|
88
|
+
tasklists: true,
|
|
89
|
+
autolinks: true,
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// 3b. Sanitize rendered Markdown HTML by default unless `trusted: true` is opted into.
|
|
93
|
+
// Runs before splicing developer-supplied highlight/component slots so custom components remain intact.
|
|
94
|
+
if (options.trusted !== true)
|
|
95
|
+
html = sanitizeMarkdownHtml(html, allowedSlotPrefixes)
|
|
96
|
+
|
|
97
|
+
// 4. Add unique heading IDs & collect TOC
|
|
98
|
+
const headings: ContentHeading[] = []
|
|
99
|
+
const usedIds = new Map<string, number>()
|
|
100
|
+
|
|
101
|
+
html = html.replace(/<h([1-6])>([\s\S]*?)<\/h\1>/g, (_match, levelStr: string, inner: string) => {
|
|
102
|
+
const level = Number(levelStr)
|
|
103
|
+
const text = stripTags(inner)
|
|
104
|
+
const base = slugifyHeading(text)
|
|
105
|
+
const count = usedIds.get(base) ?? 0
|
|
106
|
+
usedIds.set(base, count + 1)
|
|
107
|
+
const id = count === 0 ? base : `${base}-${count}`
|
|
108
|
+
headings.push({ level, id, text })
|
|
109
|
+
if (options.headingLinks && level >= 2 && !/<a\b/i.test(inner))
|
|
110
|
+
return `<h${level} id="${id}"><a href="#${id}">${inner}</a></h${level}>`
|
|
111
|
+
return `<h${level} id="${id}">${inner}</h${level}>`
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
// 4b. Image rewriting (explicit resolveImage or auto-wired @prunellejs/images; `false` disables)
|
|
115
|
+
const resolveImageFn = options.resolveImage === false
|
|
116
|
+
? undefined
|
|
117
|
+
: (options.resolveImage ?? findDefaultImageResolver(options.file))
|
|
118
|
+
if (resolveImageFn !== undefined) {
|
|
119
|
+
html = html.replace(/<img\b([^>]*?)>/gi, (full, attrs: string) => {
|
|
120
|
+
const srcMatch = /\bsrc="([^"]+)"/i.exec(attrs)
|
|
121
|
+
if (!srcMatch)
|
|
122
|
+
return full
|
|
123
|
+
const resolved = resolveImageFn(srcMatch[1]!, options.file)
|
|
124
|
+
if (!resolved)
|
|
125
|
+
return full
|
|
126
|
+
const info: MarkdownImageResult = typeof resolved === 'string' ? { src: resolved } : resolved
|
|
127
|
+
const altMatch = /\balt="([^"]*)"/i.exec(attrs)
|
|
128
|
+
const alt = altMatch ? altMatch[1]! : ''
|
|
129
|
+
const parts = [`src="${escAttr(info.src)}"`, `alt="${alt}"`]
|
|
130
|
+
if (info.srcset)
|
|
131
|
+
parts.push(`srcset="${escAttr(info.srcset)}"`)
|
|
132
|
+
if (info.sizes)
|
|
133
|
+
parts.push(`sizes="${escAttr(info.sizes)}"`)
|
|
134
|
+
if (info.width !== undefined && info.width > 0)
|
|
135
|
+
parts.push(`width="${info.width}"`)
|
|
136
|
+
if (info.height !== undefined && info.height > 0)
|
|
137
|
+
parts.push(`height="${info.height}"`)
|
|
138
|
+
parts.push(`loading="${info.loading ?? 'lazy'}"`, 'decoding="async"')
|
|
139
|
+
const imgTag = `<img ${parts.join(' ')}>`
|
|
140
|
+
if (info.avifSrcset) {
|
|
141
|
+
const sizesAttr = info.sizes ? ` sizes="${escAttr(info.sizes)}"` : ''
|
|
142
|
+
return `<picture><source type="image/avif" srcset="${escAttr(info.avifSrcset)}"${sizesAttr}>${imgTag}</picture>`
|
|
143
|
+
}
|
|
144
|
+
return imgTag
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 5. Splice this batch's highlighted code blocks & embedded TSX components back in (loop handles components wrapping code fences)
|
|
149
|
+
if (slots.length > 0) {
|
|
150
|
+
const slotRe = new RegExp(`<div data-pru-md-slot="${slotPrefix}(\\d+)"><\\/div>`, 'g')
|
|
151
|
+
let prev = ''
|
|
152
|
+
while (prev !== html) {
|
|
153
|
+
prev = html
|
|
154
|
+
html = html.replace(slotRe, (_m, idxStr: string) => slots[Number(idxStr)] ?? '')
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const words = body.trim().split(/\s+/).filter(Boolean).length
|
|
159
|
+
const readingMinutes = Math.max(1, Math.ceil(words / 220))
|
|
160
|
+
return { html, headings, wordCount: words, readingMinutes }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Renders Markdown via Bun's native Zig `Bun.markdown.html()`, with optional Shiki `highlight`, embedded TSX `components`, heading IDs, TOC, and reading time. */
|
|
164
|
+
export function renderMarkdown(body: string, options: RenderMarkdownOptions = {}): {
|
|
165
|
+
html: string
|
|
166
|
+
headings: ContentHeading[]
|
|
167
|
+
wordCount: number
|
|
168
|
+
readingMinutes: number
|
|
169
|
+
} {
|
|
170
|
+
return renderMarkdownInternal(body, options)
|
|
171
|
+
}
|
package/src/rss.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// RSS 2.0 XML feed generation and ETag matching for @prunellejs/content.
|
|
2
|
+
|
|
3
|
+
import type { Collection, ContentEntry } from './collection'
|
|
4
|
+
import { escAttr } from 'prunelle/server'
|
|
5
|
+
import { currentApp } from './app'
|
|
6
|
+
|
|
7
|
+
export interface RssFeedConfig<TData = Record<string, unknown>> {
|
|
8
|
+
/** Public path for the RSS XML file (default: '/rss.xml'). */
|
|
9
|
+
path?: string | undefined
|
|
10
|
+
title: string
|
|
11
|
+
description: string
|
|
12
|
+
/** Public site origin; defaults to `PrunelleConfig.site` when registered via `content()`. */
|
|
13
|
+
site?: string | undefined
|
|
14
|
+
collection: Collection<TData>
|
|
15
|
+
locale?: string | undefined
|
|
16
|
+
/** URL prefix prepended to each entry's slug (e.g. '/blog'). */
|
|
17
|
+
linkPrefix?: string | undefined
|
|
18
|
+
mapItem?: ((entry: ContentEntry<TData>) => {
|
|
19
|
+
title: string
|
|
20
|
+
description?: string | undefined
|
|
21
|
+
date?: string | Date | undefined
|
|
22
|
+
link?: string | undefined
|
|
23
|
+
}) | undefined
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ContentFeed {
|
|
27
|
+
readonly path?: string | undefined
|
|
28
|
+
readonly title: string
|
|
29
|
+
readonly description: string
|
|
30
|
+
readonly site?: string | undefined
|
|
31
|
+
readonly collection: Collection<unknown>
|
|
32
|
+
readonly locale?: string | undefined
|
|
33
|
+
readonly linkPrefix?: string | undefined
|
|
34
|
+
readonly mapItem?: ((entry: ContentEntry<never>) => {
|
|
35
|
+
title: string
|
|
36
|
+
description?: string | undefined
|
|
37
|
+
date?: string | Date | undefined
|
|
38
|
+
link?: string | undefined
|
|
39
|
+
}) | undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatPubDate(value: string | Date | undefined): string {
|
|
43
|
+
if (value === undefined || value === '')
|
|
44
|
+
return ''
|
|
45
|
+
const date = value instanceof Date ? value : new Date(value)
|
|
46
|
+
return Number.isNaN(date.getTime()) ? '' : `<pubDate>${date.toUTCString()}</pubDate>`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function renderRss<TData>(feed: RssFeedConfig<TData>): string {
|
|
50
|
+
const site = (feed.site ?? currentApp.site() ?? '').replace(/\/$/, '')
|
|
51
|
+
const prefix = (feed.linkPrefix ?? '').replace(/\/$/, '')
|
|
52
|
+
const items = feed.collection.all(feed.locale).map((entry) => {
|
|
53
|
+
const rawData = entry.data as Record<string, unknown>
|
|
54
|
+
const mapped = feed.mapItem
|
|
55
|
+
? feed.mapItem(entry)
|
|
56
|
+
: {
|
|
57
|
+
title: String(rawData.title ?? entry.slug),
|
|
58
|
+
description: typeof rawData.description === 'string' ? rawData.description : undefined,
|
|
59
|
+
date: rawData.date as string | Date | undefined,
|
|
60
|
+
link: `${site}${prefix}/${entry.slug}`,
|
|
61
|
+
}
|
|
62
|
+
const link = mapped.link ?? `${site}${prefix}/${entry.slug}`
|
|
63
|
+
const pubDate = formatPubDate(mapped.date)
|
|
64
|
+
const desc = mapped.description ? `<description>${escAttr(mapped.description)}</description>` : ''
|
|
65
|
+
return `<item><title>${escAttr(mapped.title)}</title><link>${escAttr(link)}</link><guid>${escAttr(link)}</guid>${pubDate}${desc}</item>`
|
|
66
|
+
}).join('')
|
|
67
|
+
|
|
68
|
+
return `<?xml version="1.0" encoding="UTF-8"?>`
|
|
69
|
+
+ `<rss version="2.0"><channel>`
|
|
70
|
+
+ `<title>${escAttr(feed.title)}</title>`
|
|
71
|
+
+ `<link>${escAttr(site)}</link>`
|
|
72
|
+
+ `<description>${escAttr(feed.description)}</description>`
|
|
73
|
+
+ items
|
|
74
|
+
+ `</channel></rss>`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function etagMatches(ifNoneMatch: string | null, etag: string): boolean {
|
|
78
|
+
if (!ifNoneMatch)
|
|
79
|
+
return false
|
|
80
|
+
const trimmed = ifNoneMatch.trim()
|
|
81
|
+
if (trimmed === '*')
|
|
82
|
+
return true
|
|
83
|
+
const target = etag.replace(/^W\//, '')
|
|
84
|
+
return trimmed.split(',').some(part => part.trim().replace(/^W\//, '') === target)
|
|
85
|
+
}
|