prism-graphql.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. Prism.languages.graphql = {
  2. 'comment': /#.*/,
  3. 'description': {
  4. pattern: /(?:"""(?:[^"]|(?!""")")*"""|"(?:\\.|[^\\"\r\n])*")(?=\s*[a-z_])/i,
  5. greedy: true,
  6. alias: 'string',
  7. inside: {
  8. 'language-markdown': {
  9. pattern: /(^"(?:"")?)(?!\1)[\s\S]+(?=\1$)/,
  10. lookbehind: true,
  11. inside: Prism.languages.markdown
  12. }
  13. }
  14. },
  15. 'string': {
  16. pattern: /"""(?:[^"]|(?!""")")*"""|"(?:\\.|[^\\"\r\n])*"/,
  17. greedy: true
  18. },
  19. 'number': /(?:\B-|\b)\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i,
  20. 'boolean': /\b(?:false|true)\b/,
  21. 'variable': /\$[a-z_]\w*/i,
  22. 'directive': {
  23. pattern: /@[a-z_]\w*/i,
  24. alias: 'function'
  25. },
  26. 'attr-name': {
  27. pattern: /\b[a-z_]\w*(?=\s*(?:\((?:[^()"]|"(?:\\.|[^\\"\r\n])*")*\))?:)/i,
  28. greedy: true
  29. },
  30. 'atom-input': {
  31. pattern: /\b[A-Z]\w*Input\b/,
  32. alias: 'class-name'
  33. },
  34. 'scalar': /\b(?:Boolean|Float|ID|Int|String)\b/,
  35. 'constant': /\b[A-Z][A-Z_\d]*\b/,
  36. 'class-name': {
  37. pattern: /(\b(?:enum|implements|interface|on|scalar|type|union)\s+|&\s*|:\s*|\[)[A-Z_]\w*/,
  38. lookbehind: true
  39. },
  40. 'fragment': {
  41. pattern: /(\bfragment\s+|\.{3}\s*(?!on\b))[a-zA-Z_]\w*/,
  42. lookbehind: true,
  43. alias: 'function'
  44. },
  45. 'definition-mutation': {
  46. pattern: /(\bmutation\s+)[a-zA-Z_]\w*/,
  47. lookbehind: true,
  48. alias: 'function'
  49. },
  50. 'definition-query': {
  51. pattern: /(\bquery\s+)[a-zA-Z_]\w*/,
  52. lookbehind: true,
  53. alias: 'function'
  54. },
  55. 'keyword': /\b(?:directive|enum|extend|fragment|implements|input|interface|mutation|on|query|repeatable|scalar|schema|subscription|type|union)\b/,
  56. 'operator': /[!=|&]|\.{3}/,
  57. 'property-query': /\w+(?=\s*\()/,
  58. 'object': /\w+(?=\s*\{)/,
  59. 'punctuation': /[!(){}\[\]:=,]/,
  60. 'property': /\w+/
  61. };
  62. Prism.hooks.add('after-tokenize', function afterTokenizeGraphql(env) {
  63. if (env.language !== 'graphql') {
  64. return;
  65. }
  66. /**
  67. * get the graphql token stream that we want to customize
  68. *
  69. * @typedef {InstanceType<import("./prism-core")["Token"]>} Token
  70. * @type {Token[]}
  71. */
  72. var validTokens = env.tokens.filter(function (token) {
  73. return typeof token !== 'string' && token.type !== 'comment' && token.type !== 'scalar';
  74. });
  75. var currentIndex = 0;
  76. /**
  77. * Returns whether the token relative to the current index has the given type.
  78. *
  79. * @param {number} offset
  80. * @returns {Token | undefined}
  81. */
  82. function getToken(offset) {
  83. return validTokens[currentIndex + offset];
  84. }
  85. /**
  86. * Returns whether the token relative to the current index has the given type.
  87. *
  88. * @param {readonly string[]} types
  89. * @param {number} [offset=0]
  90. * @returns {boolean}
  91. */
  92. function isTokenType(types, offset) {
  93. offset = offset || 0;
  94. for (var i = 0; i < types.length; i++) {
  95. var token = getToken(i + offset);
  96. if (!token || token.type !== types[i]) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * Returns the index of the closing bracket to an opening bracket.
  104. *
  105. * It is assumed that `token[currentIndex - 1]` is an opening bracket.
  106. *
  107. * If no closing bracket could be found, `-1` will be returned.
  108. *
  109. * @param {RegExp} open
  110. * @param {RegExp} close
  111. * @returns {number}
  112. */
  113. function findClosingBracket(open, close) {
  114. var stackHeight = 1;
  115. for (var i = currentIndex; i < validTokens.length; i++) {
  116. var token = validTokens[i];
  117. var content = token.content;
  118. if (token.type === 'punctuation' && typeof content === 'string') {
  119. if (open.test(content)) {
  120. stackHeight++;
  121. } else if (close.test(content)) {
  122. stackHeight--;
  123. if (stackHeight === 0) {
  124. return i;
  125. }
  126. }
  127. }
  128. }
  129. return -1;
  130. }
  131. /**
  132. * Adds an alias to the given token.
  133. *
  134. * @param {Token} token
  135. * @param {string} alias
  136. * @returns {void}
  137. */
  138. function addAlias(token, alias) {
  139. var aliases = token.alias;
  140. if (!aliases) {
  141. token.alias = aliases = [];
  142. } else if (!Array.isArray(aliases)) {
  143. token.alias = aliases = [aliases];
  144. }
  145. aliases.push(alias);
  146. }
  147. for (; currentIndex < validTokens.length;) {
  148. var startToken = validTokens[currentIndex++];
  149. // add special aliases for mutation tokens
  150. if (startToken.type === 'keyword' && startToken.content === 'mutation') {
  151. // any array of the names of all input variables (if any)
  152. var inputVariables = [];
  153. if (isTokenType(['definition-mutation', 'punctuation']) && getToken(1).content === '(') {
  154. // definition
  155. currentIndex += 2; // skip 'definition-mutation' and 'punctuation'
  156. var definitionEnd = findClosingBracket(/^\($/, /^\)$/);
  157. if (definitionEnd === -1) {
  158. continue;
  159. }
  160. // find all input variables
  161. for (; currentIndex < definitionEnd; currentIndex++) {
  162. var t = getToken(0);
  163. if (t.type === 'variable') {
  164. addAlias(t, 'variable-input');
  165. inputVariables.push(t.content);
  166. }
  167. }
  168. currentIndex = definitionEnd + 1;
  169. }
  170. if (isTokenType(['punctuation', 'property-query']) && getToken(0).content === '{') {
  171. currentIndex++; // skip opening bracket
  172. addAlias(getToken(0), 'property-mutation');
  173. if (inputVariables.length > 0) {
  174. var mutationEnd = findClosingBracket(/^\{$/, /^\}$/);
  175. if (mutationEnd === -1) {
  176. continue;
  177. }
  178. // give references to input variables a special alias
  179. for (var i = currentIndex; i < mutationEnd; i++) {
  180. var varToken = validTokens[i];
  181. if (varToken.type === 'variable' && inputVariables.indexOf(varToken.content) >= 0) {
  182. addAlias(varToken, 'variable-input');
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. });