@ender672/minja-js 0.1.1 → 0.1.2
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 +2 -12
- package/package.json +1 -1
- package/src/chat-template.js +2 -2
- package/src/minja.js +1 -46
package/README.md
CHANGED
|
@@ -9,19 +9,13 @@ This is an independent port — not affiliated with or endorsed by the original
|
|
|
9
9
|
### Low-level: parse and render a template
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
|
-
import { Parser, Context
|
|
12
|
+
import { Parser, Context } from '@ender672/minja-js/minja';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
// template string skip parsing. The AST is stateless — safe to
|
|
16
|
-
// render concurrently with different contexts.
|
|
17
|
-
const root = parseTemplate('Hello {{ name }}!');
|
|
14
|
+
const root = Parser.parse('Hello {{ name }}!');
|
|
18
15
|
const ctx = Context.make({ name: 'world' });
|
|
19
16
|
console.log(root.render(ctx)); // "Hello world!"
|
|
20
17
|
```
|
|
21
18
|
|
|
22
|
-
If you are generating template strings dynamically and don't want to
|
|
23
|
-
fill the cache, call `Parser.parse()` directly instead.
|
|
24
|
-
|
|
25
19
|
### High-level: ChatTemplate
|
|
26
20
|
|
|
27
21
|
```js
|
|
@@ -34,10 +28,6 @@ const output = tmpl.apply({
|
|
|
34
28
|
});
|
|
35
29
|
```
|
|
36
30
|
|
|
37
|
-
`ChatTemplate` automatically caches its parsed AST via `parseTemplate()`,
|
|
38
|
-
so constructing multiple `ChatTemplate` instances with the same source
|
|
39
|
-
string only parses the template once.
|
|
40
|
-
|
|
41
31
|
## Running tests
|
|
42
32
|
|
|
43
33
|
```sh
|
package/package.json
CHANGED
package/src/chat-template.js
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
// SPDX-License-Identifier: MIT
|
|
9
9
|
|
|
10
|
-
import { Parser, Context, Value
|
|
10
|
+
import { Parser, Context, Value } from './minja.js';
|
|
11
11
|
|
|
12
12
|
export class ChatTemplate {
|
|
13
13
|
constructor(source, bosToken = '', eosToken = '') {
|
|
14
14
|
this._source = source;
|
|
15
15
|
this._bosToken = bosToken;
|
|
16
16
|
this._eosToken = eosToken;
|
|
17
|
-
this._templateRoot =
|
|
17
|
+
this._templateRoot = Parser.parse(source, {
|
|
18
18
|
trimBlocks: true,
|
|
19
19
|
lstripBlocks: true,
|
|
20
20
|
keepTrailingNewline: false,
|
package/src/minja.js
CHANGED
|
@@ -2438,49 +2438,4 @@ function _createBuiltins() {
|
|
|
2438
2438
|
|
|
2439
2439
|
return new Context(globals);
|
|
2440
2440
|
}
|
|
2441
|
-
|
|
2442
|
-
// ── Template Cache ──────────────────────────────────────────────────────
|
|
2443
|
-
//
|
|
2444
|
-
// Parser.parse() builds an AST from a template string. The AST is stateless —
|
|
2445
|
-
// all runtime state lives in the Context passed to render(). That means a
|
|
2446
|
-
// parsed template can be rendered any number of times with different contexts,
|
|
2447
|
-
// and concurrent renders of the same cached AST are safe.
|
|
2448
|
-
//
|
|
2449
|
-
// This cache is keyed on (templateStr + options) so that the same template
|
|
2450
|
-
// parsed with different whitespace-control options gets separate entries.
|
|
2451
|
-
// It uses a plain Map (unbounded) which is fine for the expected use case of
|
|
2452
|
-
// tens-to-hundreds of distinct chat templates in a server process. If you are
|
|
2453
|
-
// dynamically generating template strings, bypass the cache and call
|
|
2454
|
-
// Parser.parse() directly.
|
|
2455
|
-
//
|
|
2456
|
-
const _templateCache = new Map();
|
|
2457
|
-
|
|
2458
|
-
function _cacheKey(templateStr, options) {
|
|
2459
|
-
return `${templateStr}\0${options.trimBlocks ? 1 : 0}${options.lstripBlocks ? 1 : 0}${options.keepTrailingNewline ? 1 : 0}`;
|
|
2460
|
-
}
|
|
2461
|
-
|
|
2462
|
-
/**
|
|
2463
|
-
* Parse a template string into a reusable AST, returning a cached copy if
|
|
2464
|
-
* the same template and options have been parsed before.
|
|
2465
|
-
*
|
|
2466
|
-
* The returned AST is stateless and safe for concurrent use across renders.
|
|
2467
|
-
*/
|
|
2468
|
-
function parseTemplate(templateStr, options = {}) {
|
|
2469
|
-
const key = _cacheKey(templateStr, options);
|
|
2470
|
-
let root = _templateCache.get(key);
|
|
2471
|
-
if (!root) {
|
|
2472
|
-
root = Parser.parse(templateStr, options);
|
|
2473
|
-
_templateCache.set(key, root);
|
|
2474
|
-
}
|
|
2475
|
-
return root;
|
|
2476
|
-
}
|
|
2477
|
-
|
|
2478
|
-
/**
|
|
2479
|
-
* Clear the parsed-template cache. Useful in tests or if you want to
|
|
2480
|
-
* reclaim memory after loading a batch of templates you no longer need.
|
|
2481
|
-
*/
|
|
2482
|
-
function clearTemplateCache() {
|
|
2483
|
-
_templateCache.clear();
|
|
2484
|
-
}
|
|
2485
|
-
|
|
2486
|
-
export { Parser, Context, Value, parseTemplate, clearTemplateCache };
|
|
2441
|
+
export { Parser, Context, Value };
|