index.js 4.3 KB

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