index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. function __export(m) {
  3. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  4. }
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var hljs = require("highlight.js");
  7. var parse5 = require("parse5");
  8. var theme_1 = require("./theme");
  9. function colorizeNode(node, theme, context) {
  10. if (theme === void 0) { theme = {}; }
  11. switch (node.type) {
  12. case 'text': {
  13. var text = node.data;
  14. if (context === undefined) {
  15. return (theme.default || theme_1.DEFAULT_THEME.default || theme_1.plain)(text);
  16. }
  17. else {
  18. return text;
  19. }
  20. }
  21. case 'tag': {
  22. var hljsClass = /hljs-(\w+)/.exec(node.attribs.class);
  23. if (hljsClass) {
  24. var token_1 = hljsClass[1];
  25. var nodeData = node.childNodes
  26. .map(function (node) { return colorizeNode(node, theme, token_1); })
  27. .join('');
  28. return (theme[token_1] || theme_1.DEFAULT_THEME[token_1] || theme_1.plain)(nodeData);
  29. }
  30. // Return the data itself when the class name isn't prefixed with a highlight.js token prefix.
  31. // This is common in instances of sublanguages (JSX, Markdown Code Blocks, etc.)
  32. return node.childNodes.map(function (node) { return colorizeNode(node, theme); }).join('');
  33. }
  34. }
  35. throw new Error('Invalid node type ' + node.type);
  36. }
  37. function colorize(code, theme) {
  38. if (theme === void 0) { theme = {}; }
  39. var fragment = parse5.parseFragment(code, {
  40. treeAdapter: parse5.treeAdapters.htmlparser2,
  41. });
  42. return fragment.childNodes.map(function (node) { return colorizeNode(node, theme); }).join('');
  43. }
  44. /**
  45. * Apply syntax highlighting to `code` with ASCII color codes. The language is automatically
  46. * detected if not set.
  47. *
  48. * ```ts
  49. * import {highlight} from 'cli-highlight';
  50. * import * as fs from 'fs';
  51. *
  52. * fs.readFile('package.json', 'utf8', (err: any, json: string) => {
  53. * console.log('package.json:');
  54. * console.log(highlight(json));
  55. * });
  56. * ```
  57. *
  58. * @param code The code to highlight
  59. * @param options Optional options
  60. */
  61. function highlight(code, options) {
  62. if (options === void 0) { options = {}; }
  63. var html;
  64. if (options.language) {
  65. html = hljs.highlight(options.language, code, options.ignoreIllegals, options.continuation).value;
  66. }
  67. else {
  68. html = hljs.highlightAuto(code, options.languageSubset).value;
  69. }
  70. return colorize(html, options.theme);
  71. }
  72. exports.highlight = highlight;
  73. /**
  74. * Returns all supported languages
  75. */
  76. function listLanguages() {
  77. return hljs.listLanguages();
  78. }
  79. exports.listLanguages = listLanguages;
  80. /**
  81. * Returns true if the language is supported
  82. * @param name A language name, alias or file extension
  83. */
  84. function supportsLanguage(name) {
  85. return !!hljs.getLanguage(name);
  86. }
  87. exports.supportsLanguage = supportsLanguage;
  88. exports.default = highlight;
  89. __export(require("./theme"));
  90. //# sourceMappingURL=index.js.map