mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add method renderInline and renderBlock to book
This commit is contained in:
+18
-1
@@ -320,7 +320,7 @@ Book.prototype.findParsableFile = function(filename) {
|
||||
if (!realFilepath) return null;
|
||||
|
||||
return {
|
||||
parser: parsers.get(ext),
|
||||
parser: parsers.getByExt(ext),
|
||||
path: realFilepath
|
||||
};
|
||||
});
|
||||
@@ -359,6 +359,23 @@ Book.prototype.isInLanguageBook = function(filename) {
|
||||
});
|
||||
};
|
||||
|
||||
// ----- Parser Methods
|
||||
|
||||
// Render a markup string in inline mode
|
||||
Book.prototype.renderInline = function(type, src) {
|
||||
var parser = parsers.get(type);
|
||||
return parser.inline(src)
|
||||
.get('content');
|
||||
};
|
||||
|
||||
// Render a markup string in block mode
|
||||
Book.prototype.renderBlock = function(type, src) {
|
||||
var parser = parsers.get(type);
|
||||
return parser.page(src)
|
||||
.get('content');
|
||||
};
|
||||
|
||||
|
||||
// ----- DEPRECATED METHODS
|
||||
|
||||
Book.prototype.contentLink = error.deprecateMethod(function(s) {
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ function Page(book, filename) {
|
||||
|
||||
// Can we parse it?
|
||||
extension = path.extname(this.path);
|
||||
this.parser = parsers.get(extension);
|
||||
this.parser = parsers.getByExt(extension);
|
||||
if (!this.parser) throw error.ParsingError(new Error('Can\'t parse file "'+this.path+'"'));
|
||||
|
||||
this.type = this.parser.name;
|
||||
|
||||
+11
-1
@@ -37,11 +37,20 @@ function createParser(parser, base) {
|
||||
nparser.page = Promise.wrapfn(parser.page);
|
||||
nparser.page.prepare = Promise.wrapfn(parser.page.prepare || _.identity);
|
||||
|
||||
nparser.inline = Promise.wrapfn(parser.inline);
|
||||
|
||||
return nparser;
|
||||
}
|
||||
|
||||
// Return a specific parser
|
||||
function getParser(name) {
|
||||
return _.find(PARSERS, {
|
||||
name: name
|
||||
});
|
||||
}
|
||||
|
||||
// Return a specific parser according to an extension
|
||||
function getParser(ext) {
|
||||
function getParserByExt(ext) {
|
||||
return _.find(PARSERS, function(input) {
|
||||
return input.name == ext || _.contains(input.extensions, ext);
|
||||
});
|
||||
@@ -56,5 +65,6 @@ module.exports = {
|
||||
all: PARSERS,
|
||||
extensions: _.flatten(_.pluck(PARSERS, 'extensions')),
|
||||
get: getParser,
|
||||
getByExt: getParserByExt,
|
||||
getForFile: getParserForFile
|
||||
};
|
||||
|
||||
@@ -80,7 +80,7 @@ TemplateEngine.prototype.bindContext = function(func) {
|
||||
|
||||
// Interpolate a string content to replace shortcuts according to the filetype
|
||||
TemplateEngine.prototype.interpolate = function(filepath, source) {
|
||||
var parser = parsers.get(path.extname(filepath));
|
||||
var parser = parsers.getByExt(path.extname(filepath));
|
||||
var type = parser? parser.name : null;
|
||||
|
||||
return this.applyShortcuts(type, source);
|
||||
|
||||
+2
-2
@@ -19,8 +19,8 @@
|
||||
"escape-string-regexp": "1.0.5",
|
||||
"eslint": "^2.2.0",
|
||||
"front-matter": "2.0.6",
|
||||
"gitbook-asciidoc": "1.0.2",
|
||||
"gitbook-markdown": "1.0.3",
|
||||
"gitbook-asciidoc": "1.1.0",
|
||||
"gitbook-markdown": "1.2.0",
|
||||
"gitbook-plugin-fontsettings": "1.0.2",
|
||||
"gitbook-plugin-highlight": "2.0.0",
|
||||
"gitbook-plugin-livereload": "0.0.1",
|
||||
|
||||
Reference in New Issue
Block a user