Skip to content

Commit e662903

Browse files
authored
Merge pull request #53 from felippe-regazio/master
Tiny Feature : Pass "locals" as the <include/> inner text
2 parents c5fe218 + 808e2df commit e662903

File tree

10 files changed

+2587
-3
lines changed

10 files changed

+2587
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ posthtml([ include({ encoding: 'utf8' }) ])
8585
</body>
8686
</html>
8787
```
88+
89+
You can also pass your locals directly on the \<include> content, just drop a JSON there. When doing it, all the "\n" chars will be removed from your data.
90+
If you need "\n" chars on your data, you can still use the "locals" attribute.
91+
92+
```html
93+
<include src="components/button.html">
94+
{
95+
"text": "Button"
96+
}
97+
</include>
98+
```
99+
88100
> **Note:** Also supports multi nesting.
89101
90102
<h2 align="center">LICENSE</h2>

lib/index.js

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

1818
tree.match({tag: 'include'}, node => {
1919
let src = node.attrs.src || false;
20-
let locals = node.attrs.locals || false;
2120
let content;
2221
let subtree;
2322
let source;
23+
let locals = false;
2424

2525
if (src) {
2626
src = path.resolve(options.root, src);
2727
source = fs.readFileSync(src, options.encoding);
28-
if (locals && locals.includes(':')) {
29-
locals = JSON.parse(locals);
28+
29+
try {
30+
const localsRaw = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : false);
31+
locals = JSON.parse(localsRaw);
32+
} catch {}
33+
34+
if (locals) {
3035
const result = posthtml()
3136
.use(expressions({locals}))
3237
.process(source, {sync: true});

0 commit comments

Comments
 (0)