Skip to content

Commit 974efb8

Browse files
refactor: refactored locals parsing logic to allow inner locals feature
1 parent 447bc44 commit 974efb8

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@ module.exports = (options = {}) => {
1717

1818
tree.match({tag: 'include'}, node => {
1919
let src = node.attrs.src || false;
20-
let locals = node.attrs.locals || (node.content ? node.content.join().replace(/'\n'/g, '') : false);
2120
let content;
2221
let subtree;
2322
let source;
23+
let locals;
24+
25+
const localsString = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : '');
26+
const tryToParseLocals = localsString.trim() && localsString.trim().startsWith('{');
27+
28+
if (tryToParseLocals) {
29+
try {
30+
locals = JSON.parse(localsString);
31+
} catch {
32+
locals = false;
33+
}
34+
}
2435

2536
if (src) {
2637
src = path.resolve(options.root, src);
2738
source = fs.readFileSync(src, options.encoding);
28-
if (locals && locals.includes(':')) {
29-
locals = JSON.parse(locals);
39+
40+
if (locals) {
3041
const result = posthtml()
3142
.use(expressions({locals}))
3243
.process(source, {sync: true});

0 commit comments

Comments
 (0)