lite-template 0.0.1 → 0.1.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 +6 -4
- package/.gitattributes +0 -2
- package/.github/workflows/publish.yml +0 -32
- package/src/index.ts +0 -79
- package/tsconfig.json +0 -14
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lite-template",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Ultra lightweight, zero-dependency async template engine compatible with basic EJS syntax.",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
6
9
|
"main": "dist/index.js",
|
|
7
10
|
"types": "dist/index.d.ts",
|
|
8
11
|
"scripts": {
|
|
@@ -31,11 +34,10 @@
|
|
|
31
34
|
},
|
|
32
35
|
"repository": {
|
|
33
36
|
"type": "git",
|
|
34
|
-
"url": "git+https://github.com/docmd-io/
|
|
35
|
-
"directory": "lite-template"
|
|
37
|
+
"url": "git+https://github.com/docmd-io/lite-template.git"
|
|
36
38
|
},
|
|
37
39
|
"bugs": {
|
|
38
|
-
"url": "https://github.com/docmd-io/
|
|
40
|
+
"url": "https://github.com/docmd-io/lite-template/issues"
|
|
39
41
|
},
|
|
40
42
|
"homepage": "https://docmd.io",
|
|
41
43
|
"funding": "https://github.com/sponsors/mgks",
|
package/.gitattributes
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
name: Publish to NPM
|
|
2
|
-
on:
|
|
3
|
-
release:
|
|
4
|
-
types: [published]
|
|
5
|
-
workflow_dispatch:
|
|
6
|
-
|
|
7
|
-
permissions:
|
|
8
|
-
contents: read
|
|
9
|
-
id-token: write
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
publish:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v6
|
|
16
|
-
|
|
17
|
-
- name: Setup Node.js
|
|
18
|
-
uses: actions/setup-node@v6
|
|
19
|
-
with:
|
|
20
|
-
node-version: '22'
|
|
21
|
-
registry-url: 'https://registry.npmjs.org'
|
|
22
|
-
|
|
23
|
-
- name: Install dependencies
|
|
24
|
-
run: npm install
|
|
25
|
-
|
|
26
|
-
- name: Build package
|
|
27
|
-
run: npm run build
|
|
28
|
-
|
|
29
|
-
- name: Publish to npm
|
|
30
|
-
run: npm publish --access public
|
|
31
|
-
env:
|
|
32
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/src/index.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
|
|
2
|
-
|
|
3
|
-
function escapeHtml(str) {
|
|
4
|
-
return String(str)
|
|
5
|
-
.replace(/&/g, '&')
|
|
6
|
-
.replace(/</g, '<')
|
|
7
|
-
.replace(/>/g, '>')
|
|
8
|
-
.replace(/"/g, '"')
|
|
9
|
-
.replace(/'/g, ''');
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Compiles an EJS-style template string into an async function.
|
|
14
|
-
* Supports: <% js %>, <%= escaped %>, <%- unescaped %>
|
|
15
|
-
*/
|
|
16
|
-
export function compile(template) {
|
|
17
|
-
let code = "";
|
|
18
|
-
const regex = /<%(=|-|_|#)?([\s\S]+?)([-_])?%>/g;
|
|
19
|
-
let cursor = 0;
|
|
20
|
-
let match;
|
|
21
|
-
|
|
22
|
-
while ((match = regex.exec(template)) !== null) {
|
|
23
|
-
const textBefore = template.slice(cursor, match.index);
|
|
24
|
-
if (textBefore) {
|
|
25
|
-
code += `__out += \`${textBefore.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;\n`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const type = match[1];
|
|
29
|
-
let script = match[2].trim();
|
|
30
|
-
|
|
31
|
-
if (type === '#') {
|
|
32
|
-
// It's a comment, completely ignore it.
|
|
33
|
-
} else if (type === '=') {
|
|
34
|
-
// Escape HTML output
|
|
35
|
-
code += `__out += escapeHtml(typeof (${script}) !== 'undefined' ? (${script}) : '');\n`;
|
|
36
|
-
} else if (type === '-') {
|
|
37
|
-
// Raw HTML output
|
|
38
|
-
code += `__out += String(typeof (${script}) !== 'undefined' ? (${script}) : '');\n`;
|
|
39
|
-
} else {
|
|
40
|
-
// Literal JS code execution
|
|
41
|
-
code += `${script}\n`;
|
|
42
|
-
}
|
|
43
|
-
cursor = match.index + match[0].length;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const textAfter = template.slice(cursor);
|
|
47
|
-
if (textAfter) {
|
|
48
|
-
code += `__out += \`${textAfter.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;\n`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const wrappedCode = `let __out = '';\nwith(__data || {}) {\n${code}\nreturn __out;\n}`;
|
|
52
|
-
|
|
53
|
-
let fn;
|
|
54
|
-
try {
|
|
55
|
-
fn = new AsyncFunction("__data", "escapeHtml", wrappedCode);
|
|
56
|
-
} catch (e) {
|
|
57
|
-
console.error("COMPILATION ERROR CODE:\n" + wrappedCode.split('\\n').map((l, i) => `${i+1}: ${l}`).join('\\n'));
|
|
58
|
-
throw e;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return function(data) {
|
|
62
|
-
return fn(data, escapeHtml);
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Convenience method to render template directly.
|
|
68
|
-
*/
|
|
69
|
-
export async function render(template, data = {}, options = {}) {
|
|
70
|
-
// Allow caching if 'filename' or 'id' is passed usually, but here we just compile and run.
|
|
71
|
-
const fn = compile(template);
|
|
72
|
-
// Add locals pseudo-property common in EJS contexts
|
|
73
|
-
return await fn({ locals: data, ...data });
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export default {
|
|
77
|
-
render,
|
|
78
|
-
compile
|
|
79
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "Node16",
|
|
5
|
-
"moduleResolution": "node16",
|
|
6
|
-
"strict": false,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"outDir": "dist",
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true,
|
|
11
|
-
"skipLibCheck": true
|
|
12
|
-
},
|
|
13
|
-
"include": ["src/**/*"]
|
|
14
|
-
}
|