Browse Source

Use prism-languages

Samy Pessé 9 years ago
parent
commit
bfa427a1d4
2 changed files with 23 additions and 5 deletions
  1. 21 4
      index.js
  2. 2 1
      package.json

+ 21 - 4
index.js

@@ -1,15 +1,17 @@
 var Prism = require('prismjs');
+var languages = require('prism-languages');
 var path = require('path');
 
 var prismCSS = require.resolve('prismjs/themes/prism.css');
 
-var DEFAULT_LANGUAGE = 'javascript';
+var DEFAULT_LANGUAGE = 'markup';
 var MAP_LANGUAGES = {
   'py': 'python',
   'js': 'javascript',
   'json': 'javascript',
   'rb': 'ruby',
   'csharp': 'cs',
+  'html': 'markup'
 };
 
 var assets = {
@@ -22,14 +24,29 @@ module.exports = {
   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';
+      }
 
-      // Get languages from prism
-      lang = Prism.languages[lang] || Prism.languages[DEFAULT_LANGUAGE];
+      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 Prism.highlight(block.body, lang);
+      return highlighted;
     }
   }
 };

+ 2 - 1
package.json

@@ -16,6 +16,7 @@
     "url": "https://github.com/gaearon/githook-plugin-prism/issues"
   },
   "dependencies": {
-    "prismjs": "0.0.1"
+    "prismjs": "0.0.1",
+    "prism-languages": "^0.1.3"
   }
 }