From b4f456fd88906c3f4f2788d2d5312be6cd49d39d Mon Sep 17 00:00:00 2001 From: Marius Schulz Date: Wed, 24 Oct 2018 23:16:46 +0100 Subject: [PATCH] Adds support for comments in JSON --- components/prism-json.js | 6 ++++- components/prism-json.min.js | 2 +- tests/languages/json/comment_feature.test | 27 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/languages/json/comment_feature.test diff --git a/components/prism-json.js b/components/prism-json.js index 3ac9be1b4f..8b95dcdc7b 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 e4206fa384..5d73dae749 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 0000000000..50341d3992 --- /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.