Skip to content

Commit 5cae346

Browse files
committed
breaking: add cwd property to complement root
1 parent 69789e2 commit 5cae346

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/index.js

Lines changed: 13 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 {

0 commit comments

Comments
 (0)