@bedrockio/templates 0.2.1 → 0.3.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/CHANGELOG.md +9 -0
- package/dist/cjs/TemplateRenderer.js +8 -2
- package/dist/cjs/utils.js +8 -3
- package/dist/esm/TemplateRenderer.js +8 -2
- package/dist/esm/utils.js +8 -3
- package/package.json +2 -2
- package/types/TemplateRenderer.d.ts +1 -0
- package/types/TemplateRenderer.d.ts.map +1 -1
- package/types/utils.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -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 =
|
|
45
|
-
const template = handlebars_1.default.compile(source
|
|
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
|
-
|
|
15
|
+
let source;
|
|
16
|
+
if (dir && isValidFilename(arg)) {
|
|
16
17
|
const filepath = path_1.default.resolve(dir, arg);
|
|
17
|
-
|
|
18
|
+
source = readSource(filepath) || arg;
|
|
18
19
|
}
|
|
19
20
|
else {
|
|
20
|
-
|
|
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);
|
|
@@ -45,6 +47,9 @@ function readSource(filepath) {
|
|
|
45
47
|
return tryReadFile(filepath + '.md') || tryReadFile(filepath + '.txt');
|
|
46
48
|
}
|
|
47
49
|
}
|
|
50
|
+
function isValidFilename(str) {
|
|
51
|
+
return str.length <= 255 && !str.includes('\n');
|
|
52
|
+
}
|
|
48
53
|
// Sections
|
|
49
54
|
const SECTIONS_REG = /^=== (\w+) ===\n\n/gm;
|
|
50
55
|
function getSections(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 =
|
|
40
|
-
const template = Handlebars.compile(source
|
|
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
|
-
|
|
6
|
+
let source;
|
|
7
|
+
if (dir && isValidFilename(arg)) {
|
|
7
8
|
const filepath = path.resolve(dir, arg);
|
|
8
|
-
|
|
9
|
+
source = readSource(filepath) || arg;
|
|
9
10
|
}
|
|
10
11
|
else {
|
|
11
|
-
|
|
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);
|
|
@@ -36,6 +38,9 @@ function readSource(filepath) {
|
|
|
36
38
|
return tryReadFile(filepath + '.md') || tryReadFile(filepath + '.txt');
|
|
37
39
|
}
|
|
38
40
|
}
|
|
41
|
+
function isValidFilename(str) {
|
|
42
|
+
return str.length <= 255 && !str.includes('\n');
|
|
43
|
+
}
|
|
39
44
|
// Sections
|
|
40
45
|
const SECTIONS_REG = /^=== (\w+) ===\n\n/gm;
|
|
41
46
|
export function getSections(str) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/templates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
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.
|
|
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"
|
|
@@ -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;
|
|
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"}
|
package/types/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAKA,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAKA,mEAYC;AAED;;;EAOC;AA4BD;;;;;IAsBC;AAMD,6CAMC"}
|