-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for comments in JSON #1590
Comments
I don't know as we need to modify JSON; if you want comments, couldn't you use the JS language instead? I also find most of the editors I use don't highlight comments in JSON files correctly. That said, I'm open to being convinced otherwise. |
That's exactly what I did to unblock myself for now. I highlighted my code example using the JS language, but the results aren't great since quoted properties and string literals use the same color: If you feel uncomfortable modifying JSON directly, we could introduce a new JSON5 language that extends JSON. However, I'd prefer to add comments to JSON directly. Comments are useful in general, e.g. to indicate that some parts have been omitted: {
"compilerOptions": {
"target": "es5",
"module": "commonjs",
// ...
} |
For further consideration, here's what the above screenshot would look like if the JSON language supported comments. I think it looks much better! We could reuse the comment patterns from the Prism.languages.json = {
+ comment: [
+ {
+ pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
+ lookbehind: true
+ },
+ {
+ pattern: /(^|[^\\:])\/\/.*/,
+ lookbehind: true,
+ greedy: true
+ }
+ ],
property: /"(?:\\.|[^\\"\r\n])*"(?=\s*:)/i,
string: {
pattern: /"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
greedy: true
},
number: /-?\d+\.?\d*([Ee][+-]?\d+)?/,
punctuation: /[{}[\],]/,
operator: /:/g,
boolean: /\b(?:true|false)\b/i,
null: /\bnull\b/i
}; |
Using the comments pattern from C-like will work but since JS (and therefore JSON) does not have all syntactical features of C, we can simplify the pattern. This should also work: 'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/ Also, Btw. Are you interested in making this a PR? |
Sure, I'll be happy to! Just wanted to run this by everyone first. I'll send a PR tonight! |
Closing this issue, now that support has landed in #1595. |
What's your take on supporting single-line and block comments in JSON? Several popular projects (e.g. TypeScript) allow comments in JSON configuration files:
Although the JSON spec doesn't include comments, I think it would be helpful to have support for them in Prism; comments can be particularly helpful in technical blog posts.
What do you think? I'll be happy to send a PR! :)
The text was updated successfully, but these errors were encountered: