prism-js-extras.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function (Prism) {
  2. Prism.languages.insertBefore('javascript', 'function-variable', {
  3. 'method-variable': {
  4. pattern: RegExp('(\\.\\s*)' + Prism.languages.javascript['function-variable'].pattern.source),
  5. lookbehind: true,
  6. alias: ['function-variable', 'method', 'function', 'property-access']
  7. }
  8. });
  9. Prism.languages.insertBefore('javascript', 'function', {
  10. 'method': {
  11. pattern: RegExp('(\\.\\s*)' + Prism.languages.javascript['function'].source),
  12. lookbehind: true,
  13. alias: ['function', 'property-access']
  14. }
  15. });
  16. Prism.languages.insertBefore('javascript', 'constant', {
  17. 'known-class-name': [
  18. {
  19. // standard built-ins
  20. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
  21. pattern: /\b(?:(?:Float(?:32|64)|(?:Int|Uint)(?:8|16|32)|Uint8Clamped)?Array|ArrayBuffer|BigInt|Boolean|DataView|Date|Error|Function|Intl|JSON|(?:Weak)?(?:Map|Set)|Math|Number|Object|Promise|Proxy|Reflect|RegExp|String|Symbol|WebAssembly)\b/,
  22. alias: 'class-name'
  23. },
  24. {
  25. // errors
  26. pattern: /\b(?:[A-Z]\w*)Error\b/,
  27. alias: 'class-name'
  28. }
  29. ]
  30. });
  31. /**
  32. * Replaces the `<ID>` placeholder in the given pattern with a pattern for general JS identifiers.
  33. *
  34. * @param {string} source
  35. * @param {string} [flags]
  36. * @returns {RegExp}
  37. */
  38. function withId(source, flags) {
  39. return RegExp(
  40. source.replace(/<ID>/g, function () { return /(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*/.source; }),
  41. flags);
  42. }
  43. Prism.languages.insertBefore('javascript', 'keyword', {
  44. 'imports': {
  45. // https://tc39.es/ecma262/#sec-imports
  46. pattern: withId(/(\bimport\b\s*)(?:<ID>(?:\s*,\s*(?:\*\s*as\s+<ID>|\{[^{}]*\}))?|\*\s*as\s+<ID>|\{[^{}]*\})(?=\s*\bfrom\b)/.source),
  47. lookbehind: true,
  48. inside: Prism.languages.javascript
  49. },
  50. 'exports': {
  51. // https://tc39.es/ecma262/#sec-exports
  52. pattern: withId(/(\bexport\b\s*)(?:\*(?:\s*as\s+<ID>)?(?=\s*\bfrom\b)|\{[^{}]*\})/.source),
  53. lookbehind: true,
  54. inside: Prism.languages.javascript
  55. }
  56. });
  57. Prism.languages.javascript['keyword'].unshift(
  58. {
  59. pattern: /\b(?:as|default|export|from|import)\b/,
  60. alias: 'module'
  61. },
  62. {
  63. pattern: /\b(?:await|break|catch|continue|do|else|finally|for|if|return|switch|throw|try|while|yield)\b/,
  64. alias: 'control-flow'
  65. },
  66. {
  67. pattern: /\bnull\b/,
  68. alias: ['null', 'nil']
  69. },
  70. {
  71. pattern: /\bundefined\b/,
  72. alias: 'nil'
  73. }
  74. );
  75. Prism.languages.insertBefore('javascript', 'operator', {
  76. 'spread': {
  77. pattern: /\.{3}/,
  78. alias: 'operator'
  79. },
  80. 'arrow': {
  81. pattern: /=>/,
  82. alias: 'operator'
  83. }
  84. });
  85. Prism.languages.insertBefore('javascript', 'punctuation', {
  86. 'property-access': {
  87. pattern: withId(/(\.\s*)#?<ID>/.source),
  88. lookbehind: true
  89. },
  90. 'maybe-class-name': {
  91. pattern: /(^|[^$\w\xA0-\uFFFF])[A-Z][$\w\xA0-\uFFFF]+/,
  92. lookbehind: true
  93. },
  94. 'dom': {
  95. // this contains only a few commonly used DOM variables
  96. pattern: /\b(?:document|(?:local|session)Storage|location|navigator|performance|window)\b/,
  97. alias: 'variable'
  98. },
  99. 'console': {
  100. pattern: /\bconsole(?=\s*\.)/,
  101. alias: 'class-name'
  102. }
  103. });
  104. // add 'maybe-class-name' to tokens which might be a class name
  105. var maybeClassNameTokens = ['function', 'function-variable', 'method', 'method-variable', 'property-access'];
  106. for (var i = 0; i < maybeClassNameTokens.length; i++) {
  107. var token = maybeClassNameTokens[i];
  108. var value = Prism.languages.javascript[token];
  109. // convert regex to object
  110. if (Prism.util.type(value) === 'RegExp') {
  111. value = Prism.languages.javascript[token] = {
  112. pattern: value
  113. };
  114. }
  115. // keep in mind that we don't support arrays
  116. var inside = value.inside || {};
  117. value.inside = inside;
  118. inside['maybe-class-name'] = /^[A-Z][\s\S]*/;
  119. }
  120. }(Prism));