miki-template 1.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/.github/workflows/ci.yml +54 -0
- package/AGENT.md +71 -0
- package/API_REFERENCE.md +314 -0
- package/CHANGELOG.md +97 -0
- package/CODE_OF_CONDUCT.md +14 -0
- package/CONTRIBUTING.md +27 -0
- package/README.md +304 -0
- package/ROADMAP.md +40 -0
- package/benchmarks/report.json +17 -0
- package/benchmarks/run.js +49 -0
- package/benchmarks/templates/large.dtpl +7 -0
- package/benchmarks/templates/medium.dtpl +3 -0
- package/benchmarks/templates/small.dtpl +7 -0
- package/context/component.md +109 -0
- package/context/prd.md +131 -0
- package/context/project-structure.md +33 -0
- package/docs/README.md +18 -0
- package/docs/advanced_usage.md +71 -0
- package/docs/api.md +102 -0
- package/docs/filters.md +540 -0
- package/docs/installation.md +106 -0
- package/docs/overview.md +57 -0
- package/docs/partialdef.md +41 -0
- package/docs/security.md +27 -0
- package/docs/tags.md +610 -0
- package/docs/usage.md +599 -0
- package/eslint.config.mjs +34 -0
- package/miki-template-1.2.0.vsix +0 -0
- package/miki-template-extension/LICENSE +21 -0
- package/miki-template-extension/README.md +82 -0
- package/miki-template-extension/icon.png +0 -0
- package/miki-template-extension/icon.svg +10 -0
- package/miki-template-extension/package.json +46 -0
- package/miki-template-extension/snippets/miki-template.json +177 -0
- package/miki-template-extension/syntaxes/language-configuration.json +26 -0
- package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +146 -0
- package/package.json +31 -0
- package/snippets/miki-template.json +177 -0
- package/src/asyncRender.js +21 -0
- package/src/cache.js +41 -0
- package/src/context.js +122 -0
- package/src/context_processors.js +41 -0
- package/src/esm.mjs +72 -0
- package/src/filters.js +527 -0
- package/src/i18n.js +171 -0
- package/src/index.js +454 -0
- package/src/lexer.js +92 -0
- package/src/libraries.js +240 -0
- package/src/parser.js +250 -0
- package/src/security.js +51 -0
- package/src/tags/control.js +591 -0
- package/src/tags/helpers.js +27 -0
- package/src/tags/i18n.js +230 -0
- package/src/tags/inheritance.js +216 -0
- package/src/tags/registry.js +18 -0
- package/src/tags/util.js +322 -0
- package/src/types.d.ts +107 -0
- package/syntaxes/language-configuration.json +26 -0
- package/syntaxes/miki-template.tmLanguage.json +146 -0
- package/tests/asyncRender.test.js +17 -0
- package/tests/base.html +6 -0
- package/tests/child.html +3 -0
- package/tests/context_processors.test.js +13 -0
- package/tests/esm.test.mjs +26 -0
- package/tests/filters.test.js +99 -0
- package/tests/include_security.test.js +9 -0
- package/tests/lexer.test.js +45 -0
- package/tests/parser.test.js +55 -0
- package/tests/partial.html +1 -0
- package/tests/partialdef.test.js +40 -0
- package/tests/production_checks.js +57 -0
- package/tests/security.test.js +28 -0
- package/tests/tags.test.js +203 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/project-root
|
|
2
|
+
/src
|
|
3
|
+
lexer.js
|
|
4
|
+
parser.js
|
|
5
|
+
renderer.js
|
|
6
|
+
filters.js
|
|
7
|
+
security.js
|
|
8
|
+
cache.js
|
|
9
|
+
context.js
|
|
10
|
+
tags/
|
|
11
|
+
control.js
|
|
12
|
+
inheritance.js
|
|
13
|
+
util.js
|
|
14
|
+
index.js
|
|
15
|
+
/templates
|
|
16
|
+
base.html
|
|
17
|
+
home.html
|
|
18
|
+
header.html
|
|
19
|
+
/examples
|
|
20
|
+
app.js
|
|
21
|
+
/tests
|
|
22
|
+
lexer.test.js
|
|
23
|
+
parser.test.js
|
|
24
|
+
renderer.test.js
|
|
25
|
+
filters.test.js
|
|
26
|
+
tags.test.js
|
|
27
|
+
security.test.js
|
|
28
|
+
README.md
|
|
29
|
+
API_REFERENCE.md
|
|
30
|
+
ROADMAP.md
|
|
31
|
+
CONTRIBUTING.md
|
|
32
|
+
CODE_OF_CONDUCT.md
|
|
33
|
+
package.json
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Documentation Index
|
|
2
|
+
|
|
3
|
+
Welcome to the **miki-template** documentation hub. Below is a quick navigation guide to all docs.
|
|
4
|
+
|
|
5
|
+
| Document | Description |
|
|
6
|
+
|---|---|
|
|
7
|
+
| [overview.md](overview.md) | High‑level project overview and repository layout |
|
|
8
|
+
| [installation.md](installation.md) | How to install the package and prerequisites |
|
|
9
|
+
| [usage.md](usage.md) | Basic API usage, rendering, compilation, and block rendering |
|
|
10
|
+
| [tags.md](tags.md) | Reference for every built‑in tag (`if`, `for`, `partialdef`, etc.) |
|
|
11
|
+
| [filters.md](filters.md) | Comprehensive list of built‑in filters with examples |
|
|
12
|
+
| [partialdef.md](partialdef.md) | Detailed guide for the `partialdef` feature (including inline, nesting, API) |
|
|
13
|
+
| [security.md](security.md) | Security considerations (auto‑escaping, CSP, CSRF) |
|
|
14
|
+
| [api.md](api.md) | Low‑level API surface – `compile`, `renderPartial`, `registerTag`, etc. |
|
|
15
|
+
| [contributing.md](contributing.md) | Guidelines for contributing, pull‑request flow, and code style |
|
|
16
|
+
| [roadmap.md](roadmap.md) | Future milestones and release schedule |
|
|
17
|
+
|
|
18
|
+
All files are located under `c:/Users/Coder Miki/Desktop/miki-template/docs/`.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Advanced Usage
|
|
2
|
+
|
|
3
|
+
This document showcases real‑world examples of the **miki‑template** engine.
|
|
4
|
+
|
|
5
|
+
## 1. Partial Definition & Inclusion
|
|
6
|
+
```django
|
|
7
|
+
{% partialdef "header" %}
|
|
8
|
+
<header>
|
|
9
|
+
<h1>{{ site.title }}</h1>
|
|
10
|
+
</header>
|
|
11
|
+
{% endpartialdef %}
|
|
12
|
+
|
|
13
|
+
{% include "header" %}
|
|
14
|
+
```
|
|
15
|
+
*Result:* Renders a reusable header component.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
## 2. Custom Tag Helper – Markdown
|
|
19
|
+
```js
|
|
20
|
+
const { registerHelper } = require('./src/tags/helpers');
|
|
21
|
+
const markdownIt = require('markdown-it')();
|
|
22
|
+
registerHelper('markdown', (content) => markdownIt.render(content));
|
|
23
|
+
```
|
|
24
|
+
```django
|
|
25
|
+
{% markdown %}
|
|
26
|
+
# Hello World
|
|
27
|
+
* Item 1
|
|
28
|
+
* Item 2
|
|
29
|
+
{% endmarkdown %}
|
|
30
|
+
```
|
|
31
|
+
*Result:* Converts markdown to HTML on‑the‑fly.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
## 3. Async Rendering with a Sleep Helper
|
|
35
|
+
```js
|
|
36
|
+
registerHelper('sleep', async (content) => {
|
|
37
|
+
await new Promise(r => setTimeout(r, 50));
|
|
38
|
+
return `Awake after ${content}`;
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
```django
|
|
42
|
+
{% sleep %}50ms{% endsleep %}
|
|
43
|
+
```
|
|
44
|
+
```js
|
|
45
|
+
const { asyncRender } = require('./src');
|
|
46
|
+
asyncRender(template, {} ).then(console.log);
|
|
47
|
+
```
|
|
48
|
+
*Result:* `Awake after 50ms`
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
## 4. Extended Date Formatting
|
|
52
|
+
```django
|
|
53
|
+
{{ now|date_format:"yyyy-MM-dd HH:mm:ss" }}
|
|
54
|
+
```
|
|
55
|
+
*Result:* `2026-08-29 13:48:07`
|
|
56
|
+
### Additional Date Filter Examples
|
|
57
|
+
|
|
58
|
+
```django
|
|
59
|
+
{{ now|strftime:"PPpp" }} {# Full date-fns pattern #}
|
|
60
|
+
{{ now|date:"Y-m-d H:i:s" }} {# Django style with seconds #}
|
|
61
|
+
{{ now|time:"H:i:s" }} {# Time only #}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
*Result:* Example outputs will appear when rendered.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
## 5. Performance Benchmark (see `benchmarks/run.js`)
|
|
68
|
+
The benchmark renders three template sizes (small, medium, large) synchronously and asynchronously, reporting average render time and cache hit ratio.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
> **Tip:** All examples work with the default Express integration via `app.engine('dtpl', require('miki-template').__express);`
|
package/docs/api.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
This document lists the public API exported by **miki-template** for developers to integrate the engine into their projects.
|
|
4
|
+
|
|
5
|
+
| Function / Export | Signature | Description | Example |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| `compile(templateStr, options?)` | `compile(string, object?) → { render, asyncRender, renderBlock, renderPartial }` | Compiles a template string into a renderable object. Optional `options` can include `views` directories, custom tags/filters, etc. | `const tpl = compile('Hello {{ name }}');` |
|
|
8
|
+
| `render(templateStr, context?, options?)` | `render(string, object?, object?) → string` | One‑off rendering of a template string with the provided context. | `render('Hello {{ name }}', { name: 'World' });` |
|
|
9
|
+
| `asyncRender(templateStr, context?, options?)` | `asyncRender(string, object?, object?) → Promise<string>` | Asynchronous rendering (useful with async helpers). | `await asyncRender(tpl, ctx);` |
|
|
10
|
+
| `__express(filePath, options, callback)` | `__express(string, object, function)` | Express view engine adapter – reads the file at `filePath` and renders it. | `app.set('view engine', 'miki');` |
|
|
11
|
+
| `__expressAsync(filePath, options)` | `__expressAsync(string, object) → Promise<string>` | Async Express 5+ view engine adapter. Returns a Promise that resolves to rendered HTML. | `app.engine('html', miki.__expressAsync);` |
|
|
12
|
+
| `stripExpressContext(options)` | `stripExpressContext(object) → object` | Remove Express framework keys (`_locals`, `settings`, `cache`) from an options object. | `const ctx = stripExpressContext(res.locals);` |
|
|
13
|
+
| `clearCache()` | `clearCache() → void` | Clear the in-memory compiled template cache. | `clearCache();` |
|
|
14
|
+
| `registerTag(name, parserFn)` | `registerTag(string, function)` | Register a custom tag parser. Must be called before compiling templates. | `registerTag('mytag', parserFn);` |
|
|
15
|
+
| `registerFilter(name, fn)` | `registerFilter(string, function)` | Register a custom filter. | `registerFilter('reverse', str => str.split('').reverse().join(''));` |
|
|
16
|
+
| `getFilter(name)` | `getFilter(string) → function` | Retrieve a registered filter function by name. | `const upper = getFilter('upper');` |
|
|
17
|
+
| `registerHelper(name, fn)` | `registerHelper(string, function)` | Register a helper function available inside templates. | `registerHelper('upper', s => s.toUpperCase());` |
|
|
18
|
+
| `registerContextProcessor(fn)` | `registerContextProcessor(function)` | Add a context processor that mutates the rendering context before each render. | `registerContextProcessor(ctx => ({ ...ctx, csrf_token: '123' }));` |
|
|
19
|
+
| `registerTranslation(lang, messages)` | `registerTranslation(string, object) → void` | Register translation messages for a language. | `registerTranslation('fr', { 'Hello': 'Bonjour' });` |
|
|
20
|
+
| `setLanguage(lang)` | `setLanguage(string) → void` | Set the active language for all subsequent renders. | `setLanguage('fr');` |
|
|
21
|
+
| `getLanguage()` | `getLanguage() → string` | Get the currently active language. | `const lang = getLanguage();` |
|
|
22
|
+
| `setFallbackLanguage(lang)` | `setFallbackLanguage(string) → void` | Set the fallback language for missing translations. | `setFallbackLanguage('en');` |
|
|
23
|
+
| `getAvailableLanguages()` | `getAvailableLanguages() → string[]` | List all registered language codes. | `const langs = getAvailableLanguages();` |
|
|
24
|
+
| `registerLibrary(name, def)` | `registerLibrary(string, object) → void` | Register a plugin library of tags, filters, and helpers. | `registerLibrary('humanize', { filters: { intcomma } });` |
|
|
25
|
+
| `registerLibraryFromPath(name, path)` | `registerLibraryFromPath(string, string) → object` | Load a library from a JS file on disk. | `registerLibraryFromPath('myLib', './libs/my-lib.js');` |
|
|
26
|
+
| `activateLibrary(name)` | `activateLibrary(string) → void` | Activate a registered library (makes its tags/filters available). | `activateLibrary('humanize');` |
|
|
27
|
+
| `SafeString` | `class SafeString` | Wrapper class for values that should bypass auto‑escaping. Returned by `markSafe`. |
|
|
28
|
+
| `markSafe(value)` | `markSafe(any) → SafeString` | Marks a value as safe, preventing HTML escaping. |
|
|
29
|
+
| `isSafe(value)` | `isSafe(any) → boolean` | Checks if a value is a `SafeString`. |
|
|
30
|
+
| `escapeHtml(str)` | `escapeHtml(string) → string` | Escapes HTML special characters. Used internally for auto‑escaping. |
|
|
31
|
+
|
|
32
|
+
All of the above are exported from `src/index.js` and can be imported via:
|
|
33
|
+
|
|
34
|
+
### CommonJS
|
|
35
|
+
```js
|
|
36
|
+
const {
|
|
37
|
+
compile,
|
|
38
|
+
render,
|
|
39
|
+
asyncRender,
|
|
40
|
+
__express,
|
|
41
|
+
__expressAsync,
|
|
42
|
+
stripExpressContext,
|
|
43
|
+
clearCache,
|
|
44
|
+
registerTag,
|
|
45
|
+
registerFilter,
|
|
46
|
+
getFilter,
|
|
47
|
+
registerHelper,
|
|
48
|
+
registerContextProcessor,
|
|
49
|
+
registerTranslation,
|
|
50
|
+
setLanguage,
|
|
51
|
+
getLanguage,
|
|
52
|
+
setFallbackLanguage,
|
|
53
|
+
getAvailableLanguages,
|
|
54
|
+
registerLibrary,
|
|
55
|
+
registerLibraryFromPath,
|
|
56
|
+
activateLibrary,
|
|
57
|
+
SafeString,
|
|
58
|
+
markSafe,
|
|
59
|
+
isSafe,
|
|
60
|
+
escapeHtml
|
|
61
|
+
} = require('miki-template');
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### ES Modules (ESM)
|
|
65
|
+
```js
|
|
66
|
+
import {
|
|
67
|
+
compile,
|
|
68
|
+
render,
|
|
69
|
+
asyncRender,
|
|
70
|
+
__express,
|
|
71
|
+
__expressAsync,
|
|
72
|
+
stripExpressContext,
|
|
73
|
+
clearCache,
|
|
74
|
+
registerTag,
|
|
75
|
+
registerFilter,
|
|
76
|
+
getFilter,
|
|
77
|
+
registerHelper,
|
|
78
|
+
registerContextProcessor,
|
|
79
|
+
registerTranslation,
|
|
80
|
+
setLanguage,
|
|
81
|
+
getLanguage,
|
|
82
|
+
setFallbackLanguage,
|
|
83
|
+
getAvailableLanguages,
|
|
84
|
+
registerLibrary,
|
|
85
|
+
registerLibraryFromPath,
|
|
86
|
+
activateLibrary,
|
|
87
|
+
SafeString,
|
|
88
|
+
markSafe,
|
|
89
|
+
isSafe,
|
|
90
|
+
escapeHtml
|
|
91
|
+
} from 'miki-template';
|
|
92
|
+
|
|
93
|
+
// Or import all as default
|
|
94
|
+
import miki from 'miki-template';
|
|
95
|
+
const { render: mikiRender } = miki;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
For detailed usage, refer to the corresponding sections in the documentation:
|
|
99
|
+
- **Usage** – `docs/usage.md`
|
|
100
|
+
- **Tags** – `docs/tags.md`
|
|
101
|
+
- **Filters** – `docs/filters.md`
|
|
102
|
+
- **Security** – `docs/security.md`
|