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 |
|
---|
63 | Prism.hooks.add('after-tokenize', function afterTokenizeGraphql(env) {
|
---|
64 | if (env.language !== 'graphql') {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * get the graphql token stream that we want to customize
|
---|
70 | *
|
---|
71 | * @typedef {InstanceType<import("./prism-core")["Token"]>} Token
|
---|
72 | * @type {Token[]}
|
---|
73 | */
|
---|
74 | var validTokens = env.tokens.filter(function (token) {
|
---|
75 | return typeof token !== 'string' && token.type !== 'comment' && token.type !== 'scalar';
|
---|
76 | });
|
---|
77 |
|
---|
78 | var currentIndex = 0;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Returns whether the token relative to the current index has the given type.
|
---|
82 | *
|
---|
83 | * @param {number} offset
|
---|
84 | * @returns {Token | undefined}
|
---|
85 | */
|
---|
86 | function getToken(offset) {
|
---|
87 | return validTokens[currentIndex + offset];
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Returns whether the token relative to the current index has the given type.
|
---|
92 | *
|
---|
93 | * @param {readonly string[]} types
|
---|
94 | * @param {number} [offset=0]
|
---|
95 | * @returns {boolean}
|
---|
96 | */
|
---|
97 | function isTokenType(types, offset) {
|
---|
98 | offset = offset || 0;
|
---|
99 | for (var i = 0; i < types.length; i++) {
|
---|
100 | var token = getToken(i + offset);
|
---|
101 | if (!token || token.type !== types[i]) {
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns the index of the closing bracket to an opening bracket.
|
---|
110 | *
|
---|
111 | * It is assumed that `token[currentIndex - 1]` is an opening bracket.
|
---|
112 | *
|
---|
113 | * If no closing bracket could be found, `-1` will be returned.
|
---|
114 | *
|
---|
115 | * @param {RegExp} open
|
---|
116 | * @param {RegExp} close
|
---|
117 | * @returns {number}
|
---|
118 | */
|
---|
119 | function findClosingBracket(open, close) {
|
---|
120 | var stackHeight = 1;
|
---|
121 |
|
---|
122 | for (var i = currentIndex; i < validTokens.length; i++) {
|
---|
123 | var token = validTokens[i];
|
---|
124 | var content = token.content;
|
---|
125 |
|
---|
126 | if (token.type === 'punctuation' && typeof content === 'string') {
|
---|
127 | if (open.test(content)) {
|
---|
128 | stackHeight++;
|
---|
129 | } else if (close.test(content)) {
|
---|
130 | stackHeight--;
|
---|
131 |
|
---|
132 | if (stackHeight === 0) {
|
---|
133 | return i;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Adds an alias to the given token.
|
---|
144 | *
|
---|
145 | * @param {Token} token
|
---|
146 | * @param {string} alias
|
---|
147 | * @returns {void}
|
---|
148 | */
|
---|
149 | function addAlias(token, alias) {
|
---|
150 | var aliases = token.alias;
|
---|
151 | if (!aliases) {
|
---|
152 | token.alias = aliases = [];
|
---|
153 | } else if (!Array.isArray(aliases)) {
|
---|
154 | token.alias = aliases = [aliases];
|
---|
155 | }
|
---|
156 | aliases.push(alias);
|
---|
157 | }
|
---|
158 |
|
---|
159 | for (; currentIndex < validTokens.length;) {
|
---|
160 | var startToken = validTokens[currentIndex++];
|
---|
161 |
|
---|
162 | // add special aliases for mutation tokens
|
---|
163 | if (startToken.type === 'keyword' && startToken.content === 'mutation') {
|
---|
164 | // any array of the names of all input variables (if any)
|
---|
165 | var inputVariables = [];
|
---|
166 |
|
---|
167 | if (isTokenType(['definition-mutation', 'punctuation']) && getToken(1).content === '(') {
|
---|
168 | // definition
|
---|
169 |
|
---|
170 | currentIndex += 2; // skip 'definition-mutation' and 'punctuation'
|
---|
171 |
|
---|
172 | var definitionEnd = findClosingBracket(/^\($/, /^\)$/);
|
---|
173 | if (definitionEnd === -1) {
|
---|
174 | continue;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // find all input variables
|
---|
178 | for (; currentIndex < definitionEnd; currentIndex++) {
|
---|
179 | var t = getToken(0);
|
---|
180 | if (t.type === 'variable') {
|
---|
181 | addAlias(t, 'variable-input');
|
---|
182 | inputVariables.push(t.content);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | currentIndex = definitionEnd + 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (isTokenType(['punctuation', 'property-query']) && getToken(0).content === '{') {
|
---|
190 | currentIndex++; // skip opening bracket
|
---|
191 |
|
---|
192 | addAlias(getToken(0), 'property-mutation');
|
---|
193 |
|
---|
194 | if (inputVariables.length > 0) {
|
---|
195 | var mutationEnd = findClosingBracket(/^\{$/, /^\}$/);
|
---|
196 | if (mutationEnd === -1) {
|
---|
197 | continue;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // give references to input variables a special alias
|
---|
201 | for (var i = currentIndex; i < mutationEnd; i++) {
|
---|
202 | var varToken = validTokens[i];
|
---|
203 | if (varToken.type === 'variable' && inputVariables.indexOf(varToken.content) >= 0) {
|
---|
204 | addAlias(varToken, 'variable-input');
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | });
|
---|