Skip to content

Commit

Permalink
GraphQL: Added support for multi-line strings and descriptions (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMabry committed May 29, 2020
1 parent ed71515 commit 9e64c62
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components.json
Expand Up @@ -397,6 +397,7 @@
},
"graphql": {
"title": "GraphQL",
"optional": ["markdown"],
"owner": "Golmote"
},
"groovy": {
Expand Down
14 changes: 13 additions & 1 deletion components/prism-graphql.js
@@ -1,7 +1,19 @@
Prism.languages.graphql = {
'comment': /#.*/,
'description': {
pattern: /(?:"""(?:[^"]|(?!""")")*"""|"(?:\\.|[^\\"\r\n])*")(?=\s*[a-z_])/i,
greedy: true,
alias: 'string',
inside: {
'language-markdown': {
pattern: /(^"(?:"")?)(?!\1)[\s\S]+(?=\1$)/,
lookbehind: true,
inside: Prism.languages.markdown
}
}
},
'string': {
pattern: /"(?:\\.|[^\\"\r\n])*"/,
pattern: /"""(?:[^"]|(?!""")")*"""|"(?:\\.|[^\\"\r\n])*"/,
greedy: true
},
'number': /(?:\B-|\b)\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-graphql.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions examples/prism-graphql.html
Expand Up @@ -3,7 +3,10 @@ <h2>Comments</h2>

<h2>Strings</h2>
<pre><code>""
"foo \"bar\" baz"</code></pre>
"foo \"bar\" baz"
""" "Multi-line" strings
are supported."""
</code></pre>

<h2>Numbers</h2>
<pre><code>0
Expand All @@ -28,4 +31,32 @@ <h2>Keywords</h2>
id
name
profilePic(size: 50)
}</code></pre>
}</code></pre>

<p>Markdown inside of descriptions require markdown to be loaded.
On this page, checking Markdown <strong>before</strong> checking GraphQL should
make the example below work properly.</p>

<h2>Descriptions</h2>
<pre><code>"""
This is a multiline description
# Heading
[Prism](http://www.prismjs.com)

It can contain **Markdown
on multiple lines**
"""
type Example {
id: ID!
}

type Sample {
"""
Simple multiline description
"""
name(
"This is a single line description"
first: Int
): String
}
</code></pre>
34 changes: 34 additions & 0 deletions tests/languages/graphql/description_feature.test
@@ -0,0 +1,34 @@
"Single-line description"
type Foo {}
"""
Multiline description
"""
type Bar {}

----------------------------------------------------

[
["description", [
"\"",
["language-markdown", "Single-line description"],
"\""
]],
["keyword", "type"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],

["description", [
"\"\"\"",
["language-markdown", "\r\nMultiline description\r\n"],
"\"\"\""
]],
["keyword", "type"],
["class-name", "Bar"],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for descriptions.
5 changes: 4 additions & 1 deletion tests/languages/graphql/string_feature.test
@@ -1,13 +1,16 @@
""
"foo bar"
"foo\"bar\\baz"
"""multi-line
string"""

----------------------------------------------------

[
["string", "\"\""],
["string", "\"foo bar\""],
["string", "\"foo\\\"bar\\\\baz\""]
["string", "\"foo\\\"bar\\\\baz\""],
["string", "\"\"\"multi-line\r\nstring\"\"\""]
]

----------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions tests/languages/markdown+graphql/description_feature.test
@@ -0,0 +1,26 @@
"""
Complex multiline description
# Title
"""
type Baz {}

----------------------------------------------------

[
["description", [
"\"\"\"",
["language-markdown", [
"\r\nComplex multiline description\r\n",
["title", [["punctuation", "#"], " Title"]]
]],
"\"\"\""
]],
["keyword", "type"],
["class-name", "Baz"],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for descriptions.

0 comments on commit 9e64c62

Please sign in to comment.