index.js 3.6 KB

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