miki-template 1.3.3 → 1.3.6

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.
Files changed (101) hide show
  1. package/.github/release-notes/v1.3.1.md +55 -55
  2. package/.github/release-notes/v1.3.3.md +77 -0
  3. package/.github/workflows/ci.yml +38 -54
  4. package/.github/workflows/release.yml +106 -0
  5. package/AGENT.md +71 -71
  6. package/API_REFERENCE.md +314 -314
  7. package/CHANGELOG.md +173 -169
  8. package/CODE_OF_CONDUCT.md +14 -14
  9. package/CONTRIBUTING.md +27 -27
  10. package/README.md +342 -321
  11. package/ROADMAP.md +40 -40
  12. package/benchmarks/report.json +16 -16
  13. package/benchmarks/run.js +49 -49
  14. package/benchmarks/stress.mjs +647 -647
  15. package/benchmarks/templates/large.dtpl +7 -7
  16. package/benchmarks/templates/medium.dtpl +3 -3
  17. package/benchmarks/templates/small.dtpl +7 -7
  18. package/context/component.md +109 -109
  19. package/context/prd.md +131 -131
  20. package/context/project-structure.md +33 -33
  21. package/dir/base.html +22 -22
  22. package/dir/cmpnt.html +10 -10
  23. package/dir/footer.html +2 -2
  24. package/dir/home.html +80 -80
  25. package/dir/index.html +80 -0
  26. package/dir/navbar.html +8 -8
  27. package/docs/README.md +18 -18
  28. package/docs/advanced_usage.md +71 -71
  29. package/docs/api.md +119 -119
  30. package/docs/filters.md +708 -708
  31. package/docs/installation.md +106 -106
  32. package/docs/overview.md +57 -57
  33. package/docs/partialdef.md +70 -70
  34. package/docs/security.md +27 -27
  35. package/docs/tags.md +673 -673
  36. package/docs/usage.md +646 -646
  37. package/eslint.config.mjs +42 -42
  38. package/ex.mjs +32 -32
  39. package/miki-template-extension/.github/workflows/ci.yml +116 -116
  40. package/miki-template-extension/.vscodeignore +7 -7
  41. package/miki-template-extension/CHANGELOG.md +99 -99
  42. package/miki-template-extension/LICENSE +21 -21
  43. package/miki-template-extension/README.md +273 -273
  44. package/miki-template-extension/extension.js +1013 -1013
  45. package/miki-template-extension/icon.svg +10 -10
  46. package/miki-template-extension/package.json +280 -280
  47. package/miki-template-extension/snippets/miki-template.json +717 -717
  48. package/miki-template-extension/syntaxes/language-configuration.json +114 -114
  49. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +355 -355
  50. package/miki-template-extension/tests/grammar-tests.json +162 -162
  51. package/miki-template-extension/tests/run-grammar-tests.js +82 -82
  52. package/package.json +37 -34
  53. package/sample-app/package-lock.json +901 -0
  54. package/sample-app/package.json +9 -0
  55. package/sample-app/server.js +14 -0
  56. package/sample-app/views/index.html +1 -0
  57. package/scripts/build-vsix.js +129 -129
  58. package/scripts/build-vsix.ps1 +15 -15
  59. package/snippets/miki-template.json +177 -177
  60. package/src/asyncRender.js +20 -20
  61. package/src/cache.js +80 -80
  62. package/src/context.js +126 -126
  63. package/src/context_processors.js +48 -48
  64. package/src/esm.mjs +84 -84
  65. package/src/filters.js +975 -975
  66. package/src/i18n.js +171 -171
  67. package/src/index.js +974 -940
  68. package/src/lexer.js +114 -114
  69. package/src/libraries.js +371 -371
  70. package/src/parser.js +270 -270
  71. package/src/security.js +53 -53
  72. package/src/tags/control.js +719 -719
  73. package/src/tags/extra.js +154 -154
  74. package/src/tags/helpers.js +26 -26
  75. package/src/tags/i18n.js +256 -256
  76. package/src/tags/inheritance.js +335 -335
  77. package/src/tags/registry.js +18 -18
  78. package/src/tags/util.js +400 -400
  79. package/src/types.d.ts +107 -107
  80. package/syntaxes/language-configuration.json +26 -26
  81. package/syntaxes/miki-template.tmLanguage.json +146 -146
  82. package/tests/asyncRender.test.js +17 -17
  83. package/tests/base.html +6 -6
  84. package/tests/child.html +3 -3
  85. package/tests/context_processors.test.js +13 -13
  86. package/tests/esm.test.mjs +61 -61
  87. package/tests/filters.test.js +254 -254
  88. package/tests/include_security.test.js +9 -9
  89. package/tests/integration/README.md +32 -32
  90. package/tests/integration/features.test.cjs +1681 -1681
  91. package/tests/integration/features.test.mjs +1697 -1697
  92. package/tests/integration/templates/base.miki +6 -6
  93. package/tests/integration/templates/child.miki +6 -6
  94. package/tests/integration/templates/index.html +17 -17
  95. package/tests/lexer.test.js +45 -45
  96. package/tests/parser.test.js +57 -57
  97. package/tests/partial.html +1 -1
  98. package/tests/partialdef.test.js +79 -79
  99. package/tests/production_checks.js +57 -57
  100. package/tests/security.test.js +28 -28
  101. package/tests/tags.test.js +233 -233
