md-template-vars 0.1.0 → 0.2.0

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/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # md-template-vars
2
+
3
+ A CLI tool to replace `{{variables}}` in Markdown templates with values from a YAML file.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -g md-template-vars
9
+ ```
10
+
11
+ Or use with npx:
12
+
13
+ ```bash
14
+ npx md-template-vars ./templates ./output
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```bash
20
+ md-template-vars <input> <output> [options]
21
+ ```
22
+
23
+ ### Arguments
24
+
25
+ | Argument | Description |
26
+ | -------- | ----------------------------------------- |
27
+ | `input` | Input directory containing template files |
28
+ | `output` | Output directory for processed files |
29
+
30
+ ### Options
31
+
32
+ | Option | Default | Description |
33
+ | ----------- | ---------------- | -------------------------------------- |
34
+ | `--vars` | `variables.yaml` | Path to the variables YAML file |
35
+ | `--include` | - | Glob pattern to include specific files |
36
+ | `--exclude` | - | Glob pattern to exclude specific files |
37
+
38
+ ## Examples
39
+
40
+ ### Basic usage
41
+
42
+ ```bash
43
+ md-template-vars ./templates ./output
44
+ ```
45
+
46
+ ### Custom variables file
47
+
48
+ ```bash
49
+ md-template-vars ./templates ./output --vars production.yaml
50
+ ```
51
+
52
+ ### Filter files
53
+
54
+ ```bash
55
+ # Include only files matching pattern
56
+ md-template-vars ./templates ./output --include "api-*.md"
57
+
58
+ # Exclude files matching pattern
59
+ md-template-vars ./templates ./output --exclude "draft-*.md"
60
+ ```
61
+
62
+ ## Template Syntax
63
+
64
+ Use `{{variableName}}` syntax in your Markdown files:
65
+
66
+ **Template (templates/hello.md):**
67
+ ```markdown
68
+ # Hello {{name}}
69
+
70
+ Welcome to {{project}}!
71
+ ```
72
+
73
+ **Variables (variables.yaml):**
74
+ ```yaml
75
+ name: World
76
+ project: My Project
77
+ ```
78
+
79
+ **Output (output/hello.md):**
80
+ ```markdown
81
+ # Hello World
82
+
83
+ Welcome to My Project!
84
+ ```
85
+
86
+ ### Nested Variables
87
+
88
+ You can use nested objects in your variables file and access them with dot notation:
89
+
90
+ **Template:**
91
+ ```markdown
92
+ # {{app.name}}
93
+
94
+ Database: {{database.host}}:{{database.port}}
95
+ ```
96
+
97
+ **Variables (variables.yaml):**
98
+ ```yaml
99
+ app:
100
+ name: My App
101
+
102
+ database:
103
+ host: localhost
104
+ port: 5432
105
+ ```
106
+
107
+ **Output:**
108
+ ```markdown
109
+ # My App
110
+
111
+ Database: localhost:5432
112
+ ```
113
+
114
+ ## Error Handling
115
+
116
+ | Case | Behavior |
117
+ | --------------------------- | --------------------------------------------------- |
118
+ | Undefined variable | Warning is displayed, variable syntax is kept as-is |
119
+ | Same input/output directory | Error and exit |
120
+ | Variables file not found | Error and exit |
121
+
122
+ ## License
123
+
124
+ MIT
@@ -1,4 +1,4 @@
1
- const VARIABLE_PATTERN = /\{\{(\w+)\}\}/g;
1
+ const VARIABLE_PATTERN = /\{\{([\w.]+)\}\}/g;
2
2
  export function renderTemplate(content, variables) {
3
3
  const undefinedVariables = [];
4
4
  const renderedContent = content.replace(VARIABLE_PATTERN, (match, varName) => {
@@ -1,3 +1,9 @@
1
1
  import { z } from "zod";
2
- export declare const VariablesSchema: z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>, string, string | number | boolean>>;
2
+ declare const PrimitiveValueSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>;
3
+ type NestedValue = z.infer<typeof PrimitiveValueSchema> | {
4
+ [key: string]: NestedValue;
5
+ };
6
+ export declare const VariablesSchema: z.ZodRecord<z.ZodString, z.ZodType<NestedValue, z.ZodTypeDef, NestedValue>>;
3
7
  export type Variables = Record<string, string>;
8
+ export declare function flattenVariables(obj: Record<string, NestedValue>, prefix?: string): Variables;
9
+ export {};
@@ -1,3 +1,17 @@
1
1
  import { z } from "zod";
2
- const VariableValueSchema = z.union([z.string(), z.number(), z.boolean()]).transform(String);
3
- export const VariablesSchema = z.record(z.string(), VariableValueSchema);
2
+ const PrimitiveValueSchema = z.union([z.string(), z.number(), z.boolean()]);
3
+ const NestedValueSchema = z.lazy(() => z.union([PrimitiveValueSchema, z.record(z.string(), NestedValueSchema)]));
4
+ export const VariablesSchema = z.record(z.string(), NestedValueSchema);
5
+ export function flattenVariables(obj, prefix = "") {
6
+ const result = {};
7
+ for (const [key, value] of Object.entries(obj)) {
8
+ const fullKey = prefix ? `${prefix}.${key}` : key;
9
+ if (typeof value === "object" && value !== null) {
10
+ Object.assign(result, flattenVariables(value, fullKey));
11
+ }
12
+ else {
13
+ result[fullKey] = String(value);
14
+ }
15
+ }
16
+ return result;
17
+ }
@@ -1,6 +1,6 @@
1
1
  import { readFileSync, existsSync } from "node:fs";
2
2
  import { parse } from "yaml";
3
- import { VariablesSchema } from "../../domain/value-objects/variables.js";
3
+ import { VariablesSchema, flattenVariables } from "../../domain/value-objects/variables.js";
4
4
  import { VariablesFileNotFoundError, InvalidVariablesError } from "../../shared/errors.js";
5
5
  export function loadVariables(filePath) {
6
6
  if (!existsSync(filePath)) {
@@ -12,5 +12,5 @@ export function loadVariables(filePath) {
12
12
  if (!result.success) {
13
13
  throw new InvalidVariablesError(result.error.message);
14
14
  }
15
- return result.data;
15
+ return flattenVariables(result.data);
16
16
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "md-template-vars",
3
3
  "author": "Shunta Toda",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "description": "Replace {{variables}} in markdown templates with YAML values",
6
6
  "type": "module",
7
7
  "main": "./dist/application/use-cases/process-templates.js",