mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Create new loader for nunjucks to avoid infinite loop
This commit is contained in:
+5
-11
@@ -8,13 +8,10 @@ var Promise = require('../utils/promise');
|
||||
var location = require('../utils/location');
|
||||
var fs = require('../utils/fs');
|
||||
var defaultFilters = require('../template/filters');
|
||||
var FSLoader = require('../template/fs-loader');
|
||||
var conrefsLoader = require('./conrefs');
|
||||
var Output = require('./base');
|
||||
|
||||
// Tranform a theme ID into a plugin
|
||||
function themeID(plugin) {
|
||||
return 'theme-' + plugin;
|
||||
}
|
||||
|
||||
// Directory for a theme with the templates
|
||||
function templatesPath(dir) {
|
||||
@@ -58,7 +55,7 @@ WebsiteOutput.prototype.prepare = function() {
|
||||
|
||||
.then(function() {
|
||||
// This list is ordered to give priority to templates in the book
|
||||
var searchPaths = _.pluck(this.plugins.list(), 'root');
|
||||
var searchPaths = _.pluck(that.plugins.list(), 'root');
|
||||
|
||||
// The book itself can contains a "_layouts" folder
|
||||
searchPaths.unshift(that.book.root);
|
||||
@@ -71,7 +68,7 @@ WebsiteOutput.prototype.prepare = function() {
|
||||
that.i18n.load(i18nRoot);
|
||||
});
|
||||
|
||||
that.env = new nunjucks.Environment(new nunjucks.FileSystemLoader(_.map(searchPaths, templatesPath)));
|
||||
that.env = new nunjucks.Environment(new FSLoader(_.map(searchPaths, templatesPath)));
|
||||
|
||||
// Add GitBook default filters
|
||||
_.each(defaultFilters, function(fn, filter) {
|
||||
@@ -212,13 +209,10 @@ WebsiteOutput.prototype.outputMultilingualIndex = function() {
|
||||
// Templates are stored in `_layouts` folders
|
||||
WebsiteOutput.prototype.render = function(tpl, context) {
|
||||
var filename = this.templateName(tpl);
|
||||
|
||||
context = _.extend(context, {
|
||||
template: {
|
||||
// Same template but in the default theme
|
||||
default: this.themeDefault? path.resolve(templatesPath(this.themeDefault.root), filename) : null,
|
||||
|
||||
// Same template but in the theme
|
||||
theme: path.resolve(templatesPath(this.theme.root), filename)
|
||||
self: filename
|
||||
},
|
||||
|
||||
plugins: {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
var _ = require('lodash');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var nunjucks = require('nunjucks');
|
||||
|
||||
/*
|
||||
Nunjucks loader similar to FileSystemLoader, but avoid infinite looping
|
||||
*/
|
||||
|
||||
var Loader = nunjucks.Loader.extend({
|
||||
init: function(searchPaths) {
|
||||
this.searchPaths = searchPaths.map(path.normalize);
|
||||
},
|
||||
|
||||
getSource: function(fullpath) {
|
||||
fullpath = this.resolve(null, fullpath);
|
||||
|
||||
if(!fullpath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
src: fs.readFileSync(fullpath, 'utf-8'),
|
||||
path: fullpath,
|
||||
noCache: true
|
||||
};
|
||||
},
|
||||
|
||||
resolve: function(from, to) {
|
||||
var resultFolder = _.find(this.searchPaths, function(basePath) {
|
||||
var p = path.resolve(basePath, to);
|
||||
|
||||
return (
|
||||
p.indexOf(basePath) === 0
|
||||
&& p != from
|
||||
&& fs.existsSync(p)
|
||||
);
|
||||
});
|
||||
|
||||
return path.resolve(resultFolder, to);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Loader;
|
||||
Reference in New Issue
Block a user