22
33const fs = require ( 'fs' ) ;
44const path = require ( 'path' ) ;
5+ const process = require ( 'process' ) ;
56const posthtml = require ( 'posthtml' ) ;
67const { parser} = require ( 'posthtml-parser' ) ;
78const { match} = require ( 'posthtml/lib/api' ) ;
89const expressions = require ( 'posthtml-expressions' ) ;
910
1011module . 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