mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 19:06:31 +00:00
Add JS context for template
This commit is contained in:
+1
-11
@@ -21,18 +21,8 @@ function satisfies(condition) {
|
||||
return semver.satisfies(VERSION_STABLE, condition);
|
||||
}
|
||||
|
||||
// Return templating/json context for gitbook itself
|
||||
function getContext() {
|
||||
return {
|
||||
gitbook: {
|
||||
version: pkg.version,
|
||||
time: START_TIME
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
version: pkg.version,
|
||||
satisfies: satisfies,
|
||||
getContext: getContext
|
||||
START_TIME: START_TIME
|
||||
};
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
var gitbook = require('../gitbook');
|
||||
|
||||
var encodeSummary = require('./encodeSummary');
|
||||
var encodeGlossary = require('./encodeGlossary');
|
||||
var encodeReadme = require('./encodeReadme');
|
||||
@@ -12,7 +14,11 @@ function encodeBookToJson(book) {
|
||||
return {
|
||||
summary: encodeSummary(book.getSummary()),
|
||||
glossary: encodeGlossary(book.getGlossary()),
|
||||
readme: encodeReadme(book.getReadme())
|
||||
readme: encodeReadme(book.getReadme()),
|
||||
gitbook: {
|
||||
version: gitbook.version,
|
||||
time: gitbook.START_TIME
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
var path = require('path');
|
||||
var omit = require('omit-keys');
|
||||
|
||||
var Templating = require('../../templating');
|
||||
var JSONUtils = require('../../json');
|
||||
var LocationUtils = require('../../utils/location');
|
||||
var Modifiers = require('../modifiers');
|
||||
var writeFile = require('../helper/writeFile');
|
||||
var getModifiers = require('../getModifiers');
|
||||
@@ -16,22 +20,39 @@ function onPage(output, page) {
|
||||
var options = output.getOptions();
|
||||
var file = page.getFile();
|
||||
var prefix = options.get('prefix');
|
||||
var book = output.getBook();
|
||||
var config = book.getConfig();
|
||||
|
||||
var engine = createTemplateEngine(output, page.getPath());
|
||||
|
||||
// Output file path
|
||||
var filePath = fileToOutput(output, file.getPath());
|
||||
|
||||
// Calcul relative path to the root
|
||||
var outputDirName = path.dirname(filePath);
|
||||
var basePath = LocationUtils.normalize(path.relative(outputDirName, './'));
|
||||
|
||||
return Modifiers.modifyHTML(page, getModifiers(output, page))
|
||||
.then(function(resultPage) {
|
||||
// Generate the context
|
||||
var context = JSONUtils.encodeBookWithPage(output.getBook(), resultPage);
|
||||
context.config = config.getValues().toJS();
|
||||
|
||||
context.template = {
|
||||
getJSContext: function() {
|
||||
return {};
|
||||
return {
|
||||
page: omit(context.page, 'content'),
|
||||
config: context.config,
|
||||
file: context.file,
|
||||
gitbook: context.gitbook,
|
||||
basePath: basePath,
|
||||
book: {
|
||||
language: null //context.book.language
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Output file path
|
||||
var filePath = fileToOutput(output, file.getPath());
|
||||
|
||||
// Render the theme
|
||||
return Templating.renderFile(engine, prefix + '/page.html', context)
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
"nunjucks": "2.4.1",
|
||||
"nunjucks-do": "1.0.0",
|
||||
"object-path": "^0.9.2",
|
||||
"omit-keys": "^0.1.0",
|
||||
"q": "1.4.1",
|
||||
"read-installed": "^4.0.3",
|
||||
"request": "2.70.0",
|
||||
|
||||
Reference in New Issue
Block a user