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