Browse Source

Merge pull request #3 from SamyPesse/update/gitbook_2.4.0

Define "code" block
Dan Abramov 8 years ago
parent
commit
7ace0a4a72
3 changed files with 54 additions and 33 deletions
  1. 2 5
      README.md
  2. 44 20
      index.js
  3. 8 8
      package.json

+ 2 - 5
README.md

@@ -1,9 +1,6 @@
 Gitbook Plugin for [Prism](http://prismjs.com/)
 ==============
 
-Currently only supports JavaScript.  
-Pull requests to fix this are welcome.
-
 <table>
   <tr>
     <td>
@@ -25,11 +22,11 @@ Pull requests to fix this are welcome.
 
 ## Usage
 
-Add the plugin to your `book.json`:
+Add the plugin to your `book.json`, and disable default GitBook code highlighting:
 
 ```
 {
-  "plugins": ["prism"]
+  "plugins": ["prism", "-highlight"]
 }
 ```
 

+ 44 - 20
index.js

@@ -1,28 +1,52 @@
 var Prism = require('prismjs');
-var cheerio = require('cheerio');
+var languages = require('prism-languages');
 var path = require('path');
 
 var prismCSS = require.resolve('prismjs/themes/prism.css');
 
+var DEFAULT_LANGUAGE = 'markup';
+var MAP_LANGUAGES = {
+  'py': 'python',
+  'js': 'javascript',
+  'json': 'javascript',
+  'rb': 'ruby',
+  'csharp': 'cs',
+  'html': 'markup'
+};
+
+var assets = {
+  assets: path.dirname(prismCSS),
+  css: [path.basename(prismCSS)]
+};
+
 module.exports = {
-  book: {
-    assets: path.dirname(prismCSS),
-    css: [path.basename(prismCSS)]
-  },
-  hooks: {
-    page: function (page) {
-      page.sections.forEach(function (section) {
-        var $ = cheerio.load(section.content);
-
-        $('code').each(function() {
-          var text = $(this).text();
-          var highlighted = Prism.highlight(text, Prism.languages.javascript);
-          $(this).html(highlighted);
-        });
-
-        section.content = $.html();
-      });
-      return page;
+  book: assets,
+  ebook: assets,
+  blocks: {
+    code: function(block) {
+      var highlighted = '';
+
+      // Normalize language id
+      var lang = block.kwargs.language || DEFAULT_LANGUAGE;
+      lang = MAP_LANGUAGES[lang] || lang;
+      if (!languages[lang]) lang = DEFAULT_LANGUAGE;
+
+      // Check against html, prism "markup" works for this
+      if (lang === 'html') {
+        lang = 'markup';
+      }
+
+      try {
+        // The process can fail (failed to parse)
+        highlighted = Prism.highlight(block.body, languages[lang]);
+      }
+      catch(e) {
+        console.warn('Failed to highlight:');
+        console.warn(e);
+        highlighted = block.body;
+      }
+
+      return highlighted;
     }
   }
-};
+};

+ 8 - 8
package.json

@@ -1,22 +1,22 @@
 {
   "name": "gitbook-plugin-prism",
-  "description": "google-code-prettify for gitbook",
+  "description": "Prism highlighting for gitbook",
   "main": "index.js",
   "version": "0.1.1",
   "engines": {
-    "gitbook": "*"
+    "gitbook": ">=2.4.1"
   },
-  "homepage": "https://github.com/gaearon/githook-prism",
+  "homepage": "https://github.com/gaearon/githook-plugin-prism",
   "repository": {
     "type": "git",
-    "url": "https://github.com/gaearon/prism.git"
+    "url": "https://github.com/gaearon/githook-plugin-prism.git"
   },
-  "license": "Apache 2",
+  "license": "Apache-2.0",
   "bugs": {
-    "url": "https://github.com/gaearon/prism/issues"
+    "url": "https://github.com/gaearon/githook-plugin-prism/issues"
   },
   "dependencies": {
-    "cheerio": "^0.19.0",
-    "prismjs": "0.0.1"
+    "prismjs": "0.0.1",
+    "prism-languages": "^0.1.3"
   }
 }