@prairielearn/html-ejs 1.0.1
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/.turbo/turbo-build.log +3 -0
- package/CHANGELOG.md +8 -0
- package/README.md +22 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +13 -0
- package/dist/index.test.js.map +1 -0
- package/package.json +21 -0
- package/src/index.test.ts +16 -0
- package/src/index.ts +19 -0
- package/tsconfig.json +7 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# `@prairielearn/html-ejs`
|
|
2
|
+
|
|
3
|
+
Utilities for rendering EJS templates for use with the `@prairielearn/html` package.
|
|
4
|
+
|
|
5
|
+
If you have an EJS partial that you'd like to use inside of an `html` tagged template literal, you can use the `renderEjs` helper:
|
|
6
|
+
|
|
7
|
+
```html
|
|
8
|
+
<!-- hello.ejs -->
|
|
9
|
+
Hello, <%= name %>!
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { html } from '@prairielearn/html';
|
|
14
|
+
import { renderEjs } from '@prairielearn/html-ejs';
|
|
15
|
+
|
|
16
|
+
console.log(
|
|
17
|
+
html`
|
|
18
|
+
<div>Hello, world!</div>
|
|
19
|
+
<div>${renderEjs(__filename, "<%- include('./hello'); %>", { name: 'Anjali' })}</div>
|
|
20
|
+
`.toString()
|
|
21
|
+
);
|
|
22
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type HtmlSafeString } from '@prairielearn/html';
|
|
2
|
+
/**
|
|
3
|
+
* This is a shim to allow for the use of EJS templates inside of HTML tagged
|
|
4
|
+
* template literals.
|
|
5
|
+
*
|
|
6
|
+
* The resulting string is assumed to be appropriately escaped and will be used
|
|
7
|
+
* verbatim in the resulting HTML.
|
|
8
|
+
*
|
|
9
|
+
* @param filename The name of the file from which relative includes should be resolved.
|
|
10
|
+
* @param template The raw EJS template string.
|
|
11
|
+
* @param data Any data to be made available to the template.
|
|
12
|
+
* @returns The rendered EJS.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renderEjs(filename: string, template: string, data?: any): HtmlSafeString;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.renderEjs = void 0;
|
|
7
|
+
const ejs_1 = __importDefault(require("ejs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const html_1 = require("@prairielearn/html");
|
|
10
|
+
/**
|
|
11
|
+
* This is a shim to allow for the use of EJS templates inside of HTML tagged
|
|
12
|
+
* template literals.
|
|
13
|
+
*
|
|
14
|
+
* The resulting string is assumed to be appropriately escaped and will be used
|
|
15
|
+
* verbatim in the resulting HTML.
|
|
16
|
+
*
|
|
17
|
+
* @param filename The name of the file from which relative includes should be resolved.
|
|
18
|
+
* @param template The raw EJS template string.
|
|
19
|
+
* @param data Any data to be made available to the template.
|
|
20
|
+
* @returns The rendered EJS.
|
|
21
|
+
*/
|
|
22
|
+
function renderEjs(filename, template, data = {}) {
|
|
23
|
+
return (0, html_1.unsafeHtml)(ejs_1.default.render(template, data, { views: [path_1.default.dirname(filename)] }));
|
|
24
|
+
}
|
|
25
|
+
exports.renderEjs = renderEjs;
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAsB;AACtB,gDAAwB;AAExB,6CAAqE;AACrE;;;;;;;;;;;GAWG;AACH,SAAgB,SAAS,CAAC,QAAgB,EAAE,QAAgB,EAAE,OAAY,EAAE;IAC1E,OAAO,IAAA,iBAAU,EAAC,aAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACrF,CAAC;AAFD,8BAEC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chai_1 = require("chai");
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
describe('renderEjs', () => {
|
|
6
|
+
it('renders EJS template without data', () => {
|
|
7
|
+
chai_1.assert.equal((0, index_1.renderEjs)(__filename, '<p>Hello</p>', {}).toString(), '<p>Hello</p>');
|
|
8
|
+
});
|
|
9
|
+
it('renders EJS template with data', () => {
|
|
10
|
+
chai_1.assert.equal((0, index_1.renderEjs)(__filename, '<p>Hello <%= name %></p>', { name: 'Divya' }).toString(), '<p>Hello Divya</p>');
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":";;AAAA,+BAA8B;AAE9B,mCAAoC;AAEpC,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,aAAM,CAAC,KAAK,CAAC,IAAA,iBAAS,EAAC,UAAU,EAAE,cAAc,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE,EAAE,cAAc,CAAC,CAAC;IACrF,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACxC,aAAM,CAAC,KAAK,CACV,IAAA,iBAAS,EAAC,UAAU,EAAE,0BAA0B,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,QAAQ,EAAE,EAC/E,oBAAoB,CACrB,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prairielearn/html-ejs",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc",
|
|
7
|
+
"dev": "tsc --watch --preserveWatchOutput",
|
|
8
|
+
"test": "mocha --no-config --require ts-node/register src/index.test.ts"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@prairielearn/html": "^2.0.0",
|
|
12
|
+
"ejs": "^3.1.6"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@prairielearn/tsconfig": "*",
|
|
16
|
+
"@types/ejs": "^3.1.0",
|
|
17
|
+
"mocha": "^9.2.2",
|
|
18
|
+
"ts-node": "^10.7.0",
|
|
19
|
+
"typescript": "^4.6.3"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { assert } from 'chai';
|
|
2
|
+
|
|
3
|
+
import { renderEjs } from './index';
|
|
4
|
+
|
|
5
|
+
describe('renderEjs', () => {
|
|
6
|
+
it('renders EJS template without data', () => {
|
|
7
|
+
assert.equal(renderEjs(__filename, '<p>Hello</p>', {}).toString(), '<p>Hello</p>');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('renders EJS template with data', () => {
|
|
11
|
+
assert.equal(
|
|
12
|
+
renderEjs(__filename, '<p>Hello <%= name %></p>', { name: 'Divya' }).toString(),
|
|
13
|
+
'<p>Hello Divya</p>'
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ejs from 'ejs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import { unsafeHtml, type HtmlSafeString } from '@prairielearn/html';
|
|
5
|
+
/**
|
|
6
|
+
* This is a shim to allow for the use of EJS templates inside of HTML tagged
|
|
7
|
+
* template literals.
|
|
8
|
+
*
|
|
9
|
+
* The resulting string is assumed to be appropriately escaped and will be used
|
|
10
|
+
* verbatim in the resulting HTML.
|
|
11
|
+
*
|
|
12
|
+
* @param filename The name of the file from which relative includes should be resolved.
|
|
13
|
+
* @param template The raw EJS template string.
|
|
14
|
+
* @param data Any data to be made available to the template.
|
|
15
|
+
* @returns The rendered EJS.
|
|
16
|
+
*/
|
|
17
|
+
export function renderEjs(filename: string, template: string, data: any = {}): HtmlSafeString {
|
|
18
|
+
return unsafeHtml(ejs.render(template, data, { views: [path.dirname(filename)] }));
|
|
19
|
+
}
|