diff --git a/README.md b/README.md index 9815a1bb3..04bd475de 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ engine.renderFile("hello", {name: 'alice'}) * `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`. If an array, the files are looked up in the order they occur in the array. -Defaults to `["."]` +Defaults to `process.cwd()` * `extname` is used to lookup the template file when filepath doesn't include an extension name. Defaults to `.liquid` diff --git a/index.js b/index.js index 0f46765fe..f6dd36bd1 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const assert = require('./src/util/assert.js'); const tokenizer = require('./src/tokenizer.js'); const statFileAsync = require('./src/util/fs.js').statFileAsync; const readFileAsync = require('./src/util/fs.js').readFileAsync; -const pathResolve = require('./src/util/fs.js').pathResolve; +const path = require('path'); const Render = require('./src/render.js'); const lexical = require('./src/lexical.js'); const Tag = require('./src/tag.js'); @@ -73,7 +73,7 @@ var _engine = { }, lookup: function(filepath, root) { root = this.options.root.concat(root || []); - var paths = root.map(root => pathResolve(root, filepath)); + var paths = root.map(root => path.resolve(root, filepath)); return anySeries(paths, path => statFileAsync(path).then(() => path)) .catch((e) => { if (e.code === 'ENOENT') { @@ -83,7 +83,7 @@ var _engine = { }); }, getTemplate: function(filepath, root) { - if (!filepath.match(/\.\w+$/)) { + if(!path.extname(filepath)){ filepath += this.options.extname; } return this diff --git a/makefile b/makefile index df6ec7544..44e330c3c 100644 --- a/makefile +++ b/makefile @@ -8,6 +8,7 @@ default: dist: [ -d dist/ ] || mkdir dist/ $(BROWSERIFY) index.js -s Liquid \ + --ignore path \ -t [ babelify --global true --presets [ es2015 ] ] \ > dist/liquid.js $(MINIFY) dist/liquid.js --compress warnings=false --mangle --output dist/liquid.min.js diff --git a/src/util/fs.js b/src/util/fs.js index c080211cb..ed67ec4ad 100644 --- a/src/util/fs.js +++ b/src/util/fs.js @@ -14,21 +14,7 @@ function statFileAsync(path) { }); }; -function pathResolve(root, path) { - if (path[0] == '/') return path; - - var arr = root.split('/').concat(path.split('/')); - var result = []; - arr.forEach(function(slug) { - if (slug == '..') result.pop(); - else if (!slug || slug == '.'); - else result.push(slug); - }); - return '/' + result.join('/'); -} - module.exports = { readFileAsync, - pathResolve, statFileAsync }; diff --git a/test/liquid.js b/test/liquid.js index 92662ddb9..593f61186 100644 --- a/test/liquid.js +++ b/test/liquid.js @@ -96,6 +96,17 @@ describe('liquid', function() { return expect(engine.renderFile('files/foo.html')) .to.eventually.equal('foo'); }); + it('should default root to cwd', function(){ + var files = {}; + files[process.cwd() + '/foo.html'] = 'FOO'; + mock(files); + + engine = Liquid({ + extname: '.html' + }); + return expect(engine.renderFile('foo.html')) + .to.eventually.equal('FOO'); + }); it('should render file with context', function() { return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.'); }); diff --git a/test/util/fs.js b/test/util/fs.js deleted file mode 100644 index 433d353bc..000000000 --- a/test/util/fs.js +++ /dev/null @@ -1,22 +0,0 @@ -const chai = require("chai"); -const expect = chai.expect; - -const fs = require('../../src/util/fs.js'); -const pathResolve = fs.pathResolve; - -describe('fs', function() { - describe('.pathResolve(root, path)', function() { - it('should accept root with no trailing slash', function() { - expect(pathResolve('/root', 'files/foo.html')).to.equal('/root/files/foo.html'); - }); - it('should accept root with trailing slash', function() { - expect(pathResolve('/root/', 'files/foo.html')).to.equal('/root/files/foo.html'); - }); - it('should accept dot path', function() { - expect(pathResolve('/root', './foo.html')).to.equal('/root/foo.html'); - }); - it('should accept double-dot path', function() { - expect(pathResolve('/root', 'files/../foo.html')).to.equal('/root/foo.html'); - }); - }); -});