|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
};
|