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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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.