@piqit/resolvers 0.0.2 → 0.0.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.
- package/package.json +9 -9
- package/src/edge.ts +0 -15
- package/src/file-markdown.ts +0 -352
- package/src/frontmatter.ts +0 -269
- package/src/index.ts +0 -65
- package/src/markdown.ts +0 -262
- package/src/path-pattern.ts +0 -226
- package/src/static.ts +0 -227
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@piqit/resolvers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": {
|
|
8
|
-
"types": "./
|
|
9
|
-
"
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
10
11
|
},
|
|
11
12
|
"./edge": {
|
|
12
|
-
"types": "./
|
|
13
|
-
"
|
|
13
|
+
"types": "./dist/edge.d.ts",
|
|
14
|
+
"default": "./dist/edge.js"
|
|
14
15
|
},
|
|
15
16
|
"./static": {
|
|
16
|
-
"types": "./
|
|
17
|
-
"
|
|
17
|
+
"types": "./dist/static.d.ts",
|
|
18
|
+
"default": "./dist/static.js"
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
|
-
"src",
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"piqit": "0.0.
|
|
25
|
+
"piqit": "0.0.3"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/bun": "latest",
|
package/src/edge.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @piqit/resolvers/edge - Edge-compatible resolvers
|
|
3
|
-
*
|
|
4
|
-
* This entry point only exports resolvers that work in edge environments
|
|
5
|
-
* like Cloudflare Workers, where dynamic code generation is not allowed.
|
|
6
|
-
*
|
|
7
|
-
* Use this instead of '@piqit/resolvers' in your Worker:
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* import { staticContent } from "@piqit/resolvers/edge";
|
|
11
|
-
*
|
|
12
|
-
* @packageDocumentation
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
export { staticContent, staticResolver } from "./static"
|
package/src/file-markdown.ts
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Filesystem Markdown Resolver
|
|
3
|
-
*
|
|
4
|
-
* A resolver for querying markdown files from the filesystem.
|
|
5
|
-
* Optimized for reading only what's needed based on the query.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Resolver, StandardSchema, Infer } from "piqit"
|
|
9
|
-
import { compilePattern, createParamsSchema, type PathParams } from "./path-pattern"
|
|
10
|
-
import { parseFrontmatter } from "./frontmatter"
|
|
11
|
-
import { parseMarkdownBody, type BodyOptions, type BodyResult, type Heading } from "./markdown"
|
|
12
|
-
import path from "node:path"
|
|
13
|
-
|
|
14
|
-
// =============================================================================
|
|
15
|
-
// Types
|
|
16
|
-
// =============================================================================
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Options for creating a file markdown resolver.
|
|
20
|
-
*/
|
|
21
|
-
export interface FileMarkdownOptions<
|
|
22
|
-
TPath extends string,
|
|
23
|
-
TFrontmatter extends StandardSchema,
|
|
24
|
-
TBody extends BodyOptions
|
|
25
|
-
> {
|
|
26
|
-
/**
|
|
27
|
-
* Base directory for finding files.
|
|
28
|
-
* Can be absolute or relative to cwd.
|
|
29
|
-
*/
|
|
30
|
-
base: string
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Path pattern with {param} placeholders.
|
|
34
|
-
* @example '{year}/{slug}.md'
|
|
35
|
-
*/
|
|
36
|
-
path: TPath
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Schema for validating frontmatter.
|
|
40
|
-
* The schema's inferred type defines filter parameters.
|
|
41
|
-
*/
|
|
42
|
-
frontmatter: TFrontmatter
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Body parsing options.
|
|
46
|
-
* @default { raw: false, html: false, headings: false }
|
|
47
|
-
*/
|
|
48
|
-
body?: TBody
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The shape of results from a file markdown resolver.
|
|
53
|
-
*/
|
|
54
|
-
export interface FileMarkdownResult<
|
|
55
|
-
TParams,
|
|
56
|
-
TFrontmatter,
|
|
57
|
-
TBody extends BodyResult
|
|
58
|
-
> {
|
|
59
|
-
params: TParams
|
|
60
|
-
frontmatter: TFrontmatter
|
|
61
|
-
body: TBody
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Type for body shape based on options.
|
|
66
|
-
*/
|
|
67
|
-
type ComputedBodyShape<T extends BodyOptions | undefined> = T extends BodyOptions
|
|
68
|
-
? {
|
|
69
|
-
raw: T["raw"] extends true ? string : never
|
|
70
|
-
html: T["html"] extends true ? string : never
|
|
71
|
-
headings: T["headings"] extends true ? Heading[] : never
|
|
72
|
-
}
|
|
73
|
-
: Record<string, never>
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Clean up body shape to remove never types.
|
|
77
|
-
*/
|
|
78
|
-
type CleanBodyShape<T> = {
|
|
79
|
-
[K in keyof T as T[K] extends never ? never : K]: T[K]
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// =============================================================================
|
|
83
|
-
// Result Schema Factory
|
|
84
|
-
// =============================================================================
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Create a result schema for the resolver.
|
|
88
|
-
* This schema validates the namespaced result shape.
|
|
89
|
-
*/
|
|
90
|
-
function createResultSchema<TFrontmatter, TBody extends BodyResult>(
|
|
91
|
-
_paramNames: string[],
|
|
92
|
-
frontmatterSchema: StandardSchema<TFrontmatter>,
|
|
93
|
-
bodyOptions: BodyOptions
|
|
94
|
-
): StandardSchema<FileMarkdownResult<Record<string, string>, TFrontmatter, TBody>> {
|
|
95
|
-
return {
|
|
96
|
-
"~standard": {
|
|
97
|
-
version: 1,
|
|
98
|
-
vendor: "piqit/resolvers",
|
|
99
|
-
validate(value: unknown) {
|
|
100
|
-
if (value === null || typeof value !== "object") {
|
|
101
|
-
return { issues: [{ message: "Expected object" }] }
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const obj = value as Record<string, unknown>
|
|
105
|
-
|
|
106
|
-
// Validate params
|
|
107
|
-
if (obj.params == null || typeof obj.params !== "object") {
|
|
108
|
-
return { issues: [{ message: "Missing params", path: ["params"] }] }
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Validate frontmatter using the provided schema
|
|
112
|
-
const fmResult = frontmatterSchema["~standard"].validate(obj.frontmatter)
|
|
113
|
-
if (fmResult.issues) {
|
|
114
|
-
return {
|
|
115
|
-
issues: fmResult.issues.map((issue) => ({
|
|
116
|
-
...issue,
|
|
117
|
-
path: ["frontmatter", ...(issue.path || [])],
|
|
118
|
-
})),
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Validate body shape
|
|
123
|
-
if (bodyOptions.raw || bodyOptions.html || bodyOptions.headings) {
|
|
124
|
-
if (obj.body == null || typeof obj.body !== "object") {
|
|
125
|
-
return { issues: [{ message: "Missing body", path: ["body"] }] }
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return { value: obj as unknown as FileMarkdownResult<Record<string, string>, TFrontmatter, TBody> }
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// =============================================================================
|
|
136
|
-
// Helper Functions
|
|
137
|
-
// =============================================================================
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Check if any select paths require frontmatter data.
|
|
141
|
-
*/
|
|
142
|
-
function needsFrontmatter(selectPaths: string[]): boolean {
|
|
143
|
-
return selectPaths.some((p) => p.startsWith("frontmatter.") || p === "frontmatter.*")
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Check if any select paths require body data.
|
|
148
|
-
*/
|
|
149
|
-
function needsBody(selectPaths: string[]): boolean {
|
|
150
|
-
return selectPaths.some((p) => p.startsWith("body.") || p === "body.*")
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Check if any select paths require params.
|
|
155
|
-
*/
|
|
156
|
-
function needsParams(selectPaths: string[]): boolean {
|
|
157
|
-
return selectPaths.some((p) => p.startsWith("params.") || p === "params.*")
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Get which body parts are needed based on select paths.
|
|
162
|
-
*/
|
|
163
|
-
function getNeededBodyParts(selectPaths: string[]): BodyOptions {
|
|
164
|
-
const result: BodyOptions = {}
|
|
165
|
-
|
|
166
|
-
for (const path of selectPaths) {
|
|
167
|
-
if (path === "body.*") {
|
|
168
|
-
// Need all body parts
|
|
169
|
-
return { raw: true, html: true, headings: true }
|
|
170
|
-
}
|
|
171
|
-
if (path === "body.raw") result.raw = true
|
|
172
|
-
if (path === "body.html") result.html = true
|
|
173
|
-
if (path === "body.headings") result.headings = true
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return result
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Check if filter constraints match frontmatter.
|
|
181
|
-
* Simple equality check for now.
|
|
182
|
-
*/
|
|
183
|
-
function matchesFilter(
|
|
184
|
-
frontmatter: Record<string, unknown>,
|
|
185
|
-
filter: Record<string, unknown>
|
|
186
|
-
): boolean {
|
|
187
|
-
for (const [key, value] of Object.entries(filter)) {
|
|
188
|
-
if (frontmatter[key] !== value) {
|
|
189
|
-
return false
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return true
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// =============================================================================
|
|
196
|
-
// Resolver Factory
|
|
197
|
-
// =============================================================================
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Create a filesystem markdown resolver.
|
|
201
|
-
*
|
|
202
|
-
* @example
|
|
203
|
-
* const postsResolver = fileMarkdown({
|
|
204
|
-
* base: 'content/posts',
|
|
205
|
-
* path: '{year}/{slug}.md',
|
|
206
|
-
* frontmatter: z.object({
|
|
207
|
-
* title: z.string(),
|
|
208
|
-
* status: z.enum(['draft', 'published']),
|
|
209
|
-
* }),
|
|
210
|
-
* body: { html: true, headings: true }
|
|
211
|
-
* })
|
|
212
|
-
*/
|
|
213
|
-
export function fileMarkdown<
|
|
214
|
-
TPath extends string,
|
|
215
|
-
TFrontmatter extends StandardSchema,
|
|
216
|
-
TBody extends BodyOptions = Record<string, never>
|
|
217
|
-
>(
|
|
218
|
-
options: FileMarkdownOptions<TPath, TFrontmatter, TBody>
|
|
219
|
-
): Resolver<
|
|
220
|
-
StandardSchema<Partial<PathParams<TPath>>>,
|
|
221
|
-
TFrontmatter,
|
|
222
|
-
StandardSchema<
|
|
223
|
-
FileMarkdownResult<
|
|
224
|
-
PathParams<TPath>,
|
|
225
|
-
Infer<TFrontmatter>,
|
|
226
|
-
CleanBodyShape<ComputedBodyShape<TBody>>
|
|
227
|
-
>
|
|
228
|
-
>
|
|
229
|
-
> {
|
|
230
|
-
const pattern = compilePattern(options.path)
|
|
231
|
-
const basePath = path.isAbsolute(options.base)
|
|
232
|
-
? options.base
|
|
233
|
-
: path.join(process.cwd(), options.base)
|
|
234
|
-
|
|
235
|
-
const bodyOptions: BodyOptions = options.body || {}
|
|
236
|
-
|
|
237
|
-
// Create schemas
|
|
238
|
-
const scanSchema = createParamsSchema(pattern) as StandardSchema<Partial<PathParams<TPath>>>
|
|
239
|
-
const resultSchema = createResultSchema(
|
|
240
|
-
pattern.paramNames,
|
|
241
|
-
options.frontmatter,
|
|
242
|
-
bodyOptions
|
|
243
|
-
) as StandardSchema<
|
|
244
|
-
FileMarkdownResult<
|
|
245
|
-
PathParams<TPath>,
|
|
246
|
-
Infer<TFrontmatter>,
|
|
247
|
-
CleanBodyShape<ComputedBodyShape<TBody>>
|
|
248
|
-
>
|
|
249
|
-
>
|
|
250
|
-
|
|
251
|
-
return {
|
|
252
|
-
schema: {
|
|
253
|
-
scanParams: scanSchema,
|
|
254
|
-
filterParams: options.frontmatter,
|
|
255
|
-
result: resultSchema,
|
|
256
|
-
},
|
|
257
|
-
|
|
258
|
-
async resolve(spec) {
|
|
259
|
-
const results: Array<
|
|
260
|
-
Partial<
|
|
261
|
-
FileMarkdownResult<
|
|
262
|
-
PathParams<TPath>,
|
|
263
|
-
Infer<TFrontmatter>,
|
|
264
|
-
CleanBodyShape<ComputedBodyShape<TBody>>
|
|
265
|
-
>
|
|
266
|
-
>
|
|
267
|
-
> = []
|
|
268
|
-
|
|
269
|
-
// 1. Generate glob pattern from scan constraints
|
|
270
|
-
const globPattern = pattern.toGlob(spec.scan as Record<string, unknown>)
|
|
271
|
-
|
|
272
|
-
// 2. Find matching files using Bun.Glob
|
|
273
|
-
const glob = new Bun.Glob(globPattern)
|
|
274
|
-
const files: string[] = []
|
|
275
|
-
|
|
276
|
-
for await (const file of glob.scan({ cwd: basePath, absolute: false })) {
|
|
277
|
-
files.push(file)
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// 3. Determine what we need to read
|
|
281
|
-
const wantParams = needsParams(spec.select)
|
|
282
|
-
const wantFrontmatter = needsFrontmatter(spec.select)
|
|
283
|
-
const wantBody = needsBody(spec.select)
|
|
284
|
-
const hasFilter = spec.filter && Object.keys(spec.filter).length > 0
|
|
285
|
-
const neededBodyParts = wantBody ? getNeededBodyParts(spec.select) : {}
|
|
286
|
-
|
|
287
|
-
// 4. Process each file
|
|
288
|
-
for (const relativePath of files) {
|
|
289
|
-
// Extract params from path
|
|
290
|
-
const params = pattern.match(relativePath)
|
|
291
|
-
if (!params) continue
|
|
292
|
-
|
|
293
|
-
const fullPath = path.join(basePath, relativePath)
|
|
294
|
-
|
|
295
|
-
// Read file content only if needed
|
|
296
|
-
let content: string | null = null
|
|
297
|
-
let frontmatter: Record<string, unknown> | null = null
|
|
298
|
-
let body: BodyResult | null = null
|
|
299
|
-
|
|
300
|
-
// If filtering or selecting frontmatter, we need to read it
|
|
301
|
-
if (hasFilter || wantFrontmatter) {
|
|
302
|
-
content = await Bun.file(fullPath).text()
|
|
303
|
-
frontmatter = parseFrontmatter(content)
|
|
304
|
-
|
|
305
|
-
if (!frontmatter) {
|
|
306
|
-
frontmatter = {}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Check filter constraints
|
|
310
|
-
if (hasFilter && !matchesFilter(frontmatter, spec.filter as Record<string, unknown>)) {
|
|
311
|
-
continue
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// If selecting body, parse it
|
|
316
|
-
if (wantBody) {
|
|
317
|
-
if (!content) {
|
|
318
|
-
content = await Bun.file(fullPath).text()
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Only parse the body parts that are needed
|
|
322
|
-
body = parseMarkdownBody(content, neededBodyParts)
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// Build result with only requested fields
|
|
326
|
-
const result: Partial<
|
|
327
|
-
FileMarkdownResult<
|
|
328
|
-
PathParams<TPath>,
|
|
329
|
-
Infer<TFrontmatter>,
|
|
330
|
-
CleanBodyShape<ComputedBodyShape<TBody>>
|
|
331
|
-
>
|
|
332
|
-
> = {}
|
|
333
|
-
|
|
334
|
-
if (wantParams) {
|
|
335
|
-
result.params = params as PathParams<TPath>
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (wantFrontmatter) {
|
|
339
|
-
result.frontmatter = frontmatter as Infer<TFrontmatter>
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
if (wantBody && body) {
|
|
343
|
-
result.body = body as CleanBodyShape<ComputedBodyShape<TBody>>
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
results.push(result)
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
return results
|
|
350
|
-
},
|
|
351
|
-
}
|
|
352
|
-
}
|
package/src/frontmatter.ts
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* YAML Frontmatter Extraction
|
|
3
|
-
*
|
|
4
|
-
* Optimized frontmatter parsing that reads only what's needed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// =============================================================================
|
|
8
|
-
// Frontmatter Parsing
|
|
9
|
-
// =============================================================================
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Regex to match YAML frontmatter block at the start of content.
|
|
13
|
-
* Matches --- at start, captures content, ends with ---
|
|
14
|
-
*/
|
|
15
|
-
const FRONTMATTER_REGEX = /^---\r?\n([\s\S]*?)\r?\n---/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Parse YAML frontmatter from content string.
|
|
19
|
-
*
|
|
20
|
-
* Uses a simple YAML parser that handles common cases:
|
|
21
|
-
* - key: value
|
|
22
|
-
* - key: "quoted value"
|
|
23
|
-
* - key: 'quoted value'
|
|
24
|
-
* - key: [array, items]
|
|
25
|
-
* - Multi-line values with proper indentation
|
|
26
|
-
*
|
|
27
|
-
* @param content - The file content (or just the frontmatter portion)
|
|
28
|
-
* @returns The parsed frontmatter object, or null if not found
|
|
29
|
-
*/
|
|
30
|
-
export function parseFrontmatter(content: string): Record<string, unknown> | null {
|
|
31
|
-
const match = FRONTMATTER_REGEX.exec(content)
|
|
32
|
-
if (!match) return null
|
|
33
|
-
|
|
34
|
-
const yaml = match[1]
|
|
35
|
-
return parseSimpleYaml(yaml)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Extract just the frontmatter portion from content.
|
|
40
|
-
* Returns the raw YAML string without the --- delimiters.
|
|
41
|
-
*
|
|
42
|
-
* @param content - The file content
|
|
43
|
-
* @returns The YAML content, or null if no frontmatter
|
|
44
|
-
*/
|
|
45
|
-
export function extractFrontmatterString(content: string): string | null {
|
|
46
|
-
const match = FRONTMATTER_REGEX.exec(content)
|
|
47
|
-
return match ? match[1] : null
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get the byte offset where frontmatter ends (after closing ---).
|
|
52
|
-
* Returns 0 if no frontmatter found.
|
|
53
|
-
*
|
|
54
|
-
* @param content - The file content
|
|
55
|
-
* @returns Byte offset after frontmatter, or 0
|
|
56
|
-
*/
|
|
57
|
-
export function getFrontmatterEndOffset(content: string): number {
|
|
58
|
-
const match = FRONTMATTER_REGEX.exec(content)
|
|
59
|
-
if (!match) return 0
|
|
60
|
-
return match[0].length
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// =============================================================================
|
|
64
|
-
// Simple YAML Parser
|
|
65
|
-
// =============================================================================
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Parse simple YAML into an object.
|
|
69
|
-
* Handles the common patterns found in markdown frontmatter.
|
|
70
|
-
*/
|
|
71
|
-
function parseSimpleYaml(yaml: string): Record<string, unknown> {
|
|
72
|
-
const result: Record<string, unknown> = {}
|
|
73
|
-
const lines = yaml.split(/\r?\n/)
|
|
74
|
-
|
|
75
|
-
let currentKey: string | null = null
|
|
76
|
-
let currentValue: string[] = []
|
|
77
|
-
let inMultiline = false
|
|
78
|
-
let inBlockArray = false
|
|
79
|
-
let blockArrayItems: unknown[] = []
|
|
80
|
-
let multilineIndent = 0
|
|
81
|
-
|
|
82
|
-
function commitValue() {
|
|
83
|
-
if (currentKey) {
|
|
84
|
-
if (inBlockArray) {
|
|
85
|
-
// Commit block array
|
|
86
|
-
result[currentKey] = blockArrayItems
|
|
87
|
-
blockArrayItems = []
|
|
88
|
-
inBlockArray = false
|
|
89
|
-
} else if (currentValue.length === 1) {
|
|
90
|
-
result[currentKey] = parseYamlValue(currentValue[0])
|
|
91
|
-
} else if (currentValue.length > 1) {
|
|
92
|
-
result[currentKey] = currentValue.join("\n")
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
currentKey = null
|
|
96
|
-
currentValue = []
|
|
97
|
-
inMultiline = false
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
for (const line of lines) {
|
|
101
|
-
// Check for block array item (- value)
|
|
102
|
-
const arrayItemMatch = line.match(/^(\s+)-\s+(.*)$/)
|
|
103
|
-
if (arrayItemMatch && currentKey && (inBlockArray || (inMultiline && currentValue.length === 0))) {
|
|
104
|
-
inBlockArray = true
|
|
105
|
-
inMultiline = false
|
|
106
|
-
const itemValue = arrayItemMatch[2].trim()
|
|
107
|
-
blockArrayItems.push(parseYamlValue(itemValue))
|
|
108
|
-
continue
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Check for key: value pattern
|
|
112
|
-
const keyMatch = line.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)\s*:\s*(.*)$/)
|
|
113
|
-
|
|
114
|
-
if (keyMatch && !inMultiline) {
|
|
115
|
-
// Commit previous key-value
|
|
116
|
-
commitValue()
|
|
117
|
-
|
|
118
|
-
currentKey = keyMatch[1]
|
|
119
|
-
const valueStr = keyMatch[2].trim()
|
|
120
|
-
|
|
121
|
-
if (valueStr === "" || valueStr === "|" || valueStr === ">") {
|
|
122
|
-
// Multiline value or block array starts on next line
|
|
123
|
-
inMultiline = true
|
|
124
|
-
multilineIndent = 0
|
|
125
|
-
} else {
|
|
126
|
-
currentValue = [valueStr]
|
|
127
|
-
}
|
|
128
|
-
} else if (inMultiline && currentKey && !inBlockArray) {
|
|
129
|
-
// Continuation of multiline value
|
|
130
|
-
if (line.trim() === "") {
|
|
131
|
-
currentValue.push("")
|
|
132
|
-
} else {
|
|
133
|
-
const indent = line.search(/\S/)
|
|
134
|
-
if (indent > 0) {
|
|
135
|
-
if (multilineIndent === 0) {
|
|
136
|
-
multilineIndent = indent
|
|
137
|
-
}
|
|
138
|
-
currentValue.push(line.slice(multilineIndent))
|
|
139
|
-
} else {
|
|
140
|
-
// No indent means new key or end of multiline
|
|
141
|
-
commitValue()
|
|
142
|
-
// Re-process this line
|
|
143
|
-
const reKeyMatch = line.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)\s*:\s*(.*)$/)
|
|
144
|
-
if (reKeyMatch) {
|
|
145
|
-
currentKey = reKeyMatch[1]
|
|
146
|
-
const valueStr = reKeyMatch[2].trim()
|
|
147
|
-
if (valueStr === "" || valueStr === "|" || valueStr === ">") {
|
|
148
|
-
inMultiline = true
|
|
149
|
-
multilineIndent = 0
|
|
150
|
-
} else {
|
|
151
|
-
currentValue = [valueStr]
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
} else if (currentKey && line.trim() === "") {
|
|
157
|
-
// Empty line after value - keep going
|
|
158
|
-
} else if (!currentKey && line.trim() !== "") {
|
|
159
|
-
// New key-value
|
|
160
|
-
const newKeyMatch = line.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)\s*:\s*(.*)$/)
|
|
161
|
-
if (newKeyMatch) {
|
|
162
|
-
currentKey = newKeyMatch[1]
|
|
163
|
-
const valueStr = newKeyMatch[2].trim()
|
|
164
|
-
if (valueStr === "" || valueStr === "|" || valueStr === ">") {
|
|
165
|
-
inMultiline = true
|
|
166
|
-
multilineIndent = 0
|
|
167
|
-
} else {
|
|
168
|
-
currentValue = [valueStr]
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Commit final value
|
|
175
|
-
commitValue()
|
|
176
|
-
|
|
177
|
-
return result
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Parse a single YAML value.
|
|
182
|
-
*/
|
|
183
|
-
function parseYamlValue(value: string): unknown {
|
|
184
|
-
// Remove quotes
|
|
185
|
-
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
186
|
-
return value.slice(1, -1)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Check for array
|
|
190
|
-
if (value.startsWith("[") && value.endsWith("]")) {
|
|
191
|
-
const inner = value.slice(1, -1)
|
|
192
|
-
if (inner.trim() === "") return []
|
|
193
|
-
return inner.split(",").map((item) => parseYamlValue(item.trim()))
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Check for booleans
|
|
197
|
-
if (value === "true") return true
|
|
198
|
-
if (value === "false") return false
|
|
199
|
-
|
|
200
|
-
// Check for null
|
|
201
|
-
if (value === "null" || value === "~") return null
|
|
202
|
-
|
|
203
|
-
// Check for numbers
|
|
204
|
-
const num = Number(value)
|
|
205
|
-
if (!isNaN(num) && value !== "") return num
|
|
206
|
-
|
|
207
|
-
// Check for dates (ISO format)
|
|
208
|
-
if (/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2})?/.test(value)) {
|
|
209
|
-
const date = new Date(value)
|
|
210
|
-
if (!isNaN(date.getTime())) return date.toISOString()
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return value
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// =============================================================================
|
|
217
|
-
// Optimized File Reading
|
|
218
|
-
// =============================================================================
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Read only the frontmatter from a file, optimized for large files.
|
|
222
|
-
*
|
|
223
|
-
* Reads incrementally until we find the closing ---, avoiding reading
|
|
224
|
-
* the entire file into memory.
|
|
225
|
-
*
|
|
226
|
-
* @param path - Path to the markdown file
|
|
227
|
-
* @param maxBytes - Maximum bytes to read looking for frontmatter (default 8KB)
|
|
228
|
-
* @returns The parsed frontmatter, or null if not found
|
|
229
|
-
*/
|
|
230
|
-
export async function readFrontmatter(
|
|
231
|
-
path: string,
|
|
232
|
-
maxBytes = 8192
|
|
233
|
-
): Promise<Record<string, unknown> | null> {
|
|
234
|
-
try {
|
|
235
|
-
const file = Bun.file(path)
|
|
236
|
-
const size = file.size
|
|
237
|
-
|
|
238
|
-
// Read up to maxBytes or file size
|
|
239
|
-
const bytesToRead = Math.min(maxBytes, size)
|
|
240
|
-
const buffer = await file.slice(0, bytesToRead).text()
|
|
241
|
-
|
|
242
|
-
return parseFrontmatter(buffer)
|
|
243
|
-
} catch {
|
|
244
|
-
return null
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Read frontmatter and return the offset where body starts.
|
|
250
|
-
* Useful when you need both frontmatter and body.
|
|
251
|
-
*
|
|
252
|
-
* @param path - Path to the markdown file
|
|
253
|
-
* @returns Object with frontmatter and body start offset
|
|
254
|
-
*/
|
|
255
|
-
export async function readFrontmatterWithOffset(
|
|
256
|
-
path: string
|
|
257
|
-
): Promise<{ frontmatter: Record<string, unknown> | null; bodyOffset: number }> {
|
|
258
|
-
try {
|
|
259
|
-
const file = Bun.file(path)
|
|
260
|
-
const content = await file.text()
|
|
261
|
-
|
|
262
|
-
const frontmatter = parseFrontmatter(content)
|
|
263
|
-
const bodyOffset = getFrontmatterEndOffset(content)
|
|
264
|
-
|
|
265
|
-
return { frontmatter, bodyOffset }
|
|
266
|
-
} catch {
|
|
267
|
-
return { frontmatter: null, bodyOffset: 0 }
|
|
268
|
-
}
|
|
269
|
-
}
|