package/src/index.js CHANGED
@@ -1,940 +1,974 @@
1
- /**
2
- * Django-Style Template Engine for Node.js/Express
3
- * Main entrypoint.
4
- */
5
- const fs = require('fs');
6
- const path = require('path');
7
- const { tokenize } = require('./lexer');
8
- const { Parser } = require('./parser');
9
- const { Context } = require('./context');
10
-
11
- const { registerContextProcessor, applyContextProcessors, clearContextProcessors } = require('./context_processors');
12
- const { registerFilter, getFilter } = require('./filters');
13
- const { SafeString, markSafe, isSafe, escapeHtml } = require('./security');
14
- const { getCompiled, clearCache, getParentSource, hasParentSource } = require('./cache');
15
- const { registerHelper } = require('./tags/helpers');
16
- const { registerTag, getTagRegistry } = require('./tags/registry');
17
-
18
- // Load control tags
19
- const controlTags = require('./tags/control');
20
- for (const [name, parserFn] of Object.entries(controlTags.parsers)) {
21
- registerTag(name, parserFn);
22
- }
23
-
24
- // Load inheritance tags
25
- const inheritanceTags = require('./tags/inheritance');
26
- for (const [name, parserFn] of Object.entries(inheritanceTags.parsers)) {
27
- registerTag(name, parserFn);
28
- }
29
-
30
- // Load utility tags
31
- const utilTags = require('./tags/util');
32
- for (const [name, parserFn] of Object.entries(utilTags.parsers)) {
33
- registerTag(name, parserFn);
34
- }
35
-
36
- // Load i18n tags
37
- const i18nTags = require('./tags/i18n');
38
- for (const [name, parserFn] of Object.entries(i18nTags.parsers)) {
39
- registerTag(name, parserFn);
40
- }
41
-
42
- // Load extra utility tags
43
- const extraTags = require('./tags/extra');
44
- for (const [name, parserFn] of Object.entries(extraTags.parsers)) {
45
- registerTag(name, parserFn);
46
- }
47
-
48
- // i18n module
49
- const i18n = require('./i18n');
50
-
51
- // Plugin/filter library system
52
- const libraries = require('./libraries');
53
-
54
- // Inject the registration functions so libraries can activate without
55
- // triggering a circular require. This must happen BEFORE the
56
- // auto-activation loop below.
57
- libraries.setRegistrationFunctions({
58
- registerTag,
59
- registerFilter,
60
- registerHelper
61
- });
62
-
63
- // Auto-activate the built-in libraries so their filters/tags/helpers
64
- // are available out of the box. Users can still opt out by calling
65
- // `unregisterLibrary` or by re-registering without activating.
66
- for (const libName of libraries.getLibraryNames()) {
67
- libraries.activateLibrary(libName);
68
- }
69
-
70
- /**
71
- * Render an AST recursively to resolve inheritance chain.
72
- * Async-aware: awaits Promises from any node.
73
- */
74
- function readParentSource(parentName, viewsDirs) {
75
- for (const dir of viewsDirs) {
76
- const key = dir + '\0' + parentName;
77
- if (hasParentSource(key)) {
78
- return getParentSource(key, () => null);
79
- }
80
- try {
81
- const fullPath = path.resolve(dir, parentName);
82
- const relative = path.relative(path.resolve(dir), fullPath);
83
- if (relative.startsWith('..') || path.isAbsolute(relative)) {
84
- throw new Error(`Extends tag attempted path traversal outside allowed views: '${parentName}'`);
85
- }
86
- const fileContent = fs.readFileSync(fullPath, 'utf8');
87
- // Populate the LRU cache and return the value
88
- return getParentSource(key, () => fileContent);
89
- } catch (e) {
90
- if (e.message && e.message.startsWith('Extends tag attempted path traversal')) {
91
- throw e;
92
- }
93
- }
94
- }
95
- throw new Error(`Template not found: '${parentName}' in directories ${JSON.stringify(viewsDirs)}`);
96
- }
97
-
98
- async function renderASTAsync(nodes, context) {
99
- context.parentTemplate = null;
100
- const parts = [];
101
- for (const node of nodes) {
102
- const result = node.render(context);
103
- parts.push(result instanceof Promise ? await result : result);
104
- }
105
- let output = parts.join('');
106
-
107
- if (context.parentTemplate) {
108
- const parentName = context.parentTemplate;
109
- context.parentTemplate = null;
110
-
111
- let viewsDirs = ['.'];
112
- if (context.options && context.options.settings && context.options.settings.views) {
113
- const views = context.options.settings.views;
114
- viewsDirs = Array.isArray(views) ? views : [views];
115
- } else if (context.options && context.options.views) {
116
- const views = context.options.views;
117
- viewsDirs = Array.isArray(views) ? views : [views];
118
- }
119
-
120
- const fileContent = readParentSource(parentName, viewsDirs);
121
-
122
- const parentTokens = tokenize(fileContent);
123
- const parentParser = new Parser(parentTokens, getTagRegistry());
124
- const parentNodes = parentParser.parse();
125
-
126
- if (parentParser.blocks) {
127
- for (const [name, blockList] of Object.entries(parentParser.blocks)) {
128
- if (!context.blocks[name]) {
129
- context.blocks[name] = [];
130
- }
131
- for (const blockNode of blockList) {
132
- if (!context.blocks[name].includes(blockNode)) {
133
- context.blocks[name].push(blockNode);
134
- }
135
- }
136
- }
137
- }
138
-
139
- return await renderASTAsync(parentNodes, context);
140
- }
141
-
142
- return output;
143
- }
144
-
145
- /**
146
- * Synchronous renderAST - throws if any node returns a Promise.
147
- */
148
- function renderAST(nodes, context) {
149
- context.parentTemplate = null;
150
- const output = nodes.map(node => {
151
- const result = node.render(context);
152
- if (result instanceof Promise) {
153
- throw new Error('Async node encountered during sync render. Use asyncRender() instead.');
154
- }
155
- return result;
156
- }).join('');
157
-
158
- if (context.parentTemplate) {
159
- const parentName = context.parentTemplate;
160
- context.parentTemplate = null;
161
-
162
- let viewsDirs = ['.'];
163
- if (context.options && context.options.settings && context.options.settings.views) {
164
- const views = context.options.settings.views;
165
- viewsDirs = Array.isArray(views) ? views : [views];
166
- } else if (context.options && context.options.views) {
167
- const views = context.options.views;
168
- viewsDirs = Array.isArray(views) ? views : [views];
169
- }
170
-
171
- const fileContent = readParentSource(parentName, viewsDirs);
172
-
173
- const parentTokens = tokenize(fileContent);
174
- const parentParser = new Parser(parentTokens, getTagRegistry());
175
- const parentNodes = parentParser.parse();
176
-
177
- if (parentParser.blocks) {
178
- for (const [name, blockList] of Object.entries(parentParser.blocks)) {
179
- if (!context.blocks[name]) {
180
- context.blocks[name] = [];
181
- }
182
- for (const blockNode of blockList) {
183
- if (!context.blocks[name].includes(blockNode)) {
184
- context.blocks[name].push(blockNode);
185
- }
186
- }
187
- }
188
- }
189
-
190
- return renderAST(parentNodes, context);
191
- }
192
-
193
- return output;
194
- }
195
-
196
- /**
197
- * Compiles a template string into a renderable object.
198
- */
199
- function compile(templateStr, options = {}) {
200
- return getCompiled(templateStr, options, (tmpl, opts) => {
201
- const tokens = tokenize(tmpl);
202
- const parser = new Parser(tokens, getTagRegistry());
203
- const nodes = parser.parse();
204
-
205
- // Collect partial definitions at compile time
206
- const partialDefs = {};
207
- function collectPartials(nodeList) {
208
- for (const node of nodeList) {
209
- if (node.constructor.name === 'PartialDefNode') {
210
- partialDefs[node.name] = node;
211
- }
212
- if (node.body) {
213
- collectPartials(node.body);
214
- }
215
- if (node.elifBranches) {
216
- for (const branch of node.elifBranches) {
217
- collectPartials(branch.body);
218
- }
219
- }
220
- if (node.elseBody) {
221
- collectPartials(node.elseBody);
222
- }
223
- }
224
- }
225
- collectPartials(nodes);
226
-
227
- return {
228
- render: (contextObj = {}) => {
229
- const processedContextObj = applyContextProcessors({ ...contextObj });
230
- const context = new Context(processedContextObj, opts);
231
- context.reset();
232
- if (parser.blocks) {
233
- for (const [name, blockList] of Object.entries(parser.blocks)) {
234
- context.blocks[name] = [...blockList];
235
- }
236
- }
237
- // Register partial definitions from compile-time
238
- for (const [name, partial] of Object.entries(partialDefs)) {
239
- context.registerPartial(name, partial);
240
- }
241
- return renderAST(nodes, context);
242
- },
243
- renderWith: (contextObj = {}, callOptions = {}) => {
244
- const processedContextObj = applyContextProcessors({ ...contextObj });
245
- const mergedOpts = { ...opts, ...callOptions };
246
- const context = new Context(processedContextObj, mergedOpts);
247
- context.reset();
248
- if (parser.blocks) {
249
- for (const [name, blockList] of Object.entries(parser.blocks)) {
250
- context.blocks[name] = [...blockList];
251
- }
252
- }
253
- for (const [name, partial] of Object.entries(partialDefs)) {
254
- context.registerPartial(name, partial);
255
- }
256
- return renderAST(nodes, context);
257
- },
258
- asyncRender: async (contextObj = {}) => {
259
- const processedContextObj = applyContextProcessors({ ...contextObj });
260
- const context = new Context(processedContextObj, opts);
261
- context.blocks = {};
262
- if (parser.blocks) {
263
- for (const [name, blockList] of Object.entries(parser.blocks)) {
264
- context.blocks[name] = [...blockList];
265
- }
266
- }
267
- for (const [name, partial] of Object.entries(partialDefs)) {
268
- context.registerPartial(name, partial);
269
- }
270
- return await renderASTAsync(nodes, context);
271
- },
272
- asyncRenderWith: async (contextObj = {}, callOptions = {}) => {
273
- const processedContextObj = applyContextProcessors({ ...contextObj });
274
- const mergedOpts = { ...opts, ...callOptions };
275
- const context = new Context(processedContextObj, mergedOpts);
276
- context.blocks = {};
277
- if (parser.blocks) {
278
- for (const [name, blockList] of Object.entries(parser.blocks)) {
279
- context.blocks[name] = [...blockList];
280
- }
281
- }
282
- for (const [name, partial] of Object.entries(partialDefs)) {
283
- context.registerPartial(name, partial);
284
- }
285
- return await renderASTAsync(nodes, context);
286
- },
287
- renderBlock: (blockName, contextObj = {}) => {
288
- const processedContextObj = applyContextProcessors({ ...contextObj });
289
- const context = new Context(processedContextObj, opts);
290
- context.blocks = {};
291
- if (parser.blocks) {
292
- for (const [name, blockList] of Object.entries(parser.blocks)) {
293
- context.blocks[name] = [...blockList];
294
- }
295
- }
296
- for (const [name, partial] of Object.entries(partialDefs)) {
297
- context.registerPartial(name, partial);
298
- }
299
- renderAST(nodes, context);
300
- const blockStack = context.blocks[blockName];
301
- if (!blockStack || blockStack.length === 0) {
302
- throw new Error(`Block '${blockName}' not found in template`);
303
- }
304
- if (!context.blockRenderIndices) {
305
- context.blockRenderIndices = {};
306
- }
307
- context.blockRenderIndices[blockName] = 0;
308
- let superVal = '';
309
- if (blockStack.length > 1) {
310
- context.blockRenderIndices[blockName] = 1;
311
- superVal = blockStack[1].render(context);
312
- }
313
- context.push({ block: { super: superVal } });
314
- context.blockRenderIndices[blockName] = 0;
315
- const result = blockStack[0].body.map(n => n.render(context)).join('');
316
- context.pop();
317
- context.blockRenderIndices[blockName] = -1;
318
- return result;
319
- },
320
- renderPartial: (partialName, contextObj = {}) => {
321
- const processedContextObj = applyContextProcessors({ ...contextObj });
322
- const context = new Context(processedContextObj, opts);
323
- for (const [name, partial] of Object.entries(partialDefs)) {
324
- context.registerPartial(name, partial);
325
- }
326
- const partial = context.getPartial(partialName);
327
- if (!partial) {
328
- throw new Error(`Partial '${partialName}' not found`);
329
- }
330
- return partial.body.map(n => n.render(context)).join('');
331
- }
332
- };
333
- });
334
- }
335
-
336
- /**
337
- * Convenience rendering function.
338
- *
339
- * If `templateStr` looks like a file path with a `#partialName` suffix
340
- * (e.g. `"home.html#card"`) AND `options.views` is set, the file is
341
- * loaded from the views dir(s) and only the named partial is rendered.
342
- * This is the engine's idiomatic way to serve HTMX partial responses.
343
- *
344
- * If the string contains template syntax (`{{` or `{%`), it is always
345
- * treated as a template string (not a file path), so existing call
346
- * sites that pass template source are unaffected.
347
- *
348
- * Without a `#` suffix, the whole template is rendered (existing
349
- * behavior).
350
- */
351
- function render(templateStr, contextObj = {}, options = {}) {
352
- // Only treat the input as a file path when:
353
- // 1. It has a `#` and a partial name
354
- // 2. options.views is configured
355
- // 3. The string does NOT contain template syntax
356
- const hashIdx = templateStr.indexOf('#');
357
- const hasTemplateSyntax = /\{[{%]/.test(templateStr);
358
- if (hashIdx >= 0 && !hasTemplateSyntax && options && options.views) {
359
- const fileName = templateStr.slice(0, hashIdx);
360
- const partialName = templateStr.slice(hashIdx + 1);
361
- return renderPartialFromFile(fileName, partialName, contextObj, options);
362
- }
363
-
364
- const compiled = compile(templateStr, options);
365
- if (options && Object.keys(options).length > 0) {
366
- return compiled.renderWith(contextObj, options);
367
- }
368
- return compiled.render(contextObj);
369
- }
370
-
371
- /**
372
- * Load a template file from the configured views dir(s), register its
373
- * partials, and render only the named partial. This is the
374
- * implementation behind `render("file.html#partial", ...)`.
375
- */
376
- function renderPartialFromFile(fileName, partialName, contextObj, options) {
377
- const { Context } = require('./context');
378
- const { tokenize } = require('./lexer');
379
- const { Parser } = require('./parser');
380
- const { getTagRegistry } = require('./tags/registry');
381
- const { applyContextProcessors } = require('./context_processors');
382
-
383
- let viewsDirs = ['.'];
384
- if (options.settings && options.settings.views) {
385
- viewsDirs = Array.isArray(options.settings.views) ? options.settings.views : [options.settings.views];
386
- } else if (options.views) {
387
- viewsDirs = Array.isArray(options.views) ? options.views : [options.views];
388
- }
389
-
390
- let fileContent = null;
391
- let loaded = false;
392
- // Try the literal name first, then with each supported extension appended.
393
- const candidates = [];
394
- if (/\.[a-z0-9]+$/i.test(fileName)) {
395
- candidates.push(fileName);
396
- } else {
397
- candidates.push(fileName + '.html', fileName + '.miki');
398
- }
399
- for (const dir of viewsDirs) {
400
- for (const candidate of candidates) {
401
- try {
402
- const fullPath = path.resolve(dir, candidate);
403
- const relative = path.relative(path.resolve(dir), fullPath);
404
- if (relative.startsWith('..') || path.isAbsolute(relative)) {
405
- continue;
406
- }
407
- fileContent = fs.readFileSync(fullPath, 'utf8');
408
- loaded = true;
409
- break;
410
- } catch {
411
- // try next candidate
412
- }
413
- }
414
- if (loaded) break;
415
- }
416
- if (!loaded) {
417
- throw new Error(
418
- `Template not found: '${fileName}' in directories ${JSON.stringify(viewsDirs)}`
419
- );
420
- }
421
-
422
- const tokens = tokenize(fileContent);
423
- const parser = new Parser(tokens, getTagRegistry());
424
- const nodes = parser.parse();
425
-
426
- // Build a context to register the partials. We share the partial
427
- // registry with a fresh context for the actual render.
428
- const tempContext = new Context({}, options);
429
- for (const node of nodes) {
430
- node.render(tempContext);
431
- }
432
- const partial = tempContext.getPartial(partialName);
433
- if (!partial) {
434
- throw new Error(
435
- `Partial '${partialName}' not found in template '${fileName}'`
436
- );
437
- }
438
-
439
- const processedContextObj = applyContextProcessors({ ...contextObj });
440
- const context = new Context(processedContextObj, options);
441
- context.partialDefs = tempContext.partialDefs;
442
- return partial.body.map(n => n.render(context)).join('');
443
- }
444
-
445
- /**
446
- * Async rendering function – returns a Promise.
447
- */
448
- function asyncRender(templateStr, contextObj = {}, options = {}) {
449
- const hashIdx = templateStr.indexOf('#');
450
- const hasTemplateSyntax = /\{[{%]/.test(templateStr);
451
- if (hashIdx >= 0 && !hasTemplateSyntax && options && options.views) {
452
- const fileName = templateStr.slice(0, hashIdx);
453
- const partialName = templateStr.slice(hashIdx + 1);
454
- return new Promise((resolve, reject) => {
455
- try {
456
- resolve(renderPartialFromFile(fileName, partialName, contextObj, options));
457
- } catch (e) {
458
- reject(e);
459
- }
460
- });
461
- }
462
- const compiled = compile(templateStr, options);
463
- if (options && Object.keys(options).length > 0) {
464
- return compiled.asyncRenderWith(contextObj, options);
465
- }
466
- return compiled.asyncRender(contextObj);
467
- }
468
-
469
- /**
470
- * Express adapter engine (synchronous callback form).
471
- * Strips Express framework keys from the context so they don't leak
472
- * into the template scope.
473
- *
474
- * If `filePath` contains a `#partialName` suffix, only the named
475
- * partial is rendered. This enables HTMX-style partial responses:
476
- *
477
- * res.render('home#card', { ... });
478
- */
479
- function __express(filePath, options, callback) {
480
- if (typeof callback !== 'function') {
481
- return __expressAsync(filePath, options);
482
- }
483
-
484
- // Detect "file#partial" form
485
- const hashIdx = filePath.lastIndexOf('#');
486
- if (hashIdx > 0) {
487
- const realFilePath = filePath.slice(0, hashIdx);
488
- const partialName = filePath.slice(hashIdx + 1);
489
- try {
490
- const fileContent = fs.readFileSync(realFilePath, 'utf8');
491
- const renderOptions = {
492
- views: realFilePath,
493
- ...(options || {})
494
- };
495
- const ctx = stripExpressContext(options);
496
- const result = renderPartialFromSource(fileContent, partialName, ctx, renderOptions, realFilePath);
497
- return callback(null, result);
498
- } catch (err) {
499
- return callback(err);
500
- }
501
- }
502
-
503
- try {
504
- const fileContent = fs.readFileSync(filePath, 'utf8');
505
- const renderOptions = {
506
- views: options && options.settings ? options.settings.views : path.dirname(filePath),
507
- ...(options || {})
508
- };
509
- // Strip Express framework keys from the context
510
- const ctx = stripExpressContext(options);
511
- const result = render(fileContent, ctx, renderOptions);
512
- return callback(null, result);
513
- } catch (err) {
514
- return callback(err);
515
- }
516
- }
517
-
518
- /**
519
- * Compile a template source string and render only the named partial
520
- * from it. Used by __express when a view name carries a `#partial`
521
- * suffix.
522
- *
523
- * The challenge: when a template uses `{% extends 'parent' %}`,
524
- * the partials are typically defined inside `{% block ... %}` tags.
525
- * The top-level nodes are [ExtendsNode, BlockNode, ...], not the
526
- * blocks themselves. We need to register all partials regardless of
527
- * whether their enclosing for/if blocks have data to iterate.
528
- */
529
- function renderPartialFromSource(fileContent, partialName, contextObj, options, filePath) {
530
- const { Context } = require('./context');
531
- const { tokenize } = require('./lexer');
532
- const { Parser } = require('./parser');
533
- const { getTagRegistry } = require('./tags/registry');
534
- const { applyContextProcessors } = require('./context_processors');
535
-
536
- const tokens = tokenize(fileContent);
537
- const parser = new Parser(tokens, getTagRegistry());
538
- const nodes = parser.parse();
539
-
540
- // Build a context with the caller's data so for-loops and other
541
- // constructs iterate properly when collecting partials.
542
- const processedContextObj = applyContextProcessors({ ...contextObj });
543
- const tempContext = new Context(processedContextObj, options);
544
-
545
- // Walk the ENTIRE AST and register every PartialDefNode we find,
546
- // regardless of whether its enclosing for/if has data. This ensures
547
- // partials are always available by name even when the caller
548
- // didn't provide the data the template would need to render them
549
- // in context.
550
- function registerAllPartials(nodeList) {
551
- for (const node of nodeList) {
552
- if (node.constructor.name === 'PartialDefNode') {
553
- tempContext.registerPartial(node.name, node);
554
- }
555
- if (node.body) registerAllPartials(node.body);
556
- if (node.elifBranches) for (const b of node.elifBranches) registerAllPartials(b.body);
557
- if (node.elseBody) registerAllPartials(node.elseBody);
558
- }
559
- }
560
- registerAllPartials(nodes);
561
-
562
- // Also try rendering top-level nodes so partials defined via
563
- // {% load %} or other dynamic mechanisms get a chance to register.
564
- // Errors are swallowed — we only care about partial registration.
565
- for (const node of nodes) {
566
- try {
567
- node.render(tempContext);
568
- } catch {
569
- // Ignore
570
- }
571
- }
572
-
573
- // If the template extends a parent, also collect partials from
574
- // the parent. This handles the common case where partials are
575
- // defined inside blocks that are part of an extended template.
576
- if (tempContext.parentTemplate) {
577
- try {
578
- const parentName = tempContext.parentTemplate;
579
- let viewsDirs = ['.'];
580
- if (options && options.settings && options.settings.views) {
581
- const v = options.settings.views;
582
- viewsDirs = Array.isArray(v) ? v : [v];
583
- } else if (options && options.views) {
584
- const v = options.views;
585
- viewsDirs = Array.isArray(v) ? v : [v];
586
- }
587
- for (const dir of viewsDirs) {
588
- const parentPath = require('path').resolve(dir, parentName);
589
- const relative = require('path').relative(require('path').resolve(dir), parentPath);
590
- if (relative.startsWith('..') || require('path').isAbsolute(relative)) continue;
591
- if (!require('fs').existsSync(parentPath)) continue;
592
- const parentContent = require('fs').readFileSync(parentPath, 'utf8');
593
- const pTokens = tokenize(parentContent);
594
- const pParser = new Parser(pTokens, getTagRegistry());
595
- const pNodes = pParser.parse();
596
- registerAllPartials(pNodes);
597
- for (const pNode of pNodes) {
598
- try { pNode.render(tempContext); } catch {}
599
- }
600
- break;
601
- }
602
- } catch {
603
- // Ignore parent resolution errors
604
- }
605
- }
606
-
607
- const partial = tempContext.getPartial(partialName);
608
- if (!partial) {
609
- throw new Error(
610
- `Partial '${partialName}' not found in template '${filePath || 'inline'}'`
611
- );
612
- }
613
-
614
- // Build a fresh context for the actual render using the caller's
615
- // data. Share the partial registry from the temp context.
616
- const context = new Context(processedContextObj, options);
617
- context.partialDefs = tempContext.partialDefs;
618
- return partial.body.map(n => n.render(context)).join('');
619
- }
620
-
621
- /**
622
- * Strip Express-specific framework keys from a context object.
623
- * Internal keys (those starting with `_`), `settings`, and `cache` are removed.
624
- */
625
- function stripExpressContext(options) {
626
- if (!options) return {};
627
- const ctx = {};
628
- for (const [k, v] of Object.entries(options)) {
629
- if (!k.startsWith('_') && k !== 'settings' && k !== 'cache') {
630
- ctx[k] = v;
631
- }
632
- }
633
- return ctx;
634
- }
635
-
636
- /**
637
- * Async view engine for Express 5+. Returns a Promise that resolves
638
- * to the rendered HTML. Use this when your templates have async helpers.
639
- *
640
- * app.engine('html', miki.__expressAsync);
641
- */
642
- function __expressAsync(filePath, options) {
643
- return new Promise((resolve, reject) => {
644
- // Detect "file#partial" form
645
- const hashIdx = filePath.lastIndexOf('#');
646
- if (hashIdx > 0) {
647
- const realFilePath = filePath.slice(0, hashIdx);
648
- const partialName = filePath.slice(hashIdx + 1);
649
- try {
650
- const fileContent = fs.readFileSync(realFilePath, 'utf8');
651
- const renderOptions = {
652
- views: realFilePath,
653
- ...(options || {})
654
- };
655
- const ctx = stripExpressContext(options);
656
- resolve(renderPartialFromSource(fileContent, partialName, ctx, renderOptions, realFilePath));
657
- } catch (err) {
658
- reject(err);
659
- }
660
- return;
661
- }
662
-
663
- try {
664
- const fileContent = fs.readFileSync(filePath, 'utf8');
665
- const renderOptions = {
666
- views: options && options.settings ? options.settings.views : path.dirname(filePath),
667
- ...(options || {})
668
- };
669
- const ctx = stripExpressContext(options);
670
- // Use asyncRender so async helpers are awaited
671
- asyncRender(fileContent, ctx, renderOptions)
672
- .then(resolve)
673
- .catch(reject);
674
- } catch (err) {
675
- reject(err);
676
- }
677
- });
678
- }
679
-
680
- /**
681
- * Express integration helper. Returns a function suitable for
682
- * `app.engine(ext, fn)` that makes `res.render('view#partial', ...)`
683
- * just work without any extra middleware.
684
- *
685
- * Usage:
686
- *
687
- * const miki = require('miki-template');
688
- * const app = express();
689
- * app.engine('html', miki.express());
690
- * app.set('view engine', 'html');
691
- * app.set('views', './views');
692
- *
693
- * // Full page:
694
- * app.get('/', (req, res) => res.render('home', { user }));
695
- *
696
- * // HTMX partial — just one named partial from a template:
697
- * app.get('/partials/:name', (req, res) =>
698
- * res.render(`home#${req.params.name}`, { user })
699
- * );
700
- *
701
- * The wrapper detects a `#partialName` suffix in the view name BEFORE
702
- * Express's view lookup runs, so it never tries to open a file like
703
- * `home#card.html`. It resolves the real file, calls the partial
704
- * renderer, and sends the result.
705
- */
706
- function express(options = {}) {
707
- const baseEngine = options.async ? __expressAsync : __express;
708
- return function mikiViewEngine(filePath, engineOptions, callback) {
709
- // Detect the partial selector in the view name. Express passes
710
- // the resolved file path here — if the user wrote
711
- // `res.render('home#card', ...)`, Express will have already
712
- // tried (and failed) to resolve `home#card.html`. To support
713
- // partials, we need to intercept BEFORE Express resolves the
714
- // view. We do that by hooking `res.render` when this engine is
715
- // installed.
716
- // For the direct path (when called from `res.renderPartial` or
717
- // from our own `res.render` shim), we honor the `#partial`
718
- // suffix here.
719
- if (typeof callback !== 'function') {
720
- return Promise.reject(
721
- new Error('miki.express() engine must be called via res.render() with a callback')
722
- );
723
- }
724
- return baseEngine(filePath, engineOptions, callback);
725
- };
726
- }
727
-
728
- /**
729
- * One-shot Express setup. Wires `app.engine('html', ...)` and
730
- * installs a `res.render` shim so that `res.render('view#partial')`
731
- * works without any extra middleware. This is the recommended way
732
- * to integrate miki-template with Express.
733
- *
734
- * Usage:
735
- *
736
- * const miki = require('miki-template');
737
- * const app = express();
738
- * miki.setupExpress(app, { extension: 'html', views: './views' });
739
- *
740
- * app.get('/partials/:name', (req, res) =>
741
- * res.render(`home#${req.params.name}`, { user: req.user })
742
- * );
743
- */
744
- function setupExpress(app, opts = {}) {
745
- const ext = (opts.extension || 'html').replace(/^\.+/, '');
746
- const async = !!opts.async;
747
-
748
- // Set view engine if not already set
749
- if (!app.get('view engine')) {
750
- app.set('view engine', ext);
751
- }
752
- // If opts.views is provided, always set it (so users can set
753
- // views via setupExpress without an extra app.set call).
754
- if (opts.views) {
755
- app.set('views', opts.views);
756
- }
757
-
758
- // Install the raw engine so Express can use it
759
- app.engine(ext, async ? __expressAsync : __express);
760
-
761
- // Capture config in a closure so patchedRender can use it even
762
- // when called before the request handler runs.
763
- const configExt = ext;
764
- const configViews = opts.views;
765
-
766
- // Capture the original res.render so we can dispatch on #partial
767
- const originalRender = app.response.render;
768
- app.response.render = function patchedRender(view, locals, callback) {
769
- // Normalize arguments: (view, callback) or (view, locals, callback)
770
- let cb = callback;
771
- let opts = locals;
772
- if (typeof locals === 'function') {
773
- cb = locals;
774
- opts = {};
775
- }
776
- opts = opts || {};
777
- // Inject settings so the engine can find the views dir
778
- if (!opts.settings) {
779
- opts.settings = this.req && this.req.app ? this.req.app.settings : {};
780
- }
781
-
782
- // If the view name has a `#partial` suffix, handle it ourselves
783
- // and never delegate to Express's view lookup.
784
- if (typeof view === 'string' && view.includes('#')) {
785
- const hashIdx = view.lastIndexOf('#');
786
- const fileName = view.slice(0, hashIdx);
787
- const partialName = view.slice(hashIdx + 1);
788
-
789
- // Resolve the real file path. Prefer opts.views (set by
790
- // setupExpress), then app.get('views'), then the default.
791
- const extname = require('path').extname(fileName);
792
- const candidates = extname
793
- ? [fileName]
794
- : [fileName + '.' + configExt, fileName + '.miki'];
795
- const viewsDir = configViews
796
- || (this.req && this.req.app ? this.req.app.get('views') : null)
797
- || process.cwd() + '/views';
798
- let filePath = null;
799
- for (const cand of candidates) {
800
- const p = require('path').resolve(viewsDir, cand);
801
- if (require('fs').existsSync(p)) {
802
- filePath = p;
803
- break;
804
- }
805
- }
806
- if (!filePath) {
807
- const err = new Error(
808
- `Failed to lookup view "${view}" in views directory "${viewsDir}"`
809
- );
810
- if (typeof cb === 'function') return cb(err);
811
- throw err;
812
- }
813
-
814
- const fileContent = require('fs').readFileSync(filePath, 'utf8');
815
- try {
816
- const html = renderPartialFromSource(
817
- fileContent,
818
- partialName,
819
- stripExpressContext(opts),
820
- Object.assign({ views: viewsDir }, opts),
821
- fileName
822
- );
823
- if (typeof cb === 'function') {
824
- return cb(null, html);
825
- }
826
- this.send(html);
827
- return;
828
- } catch (e) {
829
- if (typeof cb === 'function') return cb(e);
830
- throw e;
831
- }
832
- }
833
-
834
- // No partial selector: behave exactly like the original res.render
835
- if (cb) {
836
- return originalRender.call(this, view, opts, cb);
837
- }
838
- return originalRender.call(this, view, opts);
839
- };
840
- }
841
-
842
- /**
843
- * Express middleware helper. Adds a `res.renderPartial(view, locals)`
844
- * method that renders only the named partial (after a `#`) from a
845
- * view file. The view name follows the same syntax as `render()`:
846
- *
847
- * app.use(miki.expressPartialRenderer());
848
- * app.get('/card', (req, res) => res.renderPartial('home#card', { user }));
849
- */
850
- function expressPartialRenderer() {
851
- return function (req, res, next) {
852
- res.renderPartial = function (view, locals = {}) {
853
- // Compose an Express-shaped options object so the engine can
854
- // find the view file. We mirror what res.render provides.
855
- const opts = Object.assign({}, res.locals, locals, {
856
- settings: req.app.settings
857
- });
858
- const hashIdx = view.lastIndexOf('#');
859
- if (hashIdx < 0) {
860
- // No partial selector: just delegate to res.render
861
- return res.render(view, locals);
862
- }
863
- const fileName = view.slice(0, hashIdx);
864
- const partialName = view.slice(hashIdx + 1);
865
- // Find the actual file the way Express would
866
- const ext = require('path').extname(fileName);
867
- const candidates = ext
868
- ? [fileName]
869
- : [fileName + '.html', fileName + '.miki'];
870
- const viewsDir = req.app.get('views');
871
- let filePath = null;
872
- for (const cand of candidates) {
873
- try {
874
- filePath = require('path').resolve(viewsDir, cand);
875
- if (require('fs').existsSync(filePath)) break;
876
- filePath = null;
877
- } catch { filePath = null; }
878
- }
879
- if (!filePath) {
880
- return res.status(404).send(
881
- `Template not found: '${fileName}' in '${viewsDir}'`
882
- );
883
- }
884
- try {
885
- const html = renderPartialFromSource(
886
- require('fs').readFileSync(filePath, 'utf8'),
887
- partialName,
888
- stripExpressContext(opts),
889
- { views: viewsDir, ...opts },
890
- fileName
891
- );
892
- res.send(html);
893
- } catch (err) {
894
- res.status(500).send(err.message);
895
- }
896
- };
897
- next();
898
- };
899
- }
900
-
901
- module.exports = {
902
- compile,
903
- render,
904
- asyncRender,
905
- renderPartialFromFile,
906
- renderPartialFromSource,
907
- __express,
908
- __expressAsync,
909
- express,
910
- setupExpress,
911
- expressPartialRenderer,
912
- stripExpressContext,
913
- clearCache,
914
- registerTag,
915
- registerFilter,
916
- getFilter,
917
- registerHelper,
918
- registerContextProcessor,
919
- clearContextProcessors,
920
- SafeString,
921
- markSafe,
922
- isSafe,
923
- escapeHtml,
924
- // i18n
925
- registerTranslation: i18n.registerTranslation,
926
- unregisterTranslation: i18n.unregisterTranslation,
927
- setLanguage: i18n.setLanguage,
928
- getLanguage: i18n.getLanguage,
929
- setFallbackLanguage: i18n.setFallbackLanguage,
930
- getFallbackLanguage: i18n.getFallbackLanguage,
931
- getAvailableLanguages: i18n.getAvailableLanguages,
932
- // Plugin/filter libraries
933
- registerLibrary: libraries.registerLibrary,
934
- unregisterLibrary: libraries.unregisterLibrary,
935
- getLibrary: libraries.getLibrary,
936
- getLibraryNames: libraries.getLibraryNames,
937
- hasLibrary: libraries.hasLibrary,
938
- registerLibraryFromPath: libraries.registerLibraryFromPath,
939
- activateLibrary: libraries.activateLibrary
940
- };
1
+ /**
2
+ * Django-Style Template Engine for Node.js/Express
3
+ * Main entrypoint.
4
+ */
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const { tokenize } = require('./lexer');
8
+ const { Parser } = require('./parser');
9
+ const { Context } = require('./context');
10
+
11
+ const { registerContextProcessor, applyContextProcessors, clearContextProcessors } = require('./context_processors');
12
+ const { registerFilter, getFilter } = require('./filters');
13
+ const { SafeString, markSafe, isSafe, escapeHtml } = require('./security');
14
+ const { getCompiled, clearCache, getParentSource, hasParentSource } = require('./cache');
15
+ const { registerHelper } = require('./tags/helpers');
16
+ const { registerTag, getTagRegistry } = require('./tags/registry');
17
+
18
+ // Load control tags
19
+ const controlTags = require('./tags/control');
20
+ for (const [name, parserFn] of Object.entries(controlTags.parsers)) {
21
+ registerTag(name, parserFn);
22
+ }
23
+
24
+ // Load inheritance tags
25
+ const inheritanceTags = require('./tags/inheritance');
26
+ for (const [name, parserFn] of Object.entries(inheritanceTags.parsers)) {
27
+ registerTag(name, parserFn);
28
+ }
29
+
30
+ // Load utility tags
31
+ const utilTags = require('./tags/util');
32
+ for (const [name, parserFn] of Object.entries(utilTags.parsers)) {
33
+ registerTag(name, parserFn);
34
+ }
35
+
36
+ // Load i18n tags
37
+ const i18nTags = require('./tags/i18n');
38
+ for (const [name, parserFn] of Object.entries(i18nTags.parsers)) {
39
+ registerTag(name, parserFn);
40
+ }
41
+
42
+ // Load extra utility tags
43
+ const extraTags = require('./tags/extra');
44
+ for (const [name, parserFn] of Object.entries(extraTags.parsers)) {
45
+ registerTag(name, parserFn);
46
+ }
47
+
48
+ // i18n module
49
+ const i18n = require('./i18n');
50
+
51
+ // Plugin/filter library system
52
+ const libraries = require('./libraries');
53
+
54
+ // Normalize views entries: if a views entry points to a file,
55
+ // return its directory so lookups work regardless of whether the
56
+ // caller passed a file path or a directory.
57
+ function normalizeViews(views) {
58
+ if (!views) return ['.'];
59
+ const arr = Array.isArray(views) ? views.slice() : [views];
60
+ return arr.map(v => {
61
+ try {
62
+ const st = fs.statSync(v);
63
+ if (st.isFile()) return path.dirname(v);
64
+ return v;
65
+ } catch (e) {
66
+ // If the path doesn't exist or can't be stat'd, return as-is.
67
+ return v;
68
+ }
69
+ });
70
+ }
71
+
72
+ // Inject the registration functions so libraries can activate without
73
+ // triggering a circular require. This must happen BEFORE the
74
+ // auto-activation loop below.
75
+ libraries.setRegistrationFunctions({
76
+ registerTag,
77
+ registerFilter,
78
+ registerHelper
79
+ });
80
+
81
+ // Auto-activate the built-in libraries so their filters/tags/helpers
82
+ // are available out of the box. Users can still opt out by calling
83
+ // `unregisterLibrary` or by re-registering without activating.
84
+ for (const libName of libraries.getLibraryNames()) {
85
+ libraries.activateLibrary(libName);
86
+ }
87
+
88
+ /**
89
+ * Render an AST recursively to resolve inheritance chain.
90
+ * Async-aware: awaits Promises from any node.
91
+ */
92
+ function readParentSource(parentName, viewsDirs) {
93
+ for (const dir of viewsDirs) {
94
+ const key = dir + '\0' + parentName;
95
+ if (hasParentSource(key)) {
96
+ return getParentSource(key, () => null);
97
+ }
98
+ try {
99
+ const fullPath = path.resolve(dir, parentName);
100
+ const relative = path.relative(path.resolve(dir), fullPath);
101
+ if (relative.startsWith('..') || path.isAbsolute(relative)) {
102
+ throw new Error(`Extends tag attempted path traversal outside allowed views: '${parentName}'`);
103
+ }
104
+ const fileContent = fs.readFileSync(fullPath, 'utf8');
105
+ // Populate the LRU cache and return the value
106
+ return getParentSource(key, () => fileContent);
107
+ } catch (e) {
108
+ if (e.message && e.message.startsWith('Extends tag attempted path traversal')) {
109
+ throw e;
110
+ }
111
+ }
112
+ }
113
+ throw new Error(`Template not found: '${parentName}' in directories ${JSON.stringify(viewsDirs)}`);
114
+ }
115
+
116
+ async function renderASTAsync(nodes, context) {
117
+ context.parentTemplate = null;
118
+ const parts = [];
119
+ for (const node of nodes) {
120
+ const result = node.render(context);
121
+ parts.push(result instanceof Promise ? await result : result);
122
+ }
123
+ let output = parts.join('');
124
+
125
+ if (context.parentTemplate) {
126
+ const parentName = context.parentTemplate;
127
+ context.parentTemplate = null;
128
+
129
+ let viewsDirs = ['.'];
130
+ if (context.options && context.options.settings && context.options.settings.views) {
131
+ viewsDirs = normalizeViews(context.options.settings.views);
132
+ } else if (context.options && context.options.views) {
133
+ viewsDirs = normalizeViews(context.options.views);
134
+ }
135
+
136
+ const fileContent = readParentSource(parentName, viewsDirs);
137
+
138
+ const parentTokens = tokenize(fileContent);
139
+ const parentParser = new Parser(parentTokens, getTagRegistry());
140
+ const parentNodes = parentParser.parse();
141
+
142
+ if (parentParser.blocks) {
143
+ for (const [name, blockList] of Object.entries(parentParser.blocks)) {
144
+ if (!context.blocks[name]) {
145
+ context.blocks[name] = [];
146
+ }
147
+ for (const blockNode of blockList) {
148
+ if (!context.blocks[name].includes(blockNode)) {
149
+ context.blocks[name].push(blockNode);
150
+ }
151
+ }
152
+ }
153
+ }
154
+
155
+ return await renderASTAsync(parentNodes, context);
156
+ }
157
+
158
+ return output;
159
+ }
160
+
161
+ /**
162
+ * Synchronous renderAST - throws if any node returns a Promise.
163
+ */
164
+ function renderAST(nodes, context) {
165
+ context.parentTemplate = null;
166
+ const output = nodes.map(node => {
167
+ const result = node.render(context);
168
+ if (result instanceof Promise) {
169
+ throw new Error('Async node encountered during sync render. Use asyncRender() instead.');
170
+ }
171
+ return result;
172
+ }).join('');
173
+
174
+ if (context.parentTemplate) {
175
+ const parentName = context.parentTemplate;
176
+ context.parentTemplate = null;
177
+
178
+ let viewsDirs = ['.'];
179
+ if (context.options && context.options.settings && context.options.settings.views) {
180
+ viewsDirs = normalizeViews(context.options.settings.views);
181
+ } else if (context.options && context.options.views) {
182
+ viewsDirs = normalizeViews(context.options.views);
183
+ }
184
+
185
+ const fileContent = readParentSource(parentName, viewsDirs);
186
+
187
+ const parentTokens = tokenize(fileContent);
188
+ const parentParser = new Parser(parentTokens, getTagRegistry());
189
+ const parentNodes = parentParser.parse();
190
+
191
+ if (parentParser.blocks) {
192
+ for (const [name, blockList] of Object.entries(parentParser.blocks)) {
193
+ if (!context.blocks[name]) {
194
+ context.blocks[name] = [];
195
+ }
196
+ for (const blockNode of blockList) {
197
+ if (!context.blocks[name].includes(blockNode)) {
198
+ context.blocks[name].push(blockNode);
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ return renderAST(parentNodes, context);
205
+ }
206
+
207
+ return output;
208
+ }
209
+
210
+ /**
211
+ * Compiles a template string into a renderable object.
212
+ */
213
+ function compile(templateStr, options = {}) {
214
+ return getCompiled(templateStr, options, (tmpl, opts) => {
215
+ const tokens = tokenize(tmpl);
216
+ const parser = new Parser(tokens, getTagRegistry());
217
+ const nodes = parser.parse();
218
+
219
+ // Collect partial definitions at compile time
220
+ const partialDefs = {};
221
+ function collectPartials(nodeList) {
222
+ for (const node of nodeList) {
223
+ if (node.constructor.name === 'PartialDefNode') {
224
+ partialDefs[node.name] = node;
225
+ }
226
+ if (node.body) {
227
+ collectPartials(node.body);
228
+ }
229
+ if (node.elifBranches) {
230
+ for (const branch of node.elifBranches) {
231
+ collectPartials(branch.body);
232
+ }
233
+ }
234
+ if (node.elseBody) {
235
+ collectPartials(node.elseBody);
236
+ }
237
+ }
238
+ }
239
+ collectPartials(nodes);
240
+
241
+ return {
242
+ render: (contextObj = {}) => {
243
+ const processedContextObj = applyContextProcessors({ ...contextObj });
244
+ const context = new Context(processedContextObj, opts);
245
+ context.reset();
246
+ if (parser.blocks) {
247
+ for (const [name, blockList] of Object.entries(parser.blocks)) {
248
+ context.blocks[name] = [...blockList];
249
+ }
250
+ }
251
+ // Register partial definitions from compile-time
252
+ for (const [name, partial] of Object.entries(partialDefs)) {
253
+ context.registerPartial(name, partial);
254
+ }
255
+ return renderAST(nodes, context);
256
+ },
257
+ renderWith: (contextObj = {}, callOptions = {}) => {
258
+ const processedContextObj = applyContextProcessors({ ...contextObj });
259
+ const mergedOpts = { ...opts, ...callOptions };
260
+ const context = new Context(processedContextObj, mergedOpts);
261
+ context.reset();
262
+ if (parser.blocks) {
263
+ for (const [name, blockList] of Object.entries(parser.blocks)) {
264
+ context.blocks[name] = [...blockList];
265
+ }
266
+ }
267
+ for (const [name, partial] of Object.entries(partialDefs)) {
268
+ context.registerPartial(name, partial);
269
+ }
270
+ return renderAST(nodes, context);
271
+ },
272
+ asyncRender: async (contextObj = {}) => {
273
+ const processedContextObj = applyContextProcessors({ ...contextObj });
274
+ const context = new Context(processedContextObj, opts);
275
+ context.blocks = {};
276
+ if (parser.blocks) {
277
+ for (const [name, blockList] of Object.entries(parser.blocks)) {
278
+ context.blocks[name] = [...blockList];
279
+ }
280
+ }
281
+ for (const [name, partial] of Object.entries(partialDefs)) {
282
+ context.registerPartial(name, partial);
283
+ }
284
+ return await renderASTAsync(nodes, context);
285
+ },
286
+ asyncRenderWith: async (contextObj = {}, callOptions = {}) => {
287
+ const processedContextObj = applyContextProcessors({ ...contextObj });
288
+ const mergedOpts = { ...opts, ...callOptions };
289
+ const context = new Context(processedContextObj, mergedOpts);
290
+ context.blocks = {};
291
+ if (parser.blocks) {
292
+ for (const [name, blockList] of Object.entries(parser.blocks)) {
293
+ context.blocks[name] = [...blockList];
294
+ }
295
+ }
296
+ for (const [name, partial] of Object.entries(partialDefs)) {
297
+ context.registerPartial(name, partial);
298
+ }
299
+ return await renderASTAsync(nodes, context);
300
+ },
301
+ renderBlock: (blockName, contextObj = {}) => {
302
+ const processedContextObj = applyContextProcessors({ ...contextObj });
303
+ const context = new Context(processedContextObj, opts);
304
+ context.blocks = {};
305
+ if (parser.blocks) {
306
+ for (const [name, blockList] of Object.entries(parser.blocks)) {
307
+ context.blocks[name] = [...blockList];
308
+ }
309
+ }
310
+ for (const [name, partial] of Object.entries(partialDefs)) {
311
+ context.registerPartial(name, partial);
312
+ }
313
+ renderAST(nodes, context);
314
+ const blockStack = context.blocks[blockName];
315
+ if (!blockStack || blockStack.length === 0) {
316
+ throw new Error(`Block '${blockName}' not found in template`);
317
+ }
318
+ if (!context.blockRenderIndices) {
319
+ context.blockRenderIndices = {};
320
+ }
321
+ context.blockRenderIndices[blockName] = 0;
322
+ let superVal = '';
323
+ if (blockStack.length > 1) {
324
+ context.blockRenderIndices[blockName] = 1;
325
+ superVal = blockStack[1].render(context);
326
+ }
327
+ context.push({ block: { super: superVal } });
328
+ context.blockRenderIndices[blockName] = 0;
329
+ const result = blockStack[0].body.map(n => n.render(context)).join('');
330
+ context.pop();
331
+ context.blockRenderIndices[blockName] = -1;
332
+ return result;
333
+ },
334
+ renderPartial: (partialName, contextObj = {}) => {
335
+ const processedContextObj = applyContextProcessors({ ...contextObj });
336
+ const context = new Context(processedContextObj, opts);
337
+ for (const [name, partial] of Object.entries(partialDefs)) {
338
+ context.registerPartial(name, partial);
339
+ }
340
+ const partial = context.getPartial(partialName);
341
+ if (!partial) {
342
+ throw new Error(`Partial '${partialName}' not found`);
343
+ }
344
+ return partial.body.map(n => n.render(context)).join('');
345
+ }
346
+ };
347
+ });
348
+ }
349
+
350
+ /**
351
+ * Convenience rendering function.
352
+ *
353
+ * If `templateStr` looks like a file path with a `#partialName` suffix
354
+ * (e.g. `"home.html#card"`) AND `options.views` is set, the file is
355
+ * loaded from the views dir(s) and only the named partial is rendered.
356
+ * This is the engine's idiomatic way to serve HTMX partial responses.
357
+ *
358
+ * If the string contains template syntax (`{{` or `{%`), it is always
359
+ * treated as a template string (not a file path), so existing call
360
+ * sites that pass template source are unaffected.
361
+ *
362
+ * Without a `#` suffix, the whole template is rendered (existing
363
+ * behavior).
364
+ */
365
+ function render(templateStr, contextObj = {}, options = {}) {
366
+ // Only treat the input as a file path when:
367
+ // 1. It has a `#` and a partial name
368
+ // 2. options.views is configured
369
+ // 3. The string does NOT contain template syntax
370
+ const hashIdx = templateStr.indexOf('#');
371
+ const hasTemplateSyntax = /\{[{%]/.test(templateStr);
372
+ if (hashIdx >= 0 && !hasTemplateSyntax && options && options.views) {
373
+ const fileName = templateStr.slice(0, hashIdx);
374
+ const partialName = templateStr.slice(hashIdx + 1);
375
+ return renderPartialFromFile(fileName, partialName, contextObj, options);
376
+ }
377
+
378
+ const compiled = compile(templateStr, options);
379
+ if (options && Object.keys(options).length > 0) {
380
+ return compiled.renderWith(contextObj, options);
381
+ }
382
+ return compiled.render(contextObj);
383
+ }
384
+
385
+ /**
386
+ * Load a template file from the configured views dir(s), register its
387
+ * partials, and render only the named partial. This is the
388
+ * implementation behind `render("file.html#partial", ...)`.
389
+ */
390
+ function renderPartialFromFile(fileName, partialName, contextObj, options) {
391
+ const { Context } = require('./context');
392
+ const { tokenize } = require('./lexer');
393
+ const { Parser } = require('./parser');
394
+ const { getTagRegistry } = require('./tags/registry');
395
+ const { applyContextProcessors } = require('./context_processors');
396
+
397
+ let viewsDirs = ['.'];
398
+ if (options.settings && options.settings.views) {
399
+ viewsDirs = normalizeViews(options.settings.views);
400
+ } else if (options.views) {
401
+ viewsDirs = normalizeViews(options.views);
402
+ }
403
+
404
+ let fileContent = null;
405
+ let loaded = false;
406
+ // Try the literal name first, then with each supported extension appended.
407
+ const candidates = [];
408
+ if (/\.[a-z0-9]+$/i.test(fileName)) {
409
+ candidates.push(fileName);
410
+ } else {
411
+ candidates.push(fileName + '.html', fileName + '.miki');
412
+ }
413
+ for (const dir of viewsDirs) {
414
+ for (const candidate of candidates) {
415
+ try {
416
+ const fullPath = path.resolve(dir, candidate);
417
+ const relative = path.relative(path.resolve(dir), fullPath);
418
+ if (relative.startsWith('..') || path.isAbsolute(relative)) {
419
+ continue;
420
+ }
421
+ fileContent = fs.readFileSync(fullPath, 'utf8');
422
+ loaded = true;
423
+ break;
424
+ } catch {
425
+ // try next candidate
426
+ }
427
+ }
428
+ if (loaded) break;
429
+ }
430
+ if (!loaded) {
431
+ throw new Error(
432
+ `Template not found: '${fileName}' in directories ${JSON.stringify(viewsDirs)}`
433
+ );
434
+ }
435
+
436
+ const tokens = tokenize(fileContent);
437
+ const parser = new Parser(tokens, getTagRegistry());
438
+ const nodes = parser.parse();
439
+
440
+ // Build a context to register the partials. We share the partial
441
+ // registry with a fresh context for the actual render.
442
+ const tempContext = new Context({}, options);
443
+ for (const node of nodes) {
444
+ node.render(tempContext);
445
+ }
446
+ const partial = tempContext.getPartial(partialName);
447
+ if (!partial) {
448
+ throw new Error(
449
+ `Partial '${partialName}' not found in template '${fileName}'`
450
+ );
451
+ }
452
+
453
+ const processedContextObj = applyContextProcessors({ ...contextObj });
454
+ const context = new Context(processedContextObj, options);
455
+ context.partialDefs = tempContext.partialDefs;
456
+ return partial.body.map(n => n.render(context)).join('');
457
+ }
458
+
459
+ /**
460
+ * Async rendering function – returns a Promise.
461
+ */
462
+ function asyncRender(templateStr, contextObj = {}, options = {}) {
463
+ const hashIdx = templateStr.indexOf('#');
464
+ const hasTemplateSyntax = /\{[{%]/.test(templateStr);
465
+ if (hashIdx >= 0 && !hasTemplateSyntax && options && options.views) {
466
+ const fileName = templateStr.slice(0, hashIdx);
467
+ const partialName = templateStr.slice(hashIdx + 1);
468
+ return new Promise((resolve, reject) => {
469
+ try {
470
+ resolve(renderPartialFromFile(fileName, partialName, contextObj, options));
471
+ } catch (e) {
472
+ reject(e);
473
+ }
474
+ });
475
+ }
476
+ const compiled = compile(templateStr, options);
477
+ if (options && Object.keys(options).length > 0) {
478
+ return compiled.asyncRenderWith(contextObj, options);
479
+ }
480
+ return compiled.asyncRender(contextObj);
481
+ }
482
+
483
+ /**
484
+ * Express adapter engine (synchronous callback form).
485
+ * Strips Express framework keys from the context so they don't leak
486
+ * into the template scope.
487
+ *
488
+ * If `filePath` contains a `#partialName` suffix, only the named
489
+ * partial is rendered. This enables HTMX-style partial responses:
490
+ *
491
+ * res.render('home#card', { ... });
492
+ */
493
+ function __express(filePath, options, callback) {
494
+ if (typeof callback !== 'function') {
495
+ return __expressAsync(filePath, options);
496
+ }
497
+
498
+ // Detect "file#partial" form
499
+ const hashIdx = filePath.lastIndexOf('#');
500
+ if (hashIdx > 0) {
501
+ const realFilePath = filePath.slice(0, hashIdx);
502
+ const partialName = filePath.slice(hashIdx + 1);
503
+ try {
504
+ const fileContent = fs.readFileSync(realFilePath, 'utf8');
505
+ const renderOptions = {
506
+ views: path.dirname(realFilePath),
507
+ ...(options || {})
508
+ };
509
+ const ctx = stripExpressContext(options);
510
+ const result = renderPartialFromSource(fileContent, partialName, ctx, renderOptions, realFilePath);
511
+ return callback(null, result);
512
+ } catch (err) {
513
+ return callback(err);
514
+ }
515
+ }
516
+
517
+ try {
518
+ const fileContent = fs.readFileSync(filePath, 'utf8');
519
+ const renderOptions = {
520
+ views: options && options.settings ? options.settings.views : path.dirname(filePath),
521
+ ...(options || {})
522
+ };
523
+ // Strip Express framework keys from the context
524
+ const ctx = stripExpressContext(options);
525
+ const result = render(fileContent, ctx, renderOptions);
526
+ return callback(null, result);
527
+ } catch (err) {
528
+ return callback(err);
529
+ }
530
+ }
531
+
532
+ /**
533
+ * Compile a template source string and render only the named partial
534
+ * from it. Used by __express when a view name carries a `#partial`
535
+ * suffix.
536
+ *
537
+ * The challenge: when a template uses `{% extends 'parent' %}`,
538
+ * the partials are typically defined inside `{% block ... %}` tags.
539
+ * The top-level nodes are [ExtendsNode, BlockNode, ...], not the
540
+ * blocks themselves. We need to register all partials regardless of
541
+ * whether their enclosing for/if blocks have data to iterate.
542
+ */
543
+ function renderPartialFromSource(fileContent, partialName, contextObj, options, filePath) {
544
+ const { Context } = require('./context');
545
+ const { tokenize } = require('./lexer');
546
+ const { Parser } = require('./parser');
547
+ const { getTagRegistry } = require('./tags/registry');
548
+ const { applyContextProcessors } = require('./context_processors');
549
+
550
+ const tokens = tokenize(fileContent);
551
+ const parser = new Parser(tokens, getTagRegistry());
552
+ const nodes = parser.parse();
553
+
554
+ // Build a context with the caller's data so for-loops and other
555
+ // constructs iterate properly when collecting partials.
556
+ const processedContextObj = applyContextProcessors({ ...contextObj });
557
+ const tempContext = new Context(processedContextObj, options);
558
+
559
+ // Walk the ENTIRE AST and register every PartialDefNode we find,
560
+ // regardless of whether its enclosing for/if has data. This ensures
561
+ // partials are always available by name even when the caller
562
+ // didn't provide the data the template would need to render them
563
+ // in context.
564
+ function registerAllPartials(nodeList) {
565
+ for (const node of nodeList) {
566
+ if (node.constructor.name === 'PartialDefNode') {
567
+ tempContext.registerPartial(node.name, node);
568
+ }
569
+ if (node.body) registerAllPartials(node.body);
570
+ if (node.elifBranches) for (const b of node.elifBranches) registerAllPartials(b.body);
571
+ if (node.elseBody) registerAllPartials(node.elseBody);
572
+ }
573
+ }
574
+ registerAllPartials(nodes);
575
+
576
+ // Also try rendering top-level nodes so partials defined via
577
+ // {% load %} or other dynamic mechanisms get a chance to register.
578
+ // Errors are swallowed — we only care about partial registration.
579
+ for (const node of nodes) {
580
+ try {
581
+ node.render(tempContext);
582
+ } catch {
583
+ // Ignore
584
+ }
585
+ }
586
+
587
+ // If the template extends a parent, also collect partials from
588
+ // the parent. This handles the common case where partials are
589
+ // defined inside blocks that are part of an extended template.
590
+ if (tempContext.parentTemplate) {
591
+ try {
592
+ const parentName = tempContext.parentTemplate;
593
+ let viewsDirs = ['.'];
594
+ if (options && options.settings && options.settings.views) {
595
+ viewsDirs = normalizeViews(options.settings.views);
596
+ } else if (options && options.views) {
597
+ viewsDirs = normalizeViews(options.views);
598
+ }
599
+ for (const dir of viewsDirs) {
600
+ const parentPath = require('path').resolve(dir, parentName);
601
+ const relative = require('path').relative(require('path').resolve(dir), parentPath);
602
+ if (relative.startsWith('..') || require('path').isAbsolute(relative)) continue;
603
+ if (!require('fs').existsSync(parentPath)) continue;
604
+ const parentContent = require('fs').readFileSync(parentPath, 'utf8');
605
+ const pTokens = tokenize(parentContent);
606
+ const pParser = new Parser(pTokens, getTagRegistry());
607
+ const pNodes = pParser.parse();
608
+ registerAllPartials(pNodes);
609
+ for (const pNode of pNodes) {
610
+ try { pNode.render(tempContext); } catch {}
611
+ }
612
+ break;
613
+ }
614
+ } catch {
615
+ // Ignore parent resolution errors
616
+ }
617
+ }
618
+
619
+ const partial = tempContext.getPartial(partialName);
620
+ if (!partial) {
621
+ throw new Error(
622
+ `Partial '${partialName}' not found in template '${filePath || 'inline'}'`
623
+ );
624
+ }
625
+
626
+ // Build a fresh context for the actual render using the caller's
627
+ // data. Share the partial registry from the temp context.
628
+ const context = new Context(processedContextObj, options);
629
+ context.partialDefs = tempContext.partialDefs;
630
+ return partial.body.map(n => n.render(context)).join('');
631
+ }
632
+
633
+ /**
634
+ * Strip Express-specific framework keys from a context object.
635
+ * Internal keys (those starting with `_`), `settings`, and `cache` are removed.
636
+ */
637
+ function stripExpressContext(options) {
638
+ if (!options) return {};
639
+ const ctx = {};
640
+ for (const [k, v] of Object.entries(options)) {
641
+ if (!k.startsWith('_') && k !== 'settings' && k !== 'cache') {
642
+ ctx[k] = v;
643
+ }
644
+ }
645
+ return ctx;
646
+ }
647
+
648
+ /**
649
+ * Async view engine for Express 5+. Returns a Promise that resolves
650
+ * to the rendered HTML. Use this when your templates have async helpers.
651
+ *
652
+ * app.engine('html', miki.__expressAsync);
653
+ */
654
+ function __expressAsync(filePath, options) {
655
+ return new Promise((resolve, reject) => {
656
+ // Detect "file#partial" form
657
+ const hashIdx = filePath.lastIndexOf('#');
658
+ if (hashIdx > 0) {
659
+ const realFilePath = filePath.slice(0, hashIdx);
660
+ const partialName = filePath.slice(hashIdx + 1);
661
+ try {
662
+ const fileContent = fs.readFileSync(realFilePath, 'utf8');
663
+ const renderOptions = {
664
+ views: path.dirname(realFilePath),
665
+ ...(options || {})
666
+ };
667
+ const ctx = stripExpressContext(options);
668
+ resolve(renderPartialFromSource(fileContent, partialName, ctx, renderOptions, realFilePath));
669
+ } catch (err) {
670
+ reject(err);
671
+ }
672
+ return;
673
+ }
674
+
675
+ try {
676
+ const fileContent = fs.readFileSync(filePath, 'utf8');
677
+ const renderOptions = {
678
+ views: options && options.settings ? options.settings.views : path.dirname(filePath),
679
+ ...(options || {})
680
+ };
681
+ const ctx = stripExpressContext(options);
682
+ // Use asyncRender so async helpers are awaited
683
+ asyncRender(fileContent, ctx, renderOptions)
684
+ .then(resolve)
685
+ .catch(reject);
686
+ } catch (err) {
687
+ reject(err);
688
+ }
689
+ });
690
+ }
691
+
692
+ /**
693
+ * Express integration helper. Returns a function suitable for
694
+ * `app.engine(ext, fn)` that makes `res.render('view#partial', ...)`
695
+ * just work without any extra middleware.
696
+ *
697
+ * Usage:
698
+ *
699
+ * const miki = require('miki-template');
700
+ * const app = express();
701
+ * app.engine('html', miki.express());
702
+ * app.set('view engine', 'html');
703
+ * app.set('views', './views');
704
+ *
705
+ * // Full page:
706
+ * app.get('/', (req, res) => res.render('home', { user }));
707
+ *
708
+ * // HTMX partial — just one named partial from a template:
709
+ * app.get('/partials/:name', (req, res) =>
710
+ * res.render(`home#${req.params.name}`, { user })
711
+ * );
712
+ *
713
+ * The wrapper detects a `#partialName` suffix in the view name BEFORE
714
+ * Express's view lookup runs, so it never tries to open a file like
715
+ * `home#card.html`. It resolves the real file, calls the partial
716
+ * renderer, and sends the result.
717
+ */
718
+ function express(options = {}) {
719
+ const baseEngine = options.async ? __expressAsync : __express;
720
+ return function mikiViewEngine(filePath, engineOptions, callback) {
721
+ // Detect the partial selector in the view name. Express passes
722
+ // the resolved file path here — if the user wrote
723
+ // `res.render('home#card', ...)`, Express will have already
724
+ // tried (and failed) to resolve `home#card.html`. To support
725
+ // partials, we need to intercept BEFORE Express resolves the
726
+ // view. We do that by hooking `res.render` when this engine is
727
+ // installed.
728
+ // For the direct path (when called from `res.renderPartial` or
729
+ // from our own `res.render` shim), we honor the `#partial`
730
+ // suffix here.
731
+ if (typeof callback !== 'function') {
732
+ return Promise.reject(
733
+ new Error('miki.express() engine must be called via res.render() with a callback')
734
+ );
735
+ }
736
+ return baseEngine(filePath, engineOptions, callback);
737
+ };
738
+ }
739
+
740
+ /**
741
+ * One-shot Express setup. Wires `app.engine('html', ...)` and
742
+ * installs a `res.render` shim so that `res.render('view#partial')`
743
+ * works without any extra middleware. This is the recommended way
744
+ * to integrate miki-template with Express.
745
+ *
746
+ * Usage:
747
+ *
748
+ * const miki = require('miki-template');
749
+ * const app = express();
750
+ * miki.setupExpress(app, { extension: 'html', views: './views' });
751
+ *
752
+ * app.get('/partials/:name', (req, res) =>
753
+ * res.render(`home#${req.params.name}`, { user: req.user })
754
+ * );
755
+ */
756
+ function setupExpress(app, opts = {}) {
757
+ const ext = (opts.extension || 'html').replace(/^\.+/, '');
758
+ const async = !!opts.async;
759
+
760
+ // Set view engine if not already set
761
+ if (!app.get('view engine')) {
762
+ app.set('view engine', ext);
763
+ }
764
+ // If opts.views is provided, always set it (so users can set
765
+ // views via setupExpress without an extra app.set call).
766
+ if (opts.views) {
767
+ app.set('views', opts.views);
768
+ }
769
+
770
+ // Install the raw engine so Express can use it
771
+ app.engine(ext, async ? __expressAsync : __express);
772
+
773
+ // Capture config in a closure so patchedRender can use it even
774
+ // when called before the request handler runs.
775
+ const configExt = ext;
776
+ const configViews = opts.views;
777
+
778
+ // Capture the original res.render so we can dispatch on #partial
779
+ const originalRender = app.response.render;
780
+ app.response.render = function patchedRender(view, locals, callback) {
781
+ // Normalize arguments: (view, callback) or (view, locals, callback)
782
+ let cb = callback;
783
+ let opts = locals;
784
+ if (typeof locals === 'function') {
785
+ cb = locals;
786
+ opts = {};
787
+ }
788
+ opts = opts || {};
789
+ // Inject settings so the engine can find the views dir
790
+ if (!opts.settings) {
791
+ opts.settings = this.req && this.req.app ? this.req.app.settings : {};
792
+ }
793
+
794
+ // If the view name has a `#partial` suffix, handle it ourselves
795
+ // and never delegate to Express's view lookup.
796
+ if (typeof view === 'string' && view.includes('#')) {
797
+ const hashIdx = view.lastIndexOf('#');
798
+ const fileName = view.slice(0, hashIdx);
799
+ const partialName = view.slice(hashIdx + 1);
800
+
801
+ // Resolve the real file path. Prefer opts.views (set by
802
+ // setupExpress), then app.get('views'), then the default.
803
+ const extname = require('path').extname(fileName);
804
+ const candidates = extname
805
+ ? [fileName]
806
+ : [fileName + '.' + configExt, fileName + '.miki'];
807
+ let viewsDir = configViews
808
+ || (this.req && this.req.app ? this.req.app.get('views') : null)
809
+ || process.cwd() + '/views';
810
+ // Normalize arrays or accidental file paths to directories
811
+ if (Array.isArray(viewsDir)) {
812
+ const arr = normalizeViews(viewsDir);
813
+ viewsDir = arr.length > 0 ? arr[0] : viewsDir[0];
814
+ } else {
815
+ try {
816
+ const st = fs.statSync(viewsDir);
817
+ if (st.isFile()) viewsDir = path.dirname(viewsDir);
818
+ } catch (e) {
819
+ // ignore
820
+ }
821
+ }
822
+ let filePath = null;
823
+ for (const cand of candidates) {
824
+ const p = require('path').resolve(viewsDir, cand);
825
+ if (require('fs').existsSync(p)) {
826
+ filePath = p;
827
+ break;
828
+ }
829
+ }
830
+ if (!filePath) {
831
+ const err = new Error(
832
+ `Failed to lookup view "${view}" in views directory "${viewsDir}"`
833
+ );
834
+ if (typeof cb === 'function') return cb(err);
835
+ throw err;
836
+ }
837
+
838
+ const fileContent = require('fs').readFileSync(filePath, 'utf8');
839
+ try {
840
+ const html = renderPartialFromSource(
841
+ fileContent,
842
+ partialName,
843
+ stripExpressContext(opts),
844
+ Object.assign({ views: viewsDir }, opts),
845
+ fileName
846
+ );
847
+ if (typeof cb === 'function') {
848
+ return cb(null, html);
849
+ }
850
+ this.send(html);
851
+ return;
852
+ } catch (e) {
853
+ if (typeof cb === 'function') return cb(e);
854
+ throw e;
855
+ }
856
+ }
857
+
858
+ // No partial selector: behave exactly like the original res.render
859
+ if (cb) {
860
+ return originalRender.call(this, view, opts, cb);
861
+ }
862
+ return originalRender.call(this, view, opts);
863
+ };
864
+ }
865
+
866
+ /**
867
+ * Express middleware helper. Adds a `res.renderPartial(view, locals)`
868
+ * method that renders only the named partial (after a `#`) from a
869
+ * view file. The view name follows the same syntax as `render()`:
870
+ *
871
+ * app.use(miki.expressPartialRenderer());
872
+ * app.get('/card', (req, res) => res.renderPartial('home#card', { user }));
873
+ */
874
+ function expressPartialRenderer() {
875
+ return function (req, res, next) {
876
+ res.renderPartial = function (view, locals = {}) {
877
+ // Compose an Express-shaped options object so the engine can
878
+ // find the view file. We mirror what res.render provides.
879
+ const opts = Object.assign({}, res.locals, locals, {
880
+ settings: req.app.settings
881
+ });
882
+ const hashIdx = view.lastIndexOf('#');
883
+ if (hashIdx < 0) {
884
+ // No partial selector: just delegate to res.render
885
+ return res.render(view, locals);
886
+ }
887
+ const fileName = view.slice(0, hashIdx);
888
+ const partialName = view.slice(hashIdx + 1);
889
+ // Find the actual file the way Express would
890
+ const ext = require('path').extname(fileName);
891
+ const candidates = ext
892
+ ? [fileName]
893
+ : [fileName + '.html', fileName + '.miki'];
894
+ let viewsDir = req.app.get('views');
895
+ // Normalize viewsDir to a directory if needed
896
+ if (Array.isArray(viewsDir)) {
897
+ const arr = normalizeViews(viewsDir);
898
+ viewsDir = arr.length > 0 ? arr[0] : viewsDir[0];
899
+ } else {
900
+ try {
901
+ const st = fs.statSync(viewsDir);
902
+ if (st.isFile()) viewsDir = path.dirname(viewsDir);
903
+ } catch (e) {}
904
+ }
905
+ let filePath = null;
906
+ for (const cand of candidates) {
907
+ try {
908
+ filePath = require('path').resolve(viewsDir, cand);
909
+ if (require('fs').existsSync(filePath)) break;
910
+ filePath = null;
911
+ } catch { filePath = null; }
912
+ }
913
+ if (!filePath) {
914
+ return res.status(404).send(
915
+ `Template not found: '${fileName}' in '${viewsDir}'`
916
+ );
917
+ }
918
+ try {
919
+ const html = renderPartialFromSource(
920
+ require('fs').readFileSync(filePath, 'utf8'),
921
+ partialName,
922
+ stripExpressContext(opts),
923
+ { views: viewsDir, ...opts },
924
+ fileName
925
+ );
926
+ res.send(html);
927
+ } catch (err) {
928
+ res.status(500).send(err.message);
929
+ }
930
+ };
931
+ next();
932
+ };
933
+ }
934
+
935
+ module.exports = {
936
+ compile,
937
+ render,
938
+ asyncRender,
939
+ renderPartialFromFile,
940
+ renderPartialFromSource,
941
+ __express,
942
+ __expressAsync,
943
+ express,
944
+ setupExpress,
945
+ expressPartialRenderer,
946
+ stripExpressContext,
947
+ clearCache,
948
+ registerTag,
949
+ registerFilter,
950
+ getFilter,
951
+ registerHelper,
952
+ registerContextProcessor,
953
+ clearContextProcessors,
954
+ SafeString,
955
+ markSafe,
956
+ isSafe,
957
+ escapeHtml,
958
+ // i18n
959
+ registerTranslation: i18n.registerTranslation,
960
+ unregisterTranslation: i18n.unregisterTranslation,
961
+ setLanguage: i18n.setLanguage,
962
+ getLanguage: i18n.getLanguage,
963
+ setFallbackLanguage: i18n.setFallbackLanguage,
964
+ getFallbackLanguage: i18n.getFallbackLanguage,
965
+ getAvailableLanguages: i18n.getAvailableLanguages,
966
+ // Plugin/filter libraries
967
+ registerLibrary: libraries.registerLibrary,
968
+ unregisterLibrary: libraries.unregisterLibrary,
969
+ getLibrary: libraries.getLibrary,
970
+ getLibraryNames: libraries.getLibraryNames,
971
+ hasLibrary: libraries.hasLibrary,
972
+ registerLibraryFromPath: libraries.registerLibraryFromPath,
973
+ activateLibrary: libraries.activateLibrary
974
+ };