md-template-vars 0.1.2 → 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 CHANGED
@@ -83,6 +83,34 @@ project: My Project
83
83
  Welcome to My Project!
84
84
  ```
85
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
+
86
114
  ## Error Handling
87
115
 
88
116
  | Case | Behavior |
@@ -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.2",
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",