terminal-highlight.js 6.1 KB

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