|
@@ -1,7 +1,9 @@
|
|
|
var Prism = require('prismjs');
|
|
|
var languages = require('prismjs').languages;
|
|
|
var path = require('path');
|
|
|
-const cheerio = require('cheerio');
|
|
|
+var fs = require('fs');
|
|
|
+var cheerio = require('cheerio');
|
|
|
+var mkdirp = require('mkdirp');
|
|
|
|
|
|
var DEFAULT_LANGUAGE = 'markup';
|
|
|
var MAP_LANGUAGES = {
|
|
@@ -30,17 +32,23 @@ function getAssets() {
|
|
|
cssNames.push(cssName);
|
|
|
});
|
|
|
|
|
|
- var assets = {
|
|
|
+ return {
|
|
|
assets: cssFolder,
|
|
|
css: cssNames
|
|
|
};
|
|
|
-
|
|
|
- return assets;
|
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
|
book: getAssets,
|
|
|
- ebook: getAssets,
|
|
|
+ ebook: function() {
|
|
|
+
|
|
|
+ // Adding prism-ebook.css to the CSS collection forces Gitbook
|
|
|
+ // reference to it in the html markup that is converted into a PDF.
|
|
|
+ var assets = getAssets.call(this);
|
|
|
+ assets.css.push('prism-ebook.css');
|
|
|
+ return assets;
|
|
|
+
|
|
|
+ },
|
|
|
blocks: {
|
|
|
code: function(block) {
|
|
|
|
|
@@ -77,10 +85,39 @@ module.exports = {
|
|
|
}
|
|
|
|
|
|
return highlighted;
|
|
|
+
|
|
|
}
|
|
|
},
|
|
|
hooks: {
|
|
|
- 'page': function(page) {
|
|
|
+
|
|
|
+ // Manually copy prism-ebook.css into the temporary directory that Gitbook uses for inlining
|
|
|
+ // styles from this plugin. The getAssets() (above) function can't be leveraged because
|
|
|
+ // ebook-prism.css lives outside the folder referenced by this plugin's config.
|
|
|
+ //
|
|
|
+ // @Inspiration https://github.com/GitbookIO/plugin-styles-less/blob/master/index.js#L8
|
|
|
+ init: function() {
|
|
|
+
|
|
|
+ var book = this;
|
|
|
+
|
|
|
+ if (book.output.name !== 'ebook') {
|
|
|
+ // Logic below does not apply to non-ebook formats
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var outputDirectory = path.join(book.output.root(), '/gitbook/gitbook-plugin-prism');
|
|
|
+ var outputFile = path.resolve(outputDirectory, 'prism-ebook.css');
|
|
|
+ var inputFile = path.resolve(__dirname, './prism-ebook.css');
|
|
|
+ mkdirp.sync(outputDirectory);
|
|
|
+
|
|
|
+ try {
|
|
|
+ fs.writeFileSync(outputFile, fs.readFileSync(inputFile));
|
|
|
+ } catch (e) {
|
|
|
+ console.warn('Failed to write prism-ebook.css. See https://git.io/v1LHY for side effects.');
|
|
|
+ console.warn(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+ page: function(page) {
|
|
|
|
|
|
var highlighted = false;
|
|
|
|
|
@@ -91,13 +128,12 @@ module.exports = {
|
|
|
//
|
|
|
// code[class*="language-"], pre[class*="language-"]
|
|
|
//
|
|
|
- // Adding "language-" to each element should be sufficient to trigger
|
|
|
+ // Adding "language-" to <pre> element should be sufficient to trigger
|
|
|
// correct color theme.
|
|
|
- $('code, pre').each(function() {
|
|
|
+ $('pre').each(function() {
|
|
|
highlighted = true;
|
|
|
const $this = $(this);
|
|
|
$this.addClass('language-');
|
|
|
-
|
|
|
});
|
|
|
|
|
|
if (highlighted) {
|