Skip to content

Commit c704db2

Browse files
authored
Merge branch 'master' into master
2 parents f46914a + 7c10bbe commit c704db2

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ posthtml([ include({ encoding: 'utf8' }) ])
3030

3131
### Options
3232

33-
__root__: Root folder path for include. Default `./`
33+
__root__: Root directory for include. Default `process.cwd()`
34+
35+
__cwd__: Current working directory for include. Default `process.cwd()`
3436

3537
__encoding__: Default `utf-8`
3638

lib/index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict'
1+
'use strict';
22

33
const fs = require('fs');
44
const path = require('path');
@@ -8,10 +8,16 @@ const {match} = require('posthtml/lib/api');
88
const expressions = require('posthtml-expressions');
99

1010
module.exports = (options = {}) => {
11-
options.root = options.root || './';
11+
options.root = options.root ? path.resolve(options.root) : process.cwd();
1212
options.encoding = options.encoding || 'utf-8';
1313

1414
return function posthtmlInclude(tree) {
15+
const cwd = options.cwd ?
16+
path.resolve(options.cwd) :
17+
(tree.options.from ?
18+
path.dirname(path.resolve(tree.options.from)) :
19+
process.cwd());
20+
1521
tree.parser = tree.parser || parser;
1622
tree.match = tree.match || match;
1723

@@ -20,17 +26,23 @@ module.exports = (options = {}) => {
2026
let content;
2127
let subtree;
2228
let source;
23-
let posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {locals: false};
29+
let posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {
30+
locals: false
31+
};
2432
if (options.delimiters) {
2533
posthtmlExpressionsOptions.delimiters = options.delimiters;
2634
}
2735

2836
if (src) {
29-
src = path.resolve(options.root, src);
37+
src = path.isAbsolute(src) ?
38+
path.resolve(cwd, src) :
39+
path.join(options.root, src);
3040
source = fs.readFileSync(src, options.encoding);
3141

3242
try {
33-
const localsRaw = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : false);
43+
const localsRaw =
44+
node.attrs.locals ||
45+
(node.content ? node.content.join().replace(/\n/g, '') : false);
3446
const localsJson = JSON.parse(localsRaw);
3547
posthtmlExpressionsOptions = {
3648
...posthtmlExpressionsOptions,
@@ -46,10 +58,18 @@ module.exports = (options = {}) => {
4658
}
4759

4860
subtree = tree.parser(source);
61+
subtree.options = subtree.options || {};
62+
subtree.options.from = path.isAbsolute(src) ?
63+
src :
64+
(tree.options.from ?
65+
path.relative(tree.options.from, src) :
66+
src);
4967
subtree.match = tree.match;
5068
subtree.parser = tree.parser;
5169
subtree.messages = tree.messages;
52-
content = source.includes('include') ? posthtmlInclude(subtree) : subtree;
70+
content = source.includes('include') ?
71+
posthtmlInclude(subtree) :
72+
subtree;
5373

5474
if (tree.messages) {
5575
tree.messages.push({

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-include",
3-
"version": "1.7.4",
3+
"version": "2.0.0",
44
"description": "Include files in HTML",
55
"license": "MIT",
66
"author": "Ivan Voischev <[email protected]>",

0 commit comments

Comments
 (0)