@mepkg/mtwig 1.0.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/package.json +20 -0
- package/src/filters/_filters.js +5 -0
- package/src/filters/truncate.js +14 -0
- package/src/index.js +47 -0
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mepkg/mtwig",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "src/index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"src"
|
|
7
|
+
],
|
|
8
|
+
"private": false,
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node --test 'tests/**/*.test.js'",
|
|
14
|
+
"test:watch": "node --test --watch 'tests/**/*.test.js'"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"twing": "^7.3.1"
|
|
18
|
+
},
|
|
19
|
+
"version": "1.0.0"
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {createFilter} from "twing";
|
|
2
|
+
|
|
3
|
+
export default (twing) => {
|
|
4
|
+
twing.addFilter(createFilter('truncate', (_context, value, length = 35, suffix = '...') => {
|
|
5
|
+
const str = String(value ?? '');
|
|
6
|
+
if (str.length <= length) {
|
|
7
|
+
return Promise.resolve(str);
|
|
8
|
+
}
|
|
9
|
+
return Promise.resolve(str.substring(0, length) + suffix);
|
|
10
|
+
}, [
|
|
11
|
+
{name: 'length', defaultValue: 30},
|
|
12
|
+
{name: 'suffix', defaultValue: '...'}
|
|
13
|
+
]));
|
|
14
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import {createEnvironment, createFilesystemLoader} from "twing";
|
|
4
|
+
import {filters} from "./filters/_filters.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const cache = new Map();
|
|
8
|
+
const cacheHandler = {
|
|
9
|
+
write: (key, content) => {
|
|
10
|
+
cache.set(key, {content, timestamp: Date.now()});
|
|
11
|
+
return Promise.resolve();
|
|
12
|
+
},
|
|
13
|
+
load: (key) => {
|
|
14
|
+
return Promise.resolve(cache.get(key)?.content ?? null);
|
|
15
|
+
},
|
|
16
|
+
getTimestamp: (key) => {
|
|
17
|
+
return Promise.resolve(cache.get(key)?.timestamp ?? 0);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const express = (app, viewsPath) => {
|
|
22
|
+
app.set('views', viewsPath);
|
|
23
|
+
const loader = createFilesystemLoader(fs);
|
|
24
|
+
loader.addPath(viewsPath);
|
|
25
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
26
|
+
const twing = createEnvironment(loader, isProduction ? {cache: cacheHandler} : {});
|
|
27
|
+
|
|
28
|
+
for (const filter of filters) {
|
|
29
|
+
filter(twing);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
app.engine('twig', (filePath, options, callback) => {
|
|
33
|
+
const relativePath = path.relative(viewsPath, filePath);
|
|
34
|
+
twing.render(relativePath, options)
|
|
35
|
+
.then((output) => {
|
|
36
|
+
return callback(null, output);
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => {
|
|
39
|
+
return callback(err);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
app.set('view engine', 'twig');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default {
|
|
46
|
+
express
|
|
47
|
+
}
|