Browse Source

Merge pull request #12 from gaearon/feature/pdf-highlight-issue

PDF Syntax Highlighting Issue
robert mcguinness 7 years ago
parent
commit
149b3b5302
4 changed files with 68 additions and 14 deletions
  1. 45 9
      index.js
  2. 5 4
      package.json
  3. 17 0
      prism-ebook.css
  4. 1 1
      test.js

+ 45 - 9
index.js

@@ -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) {

+ 5 - 4
package.json

@@ -20,17 +20,18 @@
     "url": "https://github.com/gaearon/gitbook-plugin-prism/issues"
   },
   "dependencies": {
-    "prismjs": "1.5.1",
-    "cheerio": "0.22.0"
+    "cheerio": "0.22.0",
+    "mkdirp": "0.5.1",
+    "prismjs": "1.5.1"
   },
   "devDependencies": {
-    "eslint": "3.10.2",
+    "eslint": "3.11.1",
     "eslint-config-semistandard": "7.0.0",
     "eslint-config-standard": "6.2.1",
     "eslint-plugin-promise": "3.4.0",
     "eslint-plugin-react": "6.7.1",
     "eslint-plugin-standard": "2.0.1",
     "gitbook-tester": "1.4.3",
-    "tape": "4.6.2"
+    "tape": "4.6.3"
   }
 }

+ 17 - 0
prism-ebook.css

@@ -0,0 +1,17 @@
+/*
+ * Prism themes contain a ::selection selector that was being applied to <code>
+ * blocks in PDFs.
+ *
+ * @See https://github.com/gaearon/gitbook-plugin-prism/issues/11
+*/
+pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
+code[class*="language-"]::selection, code[class*="language-"] ::selection {
+  background: none;
+}
+
+/*
+ * @See https://github.com/gaearon/gitbook-plugin-prism/issues/11#issuecomment-262058733
+*/
+.page .section pre code {
+    background-color: none;
+}

+ 1 - 1
test.js

@@ -16,7 +16,7 @@ test('should highlight javascript code block', function (t) {
     })
     .create()
     .then(function(result) {
-      var expected = '<pre class="language-"><code class="lang-js language-"><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre>';
+      var expected = '<pre class="language-"><code class="lang-js"><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>\n</code></pre>';
       t.equal(result[0].content, expected);
     })
     .done();