index.js 763 B

1234567891011121314151617181920212223242526272829303132333435
  1. var Prism = require('prismjs');
  2. var path = require('path');
  3. var prismCSS = require.resolve('prismjs/themes/prism.css');
  4. var DEFAULT_LANGUAGE = 'javascript';
  5. var MAP_LANGUAGES = {
  6. 'py': 'python',
  7. 'js': 'javascript',
  8. 'json': 'javascript',
  9. 'rb': 'ruby',
  10. 'csharp': 'cs',
  11. };
  12. var assets = {
  13. assets: path.dirname(prismCSS),
  14. css: [path.basename(prismCSS)]
  15. };
  16. module.exports = {
  17. book: assets,
  18. ebook: assets,
  19. blocks: {
  20. code: function(block) {
  21. // Normalize language id
  22. var lang = block.kwargs.language || DEFAULT_LANGUAGE;
  23. lang = MAP_LANGUAGES[lang] || lang;
  24. // Get languages from prism
  25. lang = Prism.languages[lang] || Prism.languages[DEFAULT_LANGUAGE];
  26. return Prism.highlight(block.body, lang);
  27. }
  28. }
  29. };