mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
use native path
This commit is contained in:
@@ -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`
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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.');
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user