@bedrockio/templates 0.2.0 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.0
2
+
3
+ - Allow getting template source.
4
+ - Fixed relTime tests.
5
+
1
6
  ## 0.2.0
2
7
 
3
8
  - Allow params in URLs.
@@ -26,6 +26,12 @@ class TemplateRenderer {
26
26
  allowProtoPropertiesByDefault: true,
27
27
  });
28
28
  }
29
+ getTemplateSource = (0, lodash_memoize_1.default)((input = '', options) => {
30
+ return (0, utils_1.resolveTemplateSource)(input, {
31
+ ...this.options,
32
+ ...options,
33
+ });
34
+ });
29
35
  // Private
30
36
  /** @returns {Object} */
31
37
  resolveOptions(options = {}) {
@@ -41,8 +47,8 @@ class TemplateRenderer {
41
47
  return resolved;
42
48
  }
43
49
  loadTemplate = (0, lodash_memoize_1.default)((input = '', options) => {
44
- const source = (0, utils_1.resolveTemplateSource)(input, options);
45
- const template = handlebars_1.default.compile(source.trim());
50
+ const source = this.getTemplateSource(input, options);
51
+ const template = handlebars_1.default.compile(source);
46
52
  return (params, options) => {
47
53
  let output = template(params, options);
48
54
  output = (0, utils_1.unescapeHtml)(output);
package/dist/cjs/utils.js CHANGED
@@ -12,13 +12,15 @@ const path_1 = __importDefault(require("path"));
12
12
  const front_matter_1 = __importDefault(require("front-matter"));
13
13
  function resolveTemplateSource(arg, options) {
14
14
  const { dir } = options;
15
+ let source;
15
16
  if (dir) {
16
17
  const filepath = path_1.default.resolve(dir, arg);
17
- return readSource(filepath) || arg;
18
+ source = readSource(filepath) || arg;
18
19
  }
19
20
  else {
20
- return arg;
21
+ source = arg;
21
22
  }
23
+ return source.trim();
22
24
  }
23
25
  function runFrontMatter(str) {
24
26
  let { body, attributes: meta } = (0, front_matter_1.default)(str);
@@ -21,6 +21,12 @@ export default class TemplateRenderer {
21
21
  allowProtoPropertiesByDefault: true,
22
22
  });
23
23
  }
24
+ getTemplateSource = memoize((input = '', options) => {
25
+ return resolveTemplateSource(input, {
26
+ ...this.options,
27
+ ...options,
28
+ });
29
+ });
24
30
  // Private
25
31
  /** @returns {Object} */
26
32
  resolveOptions(options = {}) {
@@ -36,8 +42,8 @@ export default class TemplateRenderer {
36
42
  return resolved;
37
43
  }
38
44
  loadTemplate = memoize((input = '', options) => {
39
- const source = resolveTemplateSource(input, options);
40
- const template = Handlebars.compile(source.trim());
45
+ const source = this.getTemplateSource(input, options);
46
+ const template = Handlebars.compile(source);
41
47
  return (params, options) => {
42
48
  let output = template(params, options);
43
49
  output = unescapeHtml(output);
package/dist/esm/utils.js CHANGED
@@ -3,13 +3,15 @@ import path from 'path';
3
3
  import frontmatter from 'front-matter';
4
4
  export function resolveTemplateSource(arg, options) {
5
5
  const { dir } = options;
6
+ let source;
6
7
  if (dir) {
7
8
  const filepath = path.resolve(dir, arg);
8
- return readSource(filepath) || arg;
9
+ source = readSource(filepath) || arg;
9
10
  }
10
11
  else {
11
- return arg;
12
+ source = arg;
12
13
  }
14
+ return source.trim();
13
15
  }
14
16
  export function runFrontMatter(str) {
15
17
  let { body, attributes: meta } = frontmatter(str);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/templates",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Bedrock utility for custom templates.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -34,7 +34,7 @@
34
34
  "url": "https://github.com/bedrockio/templates"
35
35
  },
36
36
  "dependencies": {
37
- "@bedrockio/chrono": "^0.7.0",
37
+ "@bedrockio/chrono": "^0.9.1",
38
38
  "front-matter": "^4.0.2",
39
39
  "handlebars": "^4.7.8",
40
40
  "lodash.memoize": "^4.1.2"
@@ -3,6 +3,7 @@ export default class TemplateRenderer {
3
3
  /** @type {Object} */
4
4
  options: any;
5
5
  run(options: any): any;
6
+ getTemplateSource: any;
6
7
  /** @returns {Object} */
7
8
  resolveOptions(options?: {}): any;
8
9
  loadTemplate: any;
@@ -1 +1 @@
1
- {"version":3,"file":"TemplateRenderer.d.ts","sourceRoot":"","sources":["../src/TemplateRenderer.js"],"names":[],"mappings":"AAYA;IACE,0BASC;IARC,qBAAqB;IACrB,aAMC;IAGH,uBASC;IAID,wBAAwB;IACxB,kCAWC;IAED,kBAkBG;CACJ"}
1
+ {"version":3,"file":"TemplateRenderer.d.ts","sourceRoot":"","sources":["../src/TemplateRenderer.js"],"names":[],"mappings":"AAYA;IACE,0BASC;IARC,qBAAqB;IACrB,aAMC;IAGH,uBASC;IAED,uBAKG;IAIH,wBAAwB;IACxB,kCAWC;IAED,kBAkBG;CACJ"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAKA,mEASC;AAED;;;EAOC;AAwBD;;;;;IAsBC;AAMD,6CAMC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAKA,mEAYC;AAED;;;EAOC;AAwBD;;;;;IAsBC;AAMD,6CAMC"}