Skip to content
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

Closed
mariusschulz opened this issue Oct 21, 2018 · 6 comments
Closed

Support for comments in JSON #1590

mariusschulz opened this issue Oct 21, 2018 · 6 comments

Comments

@mariusschulz
Copy link
Contributor

mariusschulz commented Oct 21, 2018

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:

{
  "compilerOptions": {
    "target": "es5",      /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    // "lib": [],         /* Specify library files to be included in the compilation. */
    // "allowJs": true,   /* Allow javascript files to be compiled. */
    // "checkJs": true,   /* Report errors in .js files. */
    // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */

    // ...
}

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! :)

@mAAdhaTTah
Copy link
Member

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.

@mariusschulz
Copy link
Contributor Author

mariusschulz commented Oct 21, 2018

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:

A JSON object that was highlighted using the JS language

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",
    // ...
}

@mariusschulz
Copy link
Contributor Author

For further consideration, here's what the above screenshot would look like if the JSON language supported comments. I think it looks much better!

A JSON object that was highlighted using the JSON language with support for comments

We could reuse the comment patterns from the clike language:

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
};

@RunDevelopment
Copy link
Member

RunDevelopment commented Oct 24, 2018

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, property has to be greedy because it is not the first pattern anymore.

Btw. Are you interested in making this a PR?

@mariusschulz
Copy link
Contributor Author

Sure, I'll be happy to! Just wanted to run this by everyone first. I'll send a PR tonight!

@mariusschulz
Copy link
Contributor Author

Closing this issue, now that support has landed in #1595.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants