Skip to content

Commit

Permalink
Adds support for comments in JSON (#1595)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mariusschulz authored and mAAdhaTTah committed Oct 25, 2018
1 parent 6d1a2c6 commit 8720b3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 5 additions & 1 deletion components/prism-json.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/prism-json.min.js

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

27 changes: 27 additions & 0 deletions tests/languages/json/comment_feature.test
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 8720b3e

Please sign in to comment.