mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 18:13:29 +00:00
Check parser used before applying template shortcuts
This commit is contained in:
+11
-5
@@ -203,8 +203,9 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
// Add shortcuts
|
||||
if (!_.isArray(block.shortcuts)) block.shortcuts = [block.shortcuts];
|
||||
_.each(block.shortcuts, function(shortcut) {
|
||||
this.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"'");
|
||||
this.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"' for parsers ", shortcut.parsers);
|
||||
this.shortcuts.push({
|
||||
parsers: shortcut.parsers,
|
||||
start: shortcut.start,
|
||||
end: shortcut.end,
|
||||
tag: {
|
||||
@@ -216,7 +217,8 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
};
|
||||
|
||||
// Apply a shortcut to a string
|
||||
TemplateEngine.prototype._applyShortcut = function(content, shortcut) {
|
||||
TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) {
|
||||
if (!_.contains(shortcut.parsers, parser)) return content;
|
||||
var regex = new RegExp(
|
||||
stringUtils.escapeRegex(shortcut.start) + "\\s*([\\s\\S]*?[^\\$])\\s*" + stringUtils.escapeRegex(shortcut.end),
|
||||
'g'
|
||||
@@ -237,11 +239,14 @@ TemplateEngine.prototype.renderString = function(content, context, options) {
|
||||
version: pkg.version
|
||||
}
|
||||
});
|
||||
options = _.defaults(options || {}, { path: null});
|
||||
options = _.defaults(options || {}, {
|
||||
path: null,
|
||||
type: null
|
||||
});
|
||||
if (options.path) options.path = this.book.resolve(options.path);
|
||||
|
||||
// Replace shortcuts
|
||||
content = _.reduce(this.shortcuts, this._applyShortcut.bind(this), content);
|
||||
content = _.reduce(this.shortcuts, _.partial(this._applyShortcut.bind(this), options.type), content);
|
||||
|
||||
return Q.nfcall(this.env.renderString.bind(this.env), content, context, options);
|
||||
};
|
||||
@@ -273,7 +278,8 @@ TemplateEngine.prototype.renderPage = function(page) {
|
||||
};
|
||||
|
||||
return that.renderString(page.content, context, {
|
||||
path: page.path
|
||||
path: page.path,
|
||||
type: page.type
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
+3
-1
@@ -100,7 +100,9 @@ describe('Plugins', function () {
|
||||
|
||||
it('should correctly accept shortcuts', function(done) {
|
||||
qdone(
|
||||
books[0].template.renderString('$$hello$$')
|
||||
books[0].template.renderString('$$hello$$', {}, {
|
||||
type: "markdown"
|
||||
})
|
||||
.then(function(content) {
|
||||
assert.equal(content, "testhellotest");
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user