@prairielearn/html-ejs 1.0.1 → 1.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.
@@ -1,3 +1,3 @@
1
- @prairielearn/html-ejs:build: cache hit, replaying output 23880d36fc6dfce7
1
+ @prairielearn/html-ejs:build: cache hit, replaying output f31190aa13d9e859
2
2
  @prairielearn/html-ejs:build: warning package.json: No license field
3
3
  @prairielearn/html-ejs:build: $ tsc
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @prairielearn/html-ejs
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1aa43c734: Allow file URLs to be passed to `renderEjs`
8
+
3
9
  ## 1.0.1
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -6,9 +6,9 @@ import { type HtmlSafeString } from '@prairielearn/html';
6
6
  * The resulting string is assumed to be appropriately escaped and will be used
7
7
  * verbatim in the resulting HTML.
8
8
  *
9
- * @param filename The name of the file from which relative includes should be resolved.
9
+ * @param filePathOrUrl The path or file URL of the file from which relative includes should be resolved.
10
10
  * @param template The raw EJS template string.
11
11
  * @param data Any data to be made available to the template.
12
12
  * @returns The rendered EJS.
13
13
  */
14
- export declare function renderEjs(filename: string, template: string, data?: any): HtmlSafeString;
14
+ export declare function renderEjs(filePathOrUrl: string, template: string, data?: any): HtmlSafeString;
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.renderEjs = void 0;
7
7
  const ejs_1 = __importDefault(require("ejs"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const url_1 = require("url");
9
10
  const html_1 = require("@prairielearn/html");
10
11
  /**
11
12
  * This is a shim to allow for the use of EJS templates inside of HTML tagged
@@ -14,13 +15,19 @@ const html_1 = require("@prairielearn/html");
14
15
  * The resulting string is assumed to be appropriately escaped and will be used
15
16
  * verbatim in the resulting HTML.
16
17
  *
17
- * @param filename The name of the file from which relative includes should be resolved.
18
+ * @param filePathOrUrl The path or file URL of the file from which relative includes should be resolved.
18
19
  * @param template The raw EJS template string.
19
20
  * @param data Any data to be made available to the template.
20
21
  * @returns The rendered EJS.
21
22
  */
22
- function renderEjs(filename, template, data = {}) {
23
- return (0, html_1.unsafeHtml)(ejs_1.default.render(template, data, { views: [path_1.default.dirname(filename)] }));
23
+ function renderEjs(filePathOrUrl, template, data = {}) {
24
+ let resolvedPath = filePathOrUrl;
25
+ // This allows for us to pass `import.meta.url` to this function in ES Modules
26
+ // environments where `__filename` is not available.
27
+ if (filePathOrUrl.startsWith('file://')) {
28
+ resolvedPath = (0, url_1.fileURLToPath)(filePathOrUrl);
29
+ }
30
+ return (0, html_1.unsafeHtml)(ejs_1.default.render(template, data, { views: [path_1.default.dirname(resolvedPath)] }));
24
31
  }
25
32
  exports.renderEjs = renderEjs;
26
33
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAsB;AACtB,gDAAwB;AACxB,6BAAoC;AAEpC,6CAAqE;AACrE;;;;;;;;;;;GAWG;AACH,SAAgB,SAAS,CAAC,aAAqB,EAAE,QAAgB,EAAE,OAAY,EAAE;IAC/E,IAAI,YAAY,GAAG,aAAa,CAAC;IAEjC,8EAA8E;IAC9E,oDAAoD;IACpD,IAAI,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;QACvC,YAAY,GAAG,IAAA,mBAAa,EAAC,aAAa,CAAC,CAAC;KAC7C;IAED,OAAO,IAAA,iBAAU,EAAC,aAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,cAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzF,CAAC;AAVD,8BAUC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/html-ejs",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "tsc",
@@ -9,13 +9,13 @@
9
9
  },
10
10
  "dependencies": {
11
11
  "@prairielearn/html": "^2.0.0",
12
- "ejs": "^3.1.6"
12
+ "ejs": "^3.1.8"
13
13
  },
14
14
  "devDependencies": {
15
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"
16
+ "@types/ejs": "^3.1.1",
17
+ "mocha": "^10.0.0",
18
+ "ts-node": "^10.8.1",
19
+ "typescript": "^4.7.4"
20
20
  }
21
21
  }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import ejs from 'ejs';
2
2
  import path from 'path';
3
+ import { fileURLToPath } from 'url';
3
4
 
4
5
  import { unsafeHtml, type HtmlSafeString } from '@prairielearn/html';
5
6
  /**
@@ -9,11 +10,19 @@ import { unsafeHtml, type HtmlSafeString } from '@prairielearn/html';
9
10
  * The resulting string is assumed to be appropriately escaped and will be used
10
11
  * verbatim in the resulting HTML.
11
12
  *
12
- * @param filename The name of the file from which relative includes should be resolved.
13
+ * @param filePathOrUrl The path or file URL of the file from which relative includes should be resolved.
13
14
  * @param template The raw EJS template string.
14
15
  * @param data Any data to be made available to the template.
15
16
  * @returns The rendered EJS.
16
17
  */
17
- export function renderEjs(filename: string, template: string, data: any = {}): HtmlSafeString {
18
- return unsafeHtml(ejs.render(template, data, { views: [path.dirname(filename)] }));
18
+ export function renderEjs(filePathOrUrl: string, template: string, data: any = {}): HtmlSafeString {
19
+ let resolvedPath = filePathOrUrl;
20
+
21
+ // This allows for us to pass `import.meta.url` to this function in ES Modules
22
+ // environments where `__filename` is not available.
23
+ if (filePathOrUrl.startsWith('file://')) {
24
+ resolvedPath = fileURLToPath(filePathOrUrl);
25
+ }
26
+
27
+ return unsafeHtml(ejs.render(template, data, { views: [path.dirname(resolvedPath)] }));
19
28
  }
package/tsconfig.json CHANGED
@@ -3,5 +3,6 @@
3
3
  "compilerOptions": {
4
4
  "outDir": "./dist",
5
5
  "rootDir": "./src",
6
+ "types": ["mocha", "node"],
6
7
  }
7
8
  }