theme.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. "use strict";
  2. var __importStar = (this && this.__importStar) || function (mod) {
  3. if (mod && mod.__esModule) return mod;
  4. var result = {};
  5. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  6. result["default"] = mod;
  7. return result;
  8. };
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. var chalk_1 = __importStar(require("chalk"));
  11. // Always enable at least basic color support, even if not auto-detected
  12. var chalk = new chalk_1.default.Instance({ level: Math.min(chalk_1.default.level, 1 /* Basic */) });
  13. /**
  14. * Identity function for tokens that should not be styled (returns the input string as-is).
  15. * See [[Theme]] for an example.
  16. */
  17. exports.plain = function (codePart) { return codePart; };
  18. /**
  19. * The default theme. It is possible to override just individual keys.
  20. */
  21. exports.DEFAULT_THEME = {
  22. /**
  23. * keyword in a regular Algol-style language
  24. */
  25. keyword: chalk.blue,
  26. /**
  27. * built-in or library object (constant, class, function)
  28. */
  29. built_in: chalk.cyan,
  30. /**
  31. * user-defined type in a language with first-class syntactically significant types, like
  32. * Haskell
  33. */
  34. type: chalk.cyan.dim,
  35. /**
  36. * special identifier for a built-in value ("true", "false", "null")
  37. */
  38. literal: chalk.blue,
  39. /**
  40. * number, including units and modifiers, if any.
  41. */
  42. number: chalk.green,
  43. /**
  44. * literal regular expression
  45. */
  46. regexp: chalk.red,
  47. /**
  48. * literal string, character
  49. */
  50. string: chalk.red,
  51. /**
  52. * parsed section inside a literal string
  53. */
  54. subst: exports.plain,
  55. /**
  56. * symbolic constant, interned string, goto label
  57. */
  58. symbol: exports.plain,
  59. /**
  60. * class or class-level declaration (interfaces, traits, modules, etc)
  61. */
  62. class: chalk.blue,
  63. /**
  64. * function or method declaration
  65. */
  66. function: chalk.yellow,
  67. /**
  68. * name of a class or a function at the place of declaration
  69. */
  70. title: exports.plain,
  71. /**
  72. * block of function arguments (parameters) at the place of declaration
  73. */
  74. params: exports.plain,
  75. /**
  76. * comment
  77. */
  78. comment: chalk.green,
  79. /**
  80. * documentation markup within comments
  81. */
  82. doctag: chalk.green,
  83. /**
  84. * flags, modifiers, annotations, processing instructions, preprocessor directive, etc
  85. */
  86. meta: chalk.grey,
  87. /**
  88. * keyword or built-in within meta construct
  89. */
  90. 'meta-keyword': exports.plain,
  91. /**
  92. * string within meta construct
  93. */
  94. 'meta-string': exports.plain,
  95. /**
  96. * heading of a section in a config file, heading in text markup
  97. */
  98. section: exports.plain,
  99. /**
  100. * XML/HTML tag
  101. */
  102. tag: chalk.grey,
  103. /**
  104. * name of an XML tag, the first word in an s-expression
  105. */
  106. name: chalk.blue,
  107. /**
  108. * s-expression name from the language standard library
  109. */
  110. 'builtin-name': exports.plain,
  111. /**
  112. * name of an attribute with no language defined semantics (keys in JSON, setting names in
  113. * .ini), also sub-attribute within another highlighted object, like XML tag
  114. */
  115. attr: chalk.cyan,
  116. /**
  117. * name of an attribute followed by a structured value part, like CSS properties
  118. */
  119. attribute: exports.plain,
  120. /**
  121. * variable in a config or a template file, environment var expansion in a script
  122. */
  123. variable: exports.plain,
  124. /**
  125. * list item bullet in text markup
  126. */
  127. bullet: exports.plain,
  128. /**
  129. * code block in text markup
  130. */
  131. code: exports.plain,
  132. /**
  133. * emphasis in text markup
  134. */
  135. emphasis: chalk.italic,
  136. /**
  137. * strong emphasis in text markup
  138. */
  139. strong: chalk.bold,
  140. /**
  141. * mathematical formula in text markup
  142. */
  143. formula: exports.plain,
  144. /**
  145. * hyperlink in text markup
  146. */
  147. link: chalk.underline,
  148. /**
  149. * quotation in text markup
  150. */
  151. quote: exports.plain,
  152. /**
  153. * tag selector in CSS
  154. */
  155. 'selector-tag': exports.plain,
  156. /**
  157. * #id selector in CSS
  158. */
  159. 'selector-id': exports.plain,
  160. /**
  161. * .class selector in CSS
  162. */
  163. 'selector-class': exports.plain,
  164. /**
  165. * [attr] selector in CSS
  166. */
  167. 'selector-attr': exports.plain,
  168. /**
  169. * :pseudo selector in CSS
  170. */
  171. 'selector-pseudo': exports.plain,
  172. /**
  173. * tag of a template language
  174. */
  175. 'template-tag': exports.plain,
  176. /**
  177. * variable in a template language
  178. */
  179. 'template-variable': exports.plain,
  180. /**
  181. * added or changed line in a diff
  182. */
  183. addition: chalk.green,
  184. /**
  185. * deleted line in a diff
  186. */
  187. deletion: chalk.red,
  188. /**
  189. * things not matched by any token
  190. */
  191. default: exports.plain,
  192. };
  193. /**
  194. * Converts a [[JsonTheme]] with string values to a [[Theme]] with formatter functions. Used by [[parse]].
  195. */
  196. function fromJson(json) {
  197. var theme = {};
  198. for (var _i = 0, _a = Object.keys(json); _i < _a.length; _i++) {
  199. var key = _a[_i];
  200. var style = json[key];
  201. if (Array.isArray(style)) {
  202. ;
  203. theme[key] = style.reduce(function (prev, curr) { return (curr === 'plain' ? exports.plain : prev[curr]); }, chalk);
  204. }
  205. else {
  206. ;
  207. theme[key] = chalk[style];
  208. }
  209. }
  210. return theme;
  211. }
  212. exports.fromJson = fromJson;
  213. /**
  214. * Converts a [[Theme]] with formatter functions to a [[JsonTheme]] with string values. Used by [[stringify]].
  215. */
  216. function toJson(theme) {
  217. var jsonTheme = {};
  218. for (var _i = 0, _a = Object.keys(jsonTheme); _i < _a.length; _i++) {
  219. var key = _a[_i];
  220. var style = jsonTheme[key];
  221. jsonTheme[key] = style._styles;
  222. }
  223. return jsonTheme;
  224. }
  225. exports.toJson = toJson;
  226. /**
  227. * Stringifies a [[Theme]] with formatter functions to a JSON string.
  228. *
  229. * ```ts
  230. * import chalk = require('chalk');
  231. * import {stringify} from 'cli-highlight';
  232. * import * as fs from 'fs';
  233. *
  234. * const myTheme: Theme = {
  235. * keyword: chalk.red.bold,
  236. * addition: chalk.green,
  237. * deletion: chalk.red.strikethrough,
  238. * number: plain
  239. * }
  240. * const json = stringify(myTheme);
  241. * fs.writeFile('mytheme.json', json, (err: any) => {
  242. * if (err) throw err;
  243. * console.log('Theme saved');
  244. * });
  245. * ```
  246. */
  247. function stringify(theme) {
  248. return JSON.stringify(toJson(theme));
  249. }
  250. exports.stringify = stringify;
  251. /**
  252. * Parses a JSON string into a [[Theme]] with formatter functions.
  253. *
  254. * ```ts
  255. * import * as fs from 'fs';
  256. * import {parse, highlight} from 'cli-highlight';
  257. *
  258. * fs.readFile('mytheme.json', 'utf8', (err: any, json: string) => {
  259. * if (err) throw err;
  260. * const code = highlight('SELECT * FROM table', {theme: parse(json)});
  261. * console.log(code);
  262. * });
  263. * ```
  264. */
  265. function parse(json) {
  266. return fromJson(JSON.parse(json));
  267. }
  268. exports.parse = parse;
  269. //# sourceMappingURL=theme.js.map