mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Syntax Highlighting for "JSON with Comments" a.k.a. jsonc

Open harmathy opened this issue 1 year ago • 2 comments

Problem

In JSON there are no comments allowed. This is also honored by Github syntax highlighting in fenced code blocks:

{
    // Note: there might be additional sizes available
    "sizes": [ "S", "M", "L", "XL", "XXL" ]
}

There is also "JSON with Comments" or jsonc (see Github language list) which allows C-style comments

{
    // Note: there might be additional sizes available
    "sizes": [ "S", "M", "L", "XL", "XXL" ]
}

This is prominently used in tsconfig.json, jsconfig.json and vscode configuration files.

It would be nice to have syntax highlighting support for such code blocks.

A code fence with jsonc should be highlighted:

```jsonc
{
    // Note: there might be additional sizes available
    "sizes": [ "S", "M", "L", "XL", "XXL" ]
}
```

Proposed Solution

  • Support for "JSON with Comments" in highlight.js: highlightjs/highlight.js/issues/2016
    • this adds rendering of C-style comments to normal JSON
  • jsonc alias also available: highlightjs/highlight.js/issues/4018
    • seemingly not yet contained in a stable release

Notes

If #2313 is considered, Prism seems to also render comments in "normal" JSON, but there is no jsonc alias.

harmathy avatar Apr 10 '24 16:04 harmathy

As a workaround, I'd recommend using the javascript language tag.

ehuss avatar Apr 12 '24 15:04 ehuss

You can also replace mdbook's highlighting engine by overriding it's files in the theme folder. I have a working example with shiki at https://github.com/cestef/rwalk/tree/93b2034657d4e69aa4803941bfeb92f99eab5977/docs/theme. This could pretty much work with any package you like, not sure how prism works though.

cestef avatar Jun 15 '24 19:06 cestef