embedoc 0.9.0 → 0.9.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.
@@ -0,0 +1,685 @@
1
+ # Embed API Reference
2
+
3
+ embedoc Embed API
4
+
5
+ This module exports all types and functions needed for writing custom embeds.
6
+
7
+ ## Example
8
+
9
+ ```typescript
10
+ import { defineEmbed, type EmbedContext, type EmbedResult } from 'embedoc';
11
+
12
+ export default defineEmbed({
13
+ dependsOn: ['my_datasource'],
14
+ async render(ctx: EmbedContext): Promise<EmbedResult> {
15
+ const data = await ctx.datasources.my_datasource.query('SELECT * FROM table');
16
+ return { content: ctx.markdown.table(['Column'], data.map(r => [r.name])) };
17
+ }
18
+ });
19
+ ```
20
+
21
+ ## Interfaces
22
+
23
+ ### Datasource
24
+
25
+ Defined in: types/index.ts:184
26
+
27
+ Datasource interface for accessing external data.
28
+
29
+ Datasources are configured in `embedoc.config.yaml` and accessed
30
+ via `ctx.datasources` in embed render functions.
31
+
32
+ **Note**: The `query` option in config is for **generators** (file generation).
33
+ In embeds, use `query()` method to execute **dynamic queries with parameters**
34
+ from marker attributes or frontmatter.
35
+
36
+ Supported datasource types:
37
+ - `sqlite` - SQLite database (supports parameterized queries)
38
+ - `csv` - CSV files (use `getAll()`)
39
+ - `json` - JSON files (use `getAll()`)
40
+ - `yaml` - YAML files (use `getAll()`)
41
+ - `glob` - File listings (use `getAll()`)
42
+
43
+ #### Example
44
+
45
+ ```typescript
46
+ // In your embed's render function
47
+ const ds = ctx.datasources['metadata_db'];
48
+
49
+ // SQLite: dynamic query with marker parameters
50
+ const { id } = ctx.params; // From: <!--@embedoc:my_embed id="users"-->
51
+ const rows = await ds.query(
52
+ 'SELECT * FROM columns WHERE table_name = ?',
53
+ [id]
54
+ );
55
+
56
+ // CSV/JSON/YAML: get all data
57
+ const allData = await ds.getAll();
58
+ ```
59
+
60
+ #### Properties
61
+
62
+ | Property | Modifier | Type | Description | Defined in |
63
+ | ------ | ------ | ------ | ------ | ------ |
64
+ | <a id="type"></a> `type` | `readonly` | `string` | Datasource type identifier. One of: 'sqlite', 'csv', 'json', 'yaml', 'glob', 'inline' | types/index.ts:189 |
65
+
66
+ #### Methods
67
+
68
+ ##### getAll()
69
+
70
+ ```ts
71
+ getAll(): Promise<QueryResult>;
72
+ ```
73
+
74
+ Defined in: types/index.ts:238
75
+
76
+ Get all data from the datasource.
77
+
78
+ Returns all records without filtering. Recommended for
79
+ CSV, JSON, YAML, and Glob datasources.
80
+
81
+ ###### Returns
82
+
83
+ `Promise`\<[`QueryResult`](#queryresult)\>
84
+
85
+ Promise resolving to an array of all records
86
+
87
+ ###### Example
88
+
89
+ ```typescript
90
+ // CSV datasource
91
+ const endpoints = await ctx.datasources['api_endpoints'].getAll();
92
+
93
+ // JSON datasource
94
+ const config = await ctx.datasources['config'].getAll();
95
+ ```
96
+
97
+ ##### query()
98
+
99
+ ```ts
100
+ query(sql: string, params?: unknown[]): Promise<QueryResult>;
101
+ ```
102
+
103
+ Defined in: types/index.ts:219
104
+
105
+ Execute a parameterized query on the datasource.
106
+
107
+ **SQLite**: Execute SQL with parameters from marker attributes or frontmatter.
108
+ This allows dynamic filtering based on the document context.
109
+
110
+ **CSV/JSON/YAML/Glob**: Parameters are ignored; use `getAll()` instead.
111
+
112
+ ###### Parameters
113
+
114
+ | Parameter | Type | Description |
115
+ | ------ | ------ | ------ |
116
+ | `sql` | `string` | SQL query string with `?` placeholders for parameters |
117
+ | `params?` | `unknown`[] | Values to bind to the placeholders (prevents SQL injection) |
118
+
119
+ ###### Returns
120
+
121
+ `Promise`\<[`QueryResult`](#queryresult)\>
122
+
123
+ Promise resolving to an array of records
124
+
125
+ ###### Example
126
+
127
+ ```typescript
128
+ // Dynamic query using marker parameter
129
+ const { id } = ctx.params; // From: <!--@embedoc:table_columns id="users"-->
130
+ const columns = await ds.query(
131
+ 'SELECT * FROM columns WHERE table_name = ? ORDER BY ordinal_position',
132
+ [id]
133
+ );
134
+
135
+ // Multiple parameters
136
+ const filtered = await ds.query(
137
+ 'SELECT * FROM users WHERE status = ? AND role = ?',
138
+ [ctx.params['status'], ctx.params['role']]
139
+ );
140
+ ```
141
+
142
+ ***
143
+
144
+ ### EmbedContext
145
+
146
+ Defined in: types/index.ts:459
147
+
148
+ Context object passed to an embed's render function.
149
+
150
+ Provides access to:
151
+ - Marker parameters from the document
152
+ - Document frontmatter data
153
+ - Configured datasources
154
+ - Markdown generation helpers
155
+ - Current file path
156
+
157
+ #### Example
158
+
159
+ ```typescript
160
+ export default defineEmbed({
161
+ dependsOn: ['metadata_db'],
162
+ async render(ctx) {
163
+ // Access marker parameters
164
+ const { id } = ctx.params; // From: <!--@embedoc:my_embed id="users"-->
165
+
166
+ // Access frontmatter
167
+ const docType = ctx.frontmatter['doc_type'];
168
+
169
+ // Query datasource
170
+ const data = await ctx.datasources['metadata_db'].query(
171
+ 'SELECT * FROM tables WHERE name = ?',
172
+ [id]
173
+ );
174
+
175
+ // Generate markdown
176
+ return { content: ctx.markdown.table(['Name'], data.map(r => [r.name])) };
177
+ }
178
+ });
179
+ ```
180
+
181
+ #### Properties
182
+
183
+ | Property | Type | Description | Defined in |
184
+ | ------ | ------ | ------ | ------ |
185
+ | <a id="datasources"></a> `datasources` | `Record`\<`string`, [`Datasource`](#datasource)\> | Map of configured datasources. Keys are datasource names from `embedoc.config.yaml`. Includes both external datasources and inline datasources defined in the document. **Example** `// Access SQLite datasource const db = ctx.datasources['metadata_db']; const rows = await db.query('SELECT * FROM users'); // Access inline datasource const config = ctx.datasources['project_config']; const data = await config.getAll();` | types/index.ts:513 |
186
+ | <a id="filepath"></a> `filePath` | `string` | Absolute path to the current file being processed. Useful for generating relative links or file references. **Example** `const dir = path.dirname(ctx.filePath); const relativePath = path.relative(dir, targetFile);` | types/index.ts:536 |
187
+ | <a id="frontmatter"></a> `frontmatter` | `Record`\<`string`, `unknown`\> | Frontmatter data from the document. Parsed from YAML frontmatter at the top of the document. **Example** `// Document frontmatter: // --- // doc_id: "users" // schema: "public" // --- const docId = ctx.frontmatter['doc_id'] as string;` | types/index.ts:493 |
188
+ | <a id="markdown"></a> `markdown` | [`MarkdownHelper`](#markdownhelper) | Markdown generation helper. Always available. Provides methods for creating tables, lists, code blocks, links, and other Markdown elements. **See** [MarkdownHelper](#markdownhelper) | types/index.ts:523 |
189
+ | <a id="params"></a> `params` | `Record`\<`string`, `string`\> | Parameters from the marker attributes. Parsed from the marker syntax: `<!--@embedoc:embed_name param1="value1" param2="value2"-->` Variable references (`${...}`) are resolved before passing to the embed. **Example** `// Marker: <!--@embedoc:table_columns id="users" schema="public"--> const { id, schema } = ctx.params; // id = "users", schema = "public"` | types/index.ts:475 |
190
+
191
+ ***
192
+
193
+ ### EmbedDefinition
194
+
195
+ Defined in: types/index.ts:590
196
+
197
+ Embed definition interface.
198
+
199
+ Use with [defineEmbed](#defineembed) to create custom embeds.
200
+ Embeds are TypeScript modules that generate content
201
+ for markers in your documents.
202
+
203
+ #### Example
204
+
205
+ ```typescript
206
+ import { defineEmbed } from 'embedoc';
207
+
208
+ export default defineEmbed({
209
+ // Declare datasource dependencies for incremental builds
210
+ dependsOn: ['metadata_db'],
211
+
212
+ // Render function generates the content
213
+ async render(ctx) {
214
+ const { id } = ctx.params;
215
+ const data = await ctx.datasources['metadata_db'].query(
216
+ 'SELECT * FROM users WHERE id = ?',
217
+ [id]
218
+ );
219
+ return {
220
+ content: ctx.markdown.table(['Name', 'Email'], data.map(r => [r.name, r.email]))
221
+ };
222
+ }
223
+ });
224
+ ```
225
+
226
+ #### Properties
227
+
228
+ | Property | Type | Description | Defined in |
229
+ | ------ | ------ | ------ | ------ |
230
+ | <a id="dependson"></a> `dependsOn?` | `string`[] | List of datasource names this embed depends on. Used for dependency tracking in incremental builds. When a datasource changes, all documents using embeds that depend on it will be rebuilt. **Example** `dependsOn: ['metadata_db', 'api_endpoints']` | types/index.ts:603 |
231
+
232
+ #### Methods
233
+
234
+ ##### render()
235
+
236
+ ```ts
237
+ render(ctx: EmbedContext): Promise<EmbedResult>;
238
+ ```
239
+
240
+ Defined in: types/index.ts:614
241
+
242
+ Render function that generates the embed content.
243
+
244
+ Called for each marker in the document that references this embed.
245
+ Receives the context object with parameters, datasources, and helpers.
246
+
247
+ ###### Parameters
248
+
249
+ | Parameter | Type | Description |
250
+ | ------ | ------ | ------ |
251
+ | `ctx` | [`EmbedContext`](#embedcontext) | The embed context |
252
+
253
+ ###### Returns
254
+
255
+ `Promise`\<[`EmbedResult`](#embedresult)\>
256
+
257
+ Promise resolving to the embed result with generated content
258
+
259
+ ***
260
+
261
+ ### EmbedResult
262
+
263
+ Defined in: types/index.ts:551
264
+
265
+ Result object returned from an embed's render function.
266
+
267
+ #### Example
268
+
269
+ ```typescript
270
+ export default defineEmbed({
271
+ async render(ctx): Promise<EmbedResult> {
272
+ return { content: '# Generated Content\n\nHello, World!' };
273
+ }
274
+ });
275
+ ```
276
+
277
+ #### Properties
278
+
279
+ | Property | Type | Description | Defined in |
280
+ | ------ | ------ | ------ | ------ |
281
+ | <a id="content"></a> `content` | `string` | Generated content to insert between the markers. This string replaces the existing content between the start and end markers in the document. | types/index.ts:558 |
282
+
283
+ ***
284
+
285
+ ### MarkdownHelper
286
+
287
+ Defined in: types/index.ts:283
288
+
289
+ Helper interface for generating Markdown content.
290
+
291
+ Always available via `ctx.markdown` in embed render functions.
292
+ Provides methods for creating common Markdown elements:
293
+
294
+ #### Example
295
+
296
+ ```typescript
297
+ export default defineEmbed({
298
+ async render(ctx) {
299
+ const { markdown } = ctx;
300
+
301
+ // Create a table
302
+ const table = markdown.table(
303
+ ['Name', 'Age'],
304
+ [['Alice', 25], ['Bob', 30]]
305
+ );
306
+
307
+ // Create a list
308
+ const list = markdown.list(['Item 1', 'Item 2'], false);
309
+
310
+ return { content: table + '\n\n' + list };
311
+ }
312
+ });
313
+ ```
314
+
315
+ #### Methods
316
+
317
+ ##### bold()
318
+
319
+ ```ts
320
+ bold(text: string): string;
321
+ ```
322
+
323
+ Defined in: types/index.ts:395
324
+
325
+ Wrap text in bold formatting.
326
+
327
+ ###### Parameters
328
+
329
+ | Parameter | Type | Description |
330
+ | ------ | ------ | ------ |
331
+ | `text` | `string` | Text to make bold |
332
+
333
+ ###### Returns
334
+
335
+ `string`
336
+
337
+ Bold formatted string
338
+
339
+ ###### Example
340
+
341
+ ```typescript
342
+ ctx.markdown.bold('Important');
343
+ // Output: **Important**
344
+ ```
345
+
346
+ ##### checkbox()
347
+
348
+ ```ts
349
+ checkbox(checked: boolean): string;
350
+ ```
351
+
352
+ Defined in: types/index.ts:423
353
+
354
+ Generate a checkbox character.
355
+
356
+ ###### Parameters
357
+
358
+ | Parameter | Type | Description |
359
+ | ------ | ------ | ------ |
360
+ | `checked` | `boolean` | Whether the checkbox is checked |
361
+
362
+ ###### Returns
363
+
364
+ `string`
365
+
366
+ Checkbox character ('✔' if checked, '' if not)
367
+
368
+ ###### Example
369
+
370
+ ```typescript
371
+ ctx.markdown.checkbox(true); // Output: ✔
372
+ ctx.markdown.checkbox(false); // Output: (empty string)
373
+ ```
374
+
375
+ ##### codeBlock()
376
+
377
+ ```ts
378
+ codeBlock(code: string, language?: string): string;
379
+ ```
380
+
381
+ Defined in: types/index.ts:351
382
+
383
+ Generate a fenced code block.
384
+
385
+ ###### Parameters
386
+
387
+ | Parameter | Type | Description |
388
+ | ------ | ------ | ------ |
389
+ | `code` | `string` | The code content |
390
+ | `language?` | `string` | Optional language identifier for syntax highlighting |
391
+
392
+ ###### Returns
393
+
394
+ `string`
395
+
396
+ Formatted Markdown code block string
397
+
398
+ ###### Example
399
+
400
+ ```typescript
401
+ ctx.markdown.codeBlock('const x = 1;', 'typescript');
402
+ // Output:
403
+ // ```typescript
404
+ // const x = 1;
405
+ // ```
406
+ ```
407
+
408
+ ##### heading()
409
+
410
+ ```ts
411
+ heading(text: string, level?: number): string;
412
+ ```
413
+
414
+ Defined in: types/index.ts:381
415
+
416
+ Generate a Markdown heading.
417
+
418
+ ###### Parameters
419
+
420
+ | Parameter | Type | Description |
421
+ | ------ | ------ | ------ |
422
+ | `text` | `string` | Heading text |
423
+ | `level?` | `number` | Heading level (1-6), defaults to 1 |
424
+
425
+ ###### Returns
426
+
427
+ `string`
428
+
429
+ Formatted Markdown heading string
430
+
431
+ ###### Example
432
+
433
+ ```typescript
434
+ ctx.markdown.heading('Section Title', 2);
435
+ // Output: ## Section Title
436
+ ```
437
+
438
+ ##### italic()
439
+
440
+ ```ts
441
+ italic(text: string): string;
442
+ ```
443
+
444
+ Defined in: types/index.ts:409
445
+
446
+ Wrap text in italic formatting.
447
+
448
+ ###### Parameters
449
+
450
+ | Parameter | Type | Description |
451
+ | ------ | ------ | ------ |
452
+ | `text` | `string` | Text to make italic |
453
+
454
+ ###### Returns
455
+
456
+ `string`
457
+
458
+ Italic formatted string
459
+
460
+ ###### Example
461
+
462
+ ```typescript
463
+ ctx.markdown.italic('Emphasis');
464
+ // Output: *Emphasis*
465
+ ```
466
+
467
+ ##### link()
468
+
469
+ ```ts
470
+ link(text: string, url: string): string;
471
+ ```
472
+
473
+ Defined in: types/index.ts:366
474
+
475
+ Generate a Markdown link.
476
+
477
+ ###### Parameters
478
+
479
+ | Parameter | Type | Description |
480
+ | ------ | ------ | ------ |
481
+ | `text` | `string` | Link display text |
482
+ | `url` | `string` | Link URL |
483
+
484
+ ###### Returns
485
+
486
+ `string`
487
+
488
+ Formatted Markdown link string
489
+
490
+ ###### Example
491
+
492
+ ```typescript
493
+ ctx.markdown.link('Visit Google', 'https://google.com');
494
+ // Output: [Visit Google](https://google.com)
495
+ ```
496
+
497
+ ##### list()
498
+
499
+ ```ts
500
+ list(items: string[], ordered?: boolean): string;
501
+ ```
502
+
503
+ Defined in: types/index.ts:333
504
+
505
+ Generate a Markdown list.
506
+
507
+ ###### Parameters
508
+
509
+ | Parameter | Type | Description |
510
+ | ------ | ------ | ------ |
511
+ | `items` | `string`[] | Array of list item strings |
512
+ | `ordered?` | `boolean` | If true, creates numbered list; if false, creates bullet list |
513
+
514
+ ###### Returns
515
+
516
+ `string`
517
+
518
+ Formatted Markdown list string
519
+
520
+ ###### Example
521
+
522
+ ```typescript
523
+ // Unordered list
524
+ ctx.markdown.list(['Apple', 'Banana', 'Cherry'], false);
525
+ // Output:
526
+ // - Apple
527
+ // - Banana
528
+ // - Cherry
529
+
530
+ // Ordered list
531
+ ctx.markdown.list(['First', 'Second', 'Third'], true);
532
+ // Output:
533
+ // 1. First
534
+ // 2. Second
535
+ // 3. Third
536
+ ```
537
+
538
+ ##### table()
539
+
540
+ ```ts
541
+ table(headers: string[], rows: (string | number | boolean | null | undefined)[][]): string;
542
+ ```
543
+
544
+ Defined in: types/index.ts:307
545
+
546
+ Generate a Markdown table.
547
+
548
+ ###### Parameters
549
+
550
+ | Parameter | Type | Description |
551
+ | ------ | ------ | ------ |
552
+ | `headers` | `string`[] | Array of column header strings |
553
+ | `rows` | (`string` \| `number` \| `boolean` \| `null` \| `undefined`)[][] | 2D array of cell values (each inner array is a row) |
554
+
555
+ ###### Returns
556
+
557
+ `string`
558
+
559
+ Formatted Markdown table string
560
+
561
+ ###### Example
562
+
563
+ ```typescript
564
+ ctx.markdown.table(
565
+ ['Column', 'Type', 'Description'],
566
+ [
567
+ ['id', 'integer', 'Primary key'],
568
+ ['name', 'varchar', 'User name'],
569
+ ]
570
+ );
571
+ // Output:
572
+ // | Column | Type | Description |
573
+ // | --- | --- | --- |
574
+ // | id | integer | Primary key |
575
+ // | name | varchar | User name |
576
+ ```
577
+
578
+ ## Type Aliases
579
+
580
+ ### QueryResult
581
+
582
+ ```ts
583
+ type QueryResult = Record<string, unknown>[];
584
+ ```
585
+
586
+ Defined in: types/index.ts:149
587
+
588
+ Query result returned from datasource operations.
589
+
590
+ An array of records, where each record is an object with string keys
591
+ and unknown values. Use type assertions when accessing specific fields.
592
+
593
+ #### Example
594
+
595
+ ```typescript
596
+ const users = await ctx.datasources.db.query('SELECT * FROM users');
597
+ users.forEach(user => {
598
+ console.log(user['name'] as string);
599
+ console.log(user['age'] as number);
600
+ });
601
+ ```
602
+
603
+ ## Variables
604
+
605
+ ### defineEmbed
606
+
607
+ ```ts
608
+ const defineEmbed: DefineEmbedFn;
609
+ ```
610
+
611
+ Defined in: index.ts:166
612
+
613
+ Define a custom embed for generating content in documents.
614
+
615
+ Embeds are TypeScript modules that generate content for markers
616
+ in your documents. Use this function to create type-safe embed definitions.
617
+
618
+ #### Param
619
+
620
+ The embed definition object
621
+
622
+ #### Returns
623
+
624
+ The same definition object (for type inference)
625
+
626
+ #### Examples
627
+
628
+ ```typescript
629
+ import { defineEmbed } from 'embedoc';
630
+
631
+ export default defineEmbed({
632
+ dependsOn: ['metadata_db'],
633
+ async render(ctx) {
634
+ const { id } = ctx.params;
635
+ const data = await ctx.datasources['metadata_db'].query(
636
+ 'SELECT * FROM tables WHERE name = ?',
637
+ [id]
638
+ );
639
+ return { content: ctx.markdown.table(['Name'], data.map(r => [r.name])) };
640
+ }
641
+ });
642
+ ```
643
+
644
+ ```typescript
645
+ import { defineEmbed } from 'embedoc';
646
+
647
+ export default defineEmbed({
648
+ dependsOn: ['users_db', 'config'],
649
+ async render(ctx) {
650
+ const users = await ctx.datasources['users_db'].getAll();
651
+ const config = await ctx.datasources['config'].getAll();
652
+
653
+ return {
654
+ content: ctx.markdown.table(
655
+ ['Name', 'Role'],
656
+ users.map(u => [u.name, u.role])
657
+ )
658
+ };
659
+ }
660
+ });
661
+ ```
662
+
663
+ ```typescript
664
+ import { defineEmbed } from 'embedoc';
665
+
666
+ export default defineEmbed({
667
+ async render(ctx) {
668
+ // Access marker parameters: <!--@embedoc:my_embed title="Hello"-->
669
+ const { title } = ctx.params;
670
+
671
+ // Access document frontmatter
672
+ const author = ctx.frontmatter['author'] as string;
673
+
674
+ return {
675
+ content: ctx.markdown.heading(title, 2) + '\n\nBy ' + author
676
+ };
677
+ }
678
+ });
679
+ ```
680
+
681
+ #### See
682
+
683
+ - [EmbedDefinition](#embeddefinition) for the definition interface
684
+ - [EmbedContext](#embedcontext) for the context object passed to render
685
+ - [MarkdownHelper](#markdownhelper) for markdown generation utilities
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "embedoc",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "In-place document generator that auto-updates marker blocks while preserving manual edits. Supports multiple datasources (SQLite, CSV, JSON, YAML, glob), programmable TypeScript embeds, and incremental builds with dependency tracking.",
5
5
  "type": "module",
6
+ "workspaces": [
7
+ "examples"
8
+ ],
6
9
  "main": "./dist/index.js",
7
10
  "types": "./dist/index.d.ts",
8
11
  "exports": {
@@ -17,6 +20,7 @@
17
20
  },
18
21
  "files": [
19
22
  "dist",
23
+ "docs",
20
24
  "README.md",
21
25
  "LICENSE"
22
26
  ],
@@ -29,6 +33,7 @@
29
33
  "test": "vitest run",
30
34
  "test:watch": "vitest",
31
35
  "test:coverage": "vitest run --coverage",
36
+ "docs": "typedoc",
32
37
  "prepublishOnly": "npm run lint && npm run build && npm run test"
33
38
  },
34
39
  "keywords": [
@@ -82,6 +87,8 @@
82
87
  "@typescript-eslint/eslint-plugin": "^8.0.0",
83
88
  "@typescript-eslint/parser": "^8.0.0",
84
89
  "eslint": "^8.57.0",
90
+ "typedoc": "^0.28.0",
91
+ "typedoc-plugin-markdown": "^4.9.0",
85
92
  "typescript": "^5.7.2",
86
93
  "vitest": "^2.1.0"
87
94
  }