Skip to content

Commit

Permalink
Feat: absolute path support in template include/import
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Lea <[email protected]>
  • Loading branch information
chigix committed Sep 4, 2018
1 parent 4f458f9 commit 5c55791
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions test/fixtures/tpl-render-result/import-render-nigeru.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<title>逃げる</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>

#nigeru {
background-color: green;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<title>{{ title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
{% include "style.css" %}
{% for style in styleIncludes if styleIncludes else [] %}
{% include style %}
{% endfor %}
Expand Down
55 changes: 38 additions & 17 deletions test/nunjucks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,59 @@ function getNJKEnv(tplDir: string | string[]) {
}

test('TemplateLoader Basic Usage', done => {
const njk_env = getNJKEnv(path.join(CONST.TEST_TEMPLATE_DIR, './basic-render-case'));
njk_env.render('layout/template.njk', {
getNJKEnv(path.join(CONST.TEST_TEMPLATE_DIR, './basic-render-case'))
.render('layout/template.njk', {
title: '逃げる',
body: '逃げちゃだめだ。逃げちゃだめだ。逃げちゃだめだ。',
}, (err, result) => {
expect(err).toBeNull();
if (err) {
console.error(err);
}
expect(result).toEqual(
fs.readFileSync(
path.resolve(CONST.RENDER_CASE_DIR, './basic-render-nigeru.html'),
'utf8'));

return done();
});
});

test('Basic External Source Import Testing', done => {
getNJKEnv([
path.join(CONST.TEST_TEMPLATE_DIR, './import-render-case'),
path.join(CONST.TEST_TEMPLATE_DIR, './import-render-case/source'),
]).render('layout/template.njk', {
title: '逃げる',
body: '逃げちゃだめだ。逃げちゃだめだ。逃げちゃだめだ。',
styleIncludes: [
'style.css',
],
}, (err, result) => {
expect(err).toBeNull();
if (err) {
console.error(err);
}
expect(result).toEqual(
fs.readFileSync(
path.resolve(CONST.RENDER_CASE_DIR, './basic-render-nigeru.html'),
'utf8'));
path.resolve(CONST.RENDER_CASE_DIR, './import-render-nigeru.html')
, 'utf8'));

return done();
});
});

test('Basic External Source Import Testing', done => {
const njk_env = getNJKEnv([
test('Import Normalize Css', done => {
getNJKEnv([
path.join(CONST.TEST_TEMPLATE_DIR, './import-render-case'),
path.join(CONST.TEST_TEMPLATE_DIR, './import-render-case/source'),
]);
njk_env.render('layout/template.njk', {
title: '逃げる',
body: '逃げちゃだめだ。逃げちゃだめだ。逃げちゃだめだ。',
]).render('layout/template.njk', {
title: '守る',
body: 'あなたは死なないわ。私が守るもの',
styleIncludes: [
require.resolve('normalize.css'),
],
}, (err, result) => {
expect(err).toBeNull();
expect(result).toEqual(
fs.readFileSync(
path.resolve(CONST.RENDER_CASE_DIR, './import-render-nigeru.html')
, 'utf8'));
expect.stringContaining(
fs.readFileSync(require.resolve('normalize.css'), 'utf8')));

return done();
});
Expand Down
22 changes: 12 additions & 10 deletions tools/njk-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ module.exports = {
}
},
getSource: function (name, callback) {
let full_path = null;
const paths = this.searchPaths;

for (let i = 0; i < paths.length; i++) {
const basePath = path.resolve(paths[i]);
const p = path.resolve(paths[i], name); // Only allow the current directory and anything
// underneath it to be searched
if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
full_path = p;
break;
const full_path = (function () {
if (path.isAbsolute(name) && fs.existsSync(name)) {
return name;
}
}
for (let i = 0; i < paths.length; i++) {
const basePath = path.resolve(paths[i]);
const p = path.resolve(paths[i], name); // Only allow the current directory and anything
// underneath it to be searched
if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
return p;
}
}
})();

if (!full_path) {
return callback(null, null);
Expand Down

0 comments on commit 5c55791

Please sign in to comment.