index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var Prism = require('prismjs');
  2. var languages = require('prismjs').languages;
  3. var path = require('path');
  4. var fs = require('fs');
  5. var cheerio = require('cheerio');
  6. var mkdirp = require('mkdirp');
  7. var DEFAULT_LANGUAGE = 'markup';
  8. var MAP_LANGUAGES = {
  9. 'py': 'python',
  10. 'js': 'javascript',
  11. 'rb': 'ruby',
  12. 'cs': 'csharp',
  13. 'html': 'markup'
  14. };
  15. function getAssets() {
  16. var cssFiles = this.config.get('pluginsConfig.prism.css', []);
  17. var cssFolder = null;
  18. var cssNames = [];
  19. var cssName = null;
  20. if (cssFiles.length === 0) {
  21. cssFiles.push('prismjs/themes/prism.css');
  22. }
  23. cssFiles.forEach(function(cssFile) {
  24. var cssPath = require.resolve(cssFile);
  25. cssFolder = path.dirname(cssPath);
  26. cssName = path.basename(cssPath);
  27. cssNames.push(cssName);
  28. });
  29. return {
  30. assets: cssFolder,
  31. css: cssNames
  32. };
  33. }
  34. module.exports = {
  35. book: getAssets,
  36. ebook: function() {
  37. // Adding prism-ebook.css to the CSS collection forces Gitbook
  38. // reference to it in the html markup that is converted into a PDF.
  39. var assets = getAssets.call(this);
  40. assets.css.push('prism-ebook.css');
  41. return assets;
  42. },
  43. blocks: {
  44. code: function(block) {
  45. var highlighted = '';
  46. // Normalize language id
  47. var lang = block.kwargs.language || DEFAULT_LANGUAGE;
  48. lang = MAP_LANGUAGES[lang] || lang;
  49. // Try and find the language definition in components folder
  50. if (!languages[lang]) {
  51. try {
  52. require('prismjs/components/prism-' + lang + '.js');
  53. } catch (e) {
  54. console.warn('Failed to load prism syntax: ' + lang);
  55. console.warn(e);
  56. }
  57. }
  58. if (!languages[lang]) lang = DEFAULT_LANGUAGE;
  59. // Check against html, prism "markup" works for this
  60. if (lang === 'html') {
  61. lang = 'markup';
  62. }
  63. try {
  64. // The process can fail (failed to parse)
  65. highlighted = Prism.highlight(block.body, languages[lang]);
  66. } catch (e) {
  67. console.warn('Failed to highlight:');
  68. console.warn(e);
  69. highlighted = block.body;
  70. }
  71. return highlighted;
  72. }
  73. },
  74. hooks: {
  75. // Manually copy prism-ebook.css into the temporary directory that Gitbook uses for inlining
  76. // styles from this plugin. The getAssets() (above) function can't be leveraged because
  77. // ebook-prism.css lives outside the folder referenced by this plugin's config.
  78. //
  79. // @Inspiration https://github.com/GitbookIO/plugin-styles-less/blob/master/index.js#L8
  80. init: function() {
  81. var book = this;
  82. if (book.output.name !== 'ebook') {
  83. // Logic below does not apply to non-ebook formats
  84. return;
  85. }
  86. var outputDirectory = path.join(book.output.root(), '/gitbook/gitbook-plugin-prism');
  87. var outputFile = path.resolve(outputDirectory, 'prism-ebook.css');
  88. var inputFile = path.resolve(__dirname, './prism-ebook.css');
  89. mkdirp.sync(outputDirectory);
  90. try {
  91. fs.writeFileSync(outputFile, fs.readFileSync(inputFile));
  92. } catch (e) {
  93. console.warn('Failed to write prism-ebook.css. See https://git.io/v1LHY for side effects.');
  94. console.warn(e);
  95. }
  96. },
  97. page: function(page) {
  98. var highlighted = false;
  99. var $ = cheerio.load(page.content);
  100. // Prism css styles target the <code> and <pre> blocks using
  101. // a substring CSS selector:
  102. //
  103. // code[class*="language-"], pre[class*="language-"]
  104. //
  105. // Adding "language-" to <pre> element should be sufficient to trigger
  106. // correct color theme.
  107. $('pre').each(function() {
  108. highlighted = true;
  109. const $this = $(this);
  110. $this.addClass('language-');
  111. });
  112. if (highlighted) {
  113. page.content = $.html();
  114. }
  115. return page;
  116. }
  117. }
  118. };