terminal-highlight.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _picocolors = _interopRequireDefault(require("picocolors"));
  5. var _tokenize = _interopRequireDefault(require("./tokenize"));
  6. var _input = _interopRequireDefault(require("./input"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var HIGHLIGHT_THEME = {
  9. brackets: _picocolors.default.cyan,
  10. 'at-word': _picocolors.default.cyan,
  11. comment: _picocolors.default.gray,
  12. string: _picocolors.default.green,
  13. class: _picocolors.default.yellow,
  14. call: _picocolors.default.cyan,
  15. hash: _picocolors.default.magenta,
  16. '(': _picocolors.default.cyan,
  17. ')': _picocolors.default.cyan,
  18. '{': _picocolors.default.yellow,
  19. '}': _picocolors.default.yellow,
  20. '[': _picocolors.default.yellow,
  21. ']': _picocolors.default.yellow,
  22. ':': _picocolors.default.yellow,
  23. ';': _picocolors.default.yellow
  24. };
  25. function getTokenType(_ref, processor) {
  26. var type = _ref[0],
  27. value = _ref[1];
  28. if (type === 'word') {
  29. if (value[0] === '.') {
  30. return 'class';
  31. }
  32. if (value[0] === '#') {
  33. return 'hash';
  34. }
  35. }
  36. if (!processor.endOfFile()) {
  37. var next = processor.nextToken();
  38. processor.back(next);
  39. if (next[0] === 'brackets' || next[0] === '(') return 'call';
  40. }
  41. return type;
  42. }
  43. function terminalHighlight(css) {
  44. var processor = (0, _tokenize.default)(new _input.default(css), {
  45. ignoreErrors: true
  46. });
  47. var result = '';
  48. var _loop = function _loop() {
  49. var token = processor.nextToken();
  50. var color = HIGHLIGHT_THEME[getTokenType(token, processor)];
  51. if (color) {
  52. result += token[1].split(/\r?\n/).map(function (i) {
  53. return color(i);
  54. }).join('\n');
  55. } else {
  56. result += token[1];
  57. }
  58. };
  59. while (!processor.endOfFile()) {
  60. _loop();
  61. }
  62. return result;
  63. }
  64. var _default = terminalHighlight;
  65. exports.default = _default;
  66. module.exports = exports.default;
  67. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlcm1pbmFsLWhpZ2hsaWdodC5lczYiXSwibmFtZXMiOlsiSElHSExJR0hUX1RIRU1FIiwiYnJhY2tldHMiLCJwaWNvIiwiY3lhbiIsImNvbW1lbnQiLCJncmF5Iiwic3RyaW5nIiwiZ3JlZW4iLCJjbGFzcyIsInllbGxvdyIsImNhbGwiLCJoYXNoIiwibWFnZW50YSIsImdldFRva2VuVHlwZSIsInByb2Nlc3NvciIsInR5cGUiLCJ2YWx1ZSIsImVuZE9mRmlsZSIsIm5leHQiLCJuZXh0VG9rZW4iLCJiYWNrIiwidGVybWluYWxIaWdobGlnaHQiLCJjc3MiLCJJbnB1dCIsImlnbm9yZUVycm9ycyIsInJlc3VsdCIsInRva2VuIiwiY29sb3IiLCJzcGxpdCIsIm1hcCIsImkiLCJqb2luIl0sIm1hcHBpbmdzIjoiOzs7OztBQUFBOztBQUVBOztBQUNBOzs7O0FBRUEsSUFBTUEsZUFBZSxHQUFHO0FBQ3RCQyxFQUFBQSxRQUFRLEVBQUVDLG9CQUFLQyxJQURPO0FBRXRCLGFBQVdELG9CQUFLQyxJQUZNO0FBR3RCQyxFQUFBQSxPQUFPLEVBQUVGLG9CQUFLRyxJQUhRO0FBSXRCQyxFQUFBQSxNQUFNLEVBQUVKLG9CQUFLSyxLQUpTO0FBS3RCQyxFQUFBQSxLQUFLLEVBQUVOLG9CQUFLTyxNQUxVO0FBTXRCQyxFQUFBQSxJQUFJLEVBQUVSLG9CQUFLQyxJQU5XO0FBT3RCUSxFQUFBQSxJQUFJLEVBQUVULG9CQUFLVSxPQVBXO0FBUXRCLE9BQUtWLG9CQUFLQyxJQVJZO0FBU3RCLE9BQUtELG9CQUFLQyxJQVRZO0FBVXRCLE9BQUtELG9CQUFLTyxNQVZZO0FBV3RCLE9BQUtQLG9CQUFLTyxNQV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