Commit 8720b3e67681a013232959fd040c82bc35d1267e

Marius Schulz 2018-10-25T20:56:57

Adds support for comments in JSON (#1595) While JSON doesn't explicitly support comments, they're commonly used and there are supersets of JSON that do make comments available. PrismJS is not a linter, and this is a useful feature to include when displaying JSON.

diff --git a/components/prism-json.js b/components/prism-json.js
index 3ac9be1..8b95dcd 100644
--- a/components/prism-json.js
+++ b/components/prism-json.js
@@ -1,5 +1,9 @@
 Prism.languages.json = {
-	'property': /"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,
+	'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
+	'property': {
+		pattern: /"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,
+		greedy: true
+	},
 	'string': {
 		pattern: /"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
 		greedy: true
diff --git a/components/prism-json.min.js b/components/prism-json.min.js
index e4206fa..5d73dae 100644
--- a/components/prism-json.min.js
+++ b/components/prism-json.min.js
@@ -1 +1 @@
-Prism.languages.json={property:/"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,string:{pattern:/"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,greedy:!0},number:/-?\d+\.?\d*([Ee][+-]?\d+)?/,punctuation:/[{}[\],]/,operator:/:/g,"boolean":/\b(?:true|false)\b/i,"null":/\bnull\b/i},Prism.languages.jsonp=Prism.languages.json;
\ No newline at end of file
+Prism.languages.json={comment:/\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,property:{pattern:/"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,greedy:!0},string:{pattern:/"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,greedy:!0},number:/-?\d+\.?\d*([Ee][+-]?\d+)?/,punctuation:/[{}[\],]/,operator:/:/g,"boolean":/\b(?:true|false)\b/i,"null":/\bnull\b/i},Prism.languages.jsonp=Prism.languages.json;
\ No newline at end of file
diff --git a/tests/languages/json/comment_feature.test b/tests/languages/json/comment_feature.test
new file mode 100644
index 0000000..50341d3
--- /dev/null
+++ b/tests/languages/json/comment_feature.test
@@ -0,0 +1,27 @@
+{
+	// Line comment
+	"//": "//",
+
+	/* Block comment */
+	"/*": "*/"
+}
+
+----------------------------------------------------
+
+[
+	["punctuation", "{"],
+	["comment", "// Line comment"],
+	["property", "\"//\""],
+	["operator", ":"],
+	["string", "\"//\""],
+	["punctuation", ","],
+	["comment", "/* Block comment */"],
+	["property", "\"/*\""],
+	["operator", ":"],
+	["string", "\"*/\""],
+	["punctuation", "}"]
+]
+
+----------------------------------------------------
+
+Checks for single-line and multi-line comments.