Skip to content

Commit 43d9f3c

Browse files
authored
Merge pull request #87 from clarfonthey/master
breaking: add cwd property to complement root
2 parents 69789e2 + 5d3bdd3 commit 43d9f3c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const process = require('process');
56
const posthtml = require('posthtml');
67
const {parser} = require('posthtml-parser');
78
const {match} = require('posthtml/lib/api');
89
const expressions = require('posthtml-expressions');
910

1011
module.exports = (options = {}) => {
11-
options.root = options.root || './';
12+
options.root = options.root
13+
? path.resolve(options.root)
14+
: process.cwd();
1215
options.encoding = options.encoding || 'utf-8';
1316

1417
return function posthtmlInclude(tree) {
18+
const cwd = options.cwd
19+
? path.resolve(options.cwd)
20+
: tree.options.from
21+
? path.dirname(path.resolve(tree.options.from))
22+
: process.cwd()
23+
1524
tree.parser = tree.parser || parser;
1625
tree.match = tree.match || match;
1726

@@ -26,7 +35,9 @@ module.exports = (options = {}) => {
2635
}
2736

2837
if (src) {
29-
src = path.resolve(options.root, src);
38+
src = path.isAbsolute(src)
39+
? path.join(options.root, src)
40+
: path.resolve(cwd, src)
3041
source = fs.readFileSync(src, options.encoding);
3142

3243
try {
@@ -46,6 +57,12 @@ module.exports = (options = {}) => {
4657
}
4758

4859
subtree = tree.parser(source);
60+
subtree.options = subtree.options || {};
61+
subtree.options.from = path.isAbsolute(src)
62+
? src
63+
: tree.options.from
64+
? path.relative(tree.options.from, src)
65+
: src;
4966
subtree.match = tree.match;
5067
subtree.parser = tree.parser;
5168
subtree.messages = tree.messages;

0 commit comments

Comments
 (0)