Browse Source

closes #24: Compatibility with Gitbook 2.x.x

Rob McGuinness 7 years ago
parent
commit
e3d28ef150
3 changed files with 39 additions and 28 deletions
  1. 18 4
      index.js
  2. 1 1
      package.json
  3. 20 23
      test.js

+ 18 - 4
index.js

@@ -31,9 +31,24 @@ function requireSyntax(lang) {
   require('prismjs/components/prism-' + lang + '.js');
 }
 
+function getConfig(context, property, defaultValue) {
+  var config = context.config ? /* 3.x */ context.config : /* 2.x */ context.book.config;
+  return config.get(property, defaultValue);
+}
+
+function isEbook(book) {
+  // 2.x
+  if (book.options && book.options.generator) {
+    return book.options.generator === 'ebook';
+  }
+
+  // 3.x
+  return book.output.name === 'ebook';
+}
+
 function getAssets() {
 
-  var cssFiles = this.config.get('pluginsConfig.prism.css', []);
+  var cssFiles = getConfig(this, 'pluginsConfig.prism.css', []);
   var cssFolder = null;
   var cssNames = [];
   var cssName = null;
@@ -70,7 +85,7 @@ module.exports = {
     code: function(block) {
 
       var highlighted = '';
-      var userDefined = this.config.get('pluginsConfig.prism.lang', {});
+      var userDefined = getConfig(this, 'pluginsConfig.prism.lang', {});
 
       // Normalize language id
       var lang = block.kwargs.language || DEFAULT_LANGUAGE;
@@ -117,8 +132,7 @@ module.exports = {
 
       var book = this;
 
-      if (book.output.name !== 'ebook') {
-        // Logic below does not apply to non-ebook formats
+      if (!isEbook(book)) {
         return;
       }
 

+ 1 - 1
package.json

@@ -8,7 +8,7 @@
     "test": "npm run lint && tape test.js"
   },
   "engines": {
-    "gitbook": ">=2.4.1"
+    "gitbook": ">=2.4.1 <4.0.0"
   },
   "homepage": "https://github.com/gaearon/gitbook-plugin-prism",
   "repository": {

+ 20 - 23
test.js

@@ -1,44 +1,41 @@
 var tester = require('gitbook-tester');
 var test = require('tape');
 
-var pkg = require('./package.json');
-
-test('highlight javascript code block', function (t) {
-
-  t.plan(1);
+function expect(t, version, content, expected) {
 
   tester.builder()
-    .withContent('```js\nfunction() { return true };\n```')
+    .withContent(content)
     .withLocalPlugin(__dirname)
     .withBookJson({
-      gitbook: pkg.engines.gitbook,
+      gitbook: version,
       plugins: ['prism', '-highlight']
     })
     .create()
     .then(function(result) {
-      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></code></pre>';
-      t.equal(result[0].content, expected);
+      t.equal(result[0].content, expected, 'gitbook version ' + version);
     })
     .done();
 
+}
+
+test('highlight javascript code block', function (t) {
+
+  t.plan(2);
+
+  var content = '```js\nfunction() { return true };\n```';
+  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>';
+  expect(t, '>=2.4.1', content, expected);
+  expect(t, '>=3.0.0', content, expected);
+
 });
 
 test('highlight csharp code using shortcut', function (t) {
 
-  t.plan(1);
+  t.plan(2);
 
-  tester.builder()
-    .withContent('```cs\nusing System; class Program {public static void Main(string[] args) {Console.WriteLine("Hello, world!"); } }\n```')
-    .withLocalPlugin(__dirname)
-    .withBookJson({
-      gitbook: pkg.engines.gitbook,
-      plugins: ['prism', '-highlight']
-    })
-    .create()
-    .then(function(result) {
-      var expected = '<pre class="language-"><code class="lang-cs"><span class="token keyword">using</span> System<span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">Program</span> <span class="token punctuation">{</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">Main</span><span class="token punctuation">(</span><span class="token keyword">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">&quot;Hello, world!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre>';
-      t.equal(result[0].content, expected);
-    })
-    .done();
+  var content = '```cs\nusing System; class Program {public static void Main(string[] args) {Console.WriteLine("Hello, world!"); } }\n```';
+  var expected = '<pre class="language-"><code class="lang-cs"><span class="token keyword">using</span> System<span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">Program</span> <span class="token punctuation">{</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">Main</span><span class="token punctuation">(</span><span class="token keyword">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">&quot;Hello, world!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>\n</code></pre>';
+  expect(t, '>=2.4.1', content, expected);
+  expect(t, '>=3.0.0', content, expected);
 
 });