8000 Add support for JSON5 as extension of json.js. by kshetline · Pull Request #4259 · highlightjs/highlight.js · GitHub
[go: up one dir, main page]

Skip to content

Add support for JSON5 as extension of json.js. #4259

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

Merged
merged 6 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Core Grammars:
- fix(ex) adds support for `?'` char literal and missing `defguardp` keyword [Kevin Bloch][]
- fix(diff) fix unified diff hunk header regex to allow unpaired numbers [Chris Wilson][]
- enh(php) support single line and hash comments in attributes, constructor and functions [Antoine Musso][]
- enh(json) add json5 support [Kerry Shetline][]

Documentation:

Expand All @@ -34,7 +35,7 @@ CONTRIBUTORS
[Adam Lui]: https://github.com/adamlui
[Sebastiaan Speck]: https://github.com/sebastiaanspeck
[Filip Hoffmann]: https://github.com/folospior

[Kerry Shetline]: https://github.com/kshetline

## Version 11.11.1

Expand Down
2 changes: 2 additions & 0 deletions SUPPORTED_LANGUAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ The table below shows the full list of languages (and corresponding classes/alia
| Inform7 | inform7, i7 | |
| Ini, TOML | ini, toml | |
| Iptables | iptables | [highlightjs-iptables](https://github.com/highlightjs/highlightjs-iptables) |
| JSON | json, jsonc, json5 | |
| JSONata | jsonata | [highlightjs-jsonata](https://github.com/DevDimov/highlightjs-jsonata) |
| IRPF90 | irpf90 | |
| Java | java, jsp | |
| JavaScript | javascript, js, jsx | |
Expand Down
2 changes: 1 addition & 1 deletion src/languages/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export default function(hljs) {
keywords: KEYWORDS,
// this will be extended by TypeScript 8000
exports: { PARAMS_CONTAINS, CLASS_REFERENCE },
illegal: /#(?![$_A-z])/,
illegal: /#(?![$_A-Za-z])/,
contains: [
hljs.SHEBANG({
label: "shebang",
Expand Down
12 changes: 7 additions & 5 deletions src/languages/json.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
Language: JSON
Description: JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
Website: http://www.json.org
Websites: http://www.json.org, https://www.json5.org
Category: common, protocols, web
*/

import { EXTENDED_NUMBER_MODE } from "./lib/ecmascript";

export default function(hljs) {
const ATTRIBUTE = {
className: 'attr',
begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
begin: /(("(\\.|[^\\"\r\n])*")|('(\\.|[^\\'\r\n])*'))(?=\s*:)/,
relevance: 1.01
};
const PUNCTUATION = {
Expand All @@ -34,16 +35,17 @@ export default function(hljs) {

return {
name: 'JSON',
aliases: ['jsonc'],
aliases: ['jsonc', 'json5'],
keywords:{
literal: LITERALS,
},
contains: [
ATTRIBUTE,
PUNCTUATION,
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE,
LITERALS_MODE,
hljs.C_NUMBER_MODE,
EXTENDED_NUMBER_MODE,
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE
],
Expand Down
8 changes: 8 additions & 0 deletions src/languages/lib/ecmascript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export const IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
export const EXTENDED_NUMBER_RE = '([-+]?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)|NaN|[-+]?Infinity'; // 0x..., 0..., decimal, float

export const EXTENDED_NUMBER_MODE = {
scope: 'number',
match: EXTENDED_NUMBER_RE,
relevance: 0
};

const KEYWORDS = [
"as", // for exports
"in",
Expand Down
16 changes: 16 additions & 0 deletions test/detect/json5/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"title": "apples",
"count": [12000, 20000],
"description": {"text": "...", "sensitive": false}
},
{ // Like JSON, but can also include comments and unquoted identifiers.
title: "oranges",
// Hexadecimal allowed, + before numbers, leading/trailing decimal point, NaN, Infinity
'count': [17500, null, 0x1F2B, +5, -10, .6, 6., NaN, Infinity, +Infinity, -Infinity],
description: {'text': '...', 'sensitive': true} /* Single-quoted strings allowed */,
multi_line: "Strings can\
escape\
new lines"
}, // Trailing commas allowed
]
16 changes: 16 additions & 0 deletions test/markup/json5/default.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;apples&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;count&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-number">12000</span><span class="hljs-punctuation">,</span> <span class="hljs-number">20000</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;description&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;text&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;...&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;sensitive&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-punctuation">{</span> <span class="hljs-comment">// Like JSON, but can also include comments and unquoted identifiers.</span>
title<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;oranges&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-comment">// Hexadecimal allowed, + before numbers, leading/trailing decimal point, NaN, Infinity</span>
<span class="hljs-attr">&#x27;count&#x27;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-number">17500</span><span class="hljs-punctuation">,</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span> <span class="hljs-number">0x1F2B</span><span class="hljs-punctuation">,</span> <span class="hljs-number">+5</span><span class="hljs-punctuation">,</span> <span class="hljs-number">-10</span><span class="hljs-punctuation">,</span> <span class="hljs-number">.6</span><span class="hljs-punctuation">,</span> <span class="hljs-number">6.</span><span class="hljs-punctuation">,</span> <span class="hljs-number">NaN</span><span class="hljs-punctuation">,</span> <span class="hljs-number">Infinity</span><span class="hljs-punctuation">,</span> <span class="hljs-number">+Infinity</span><span class="hljs-punctuation">,</span> <span class="hljs-number">-Infinity</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
description<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">&#x27;text&#x27;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;...&#x27;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&#x27;sensitive&#x27;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">}</span> <span class="hljs-comment">/* Single-quoted strings allowed */</span><span class="hljs-punctuation">,</span>
multi_line<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Strings can\
escape\
new lines&quot;</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> <span class="hljs-comment">// Trailing commas allowed</span>
<span class="hljs-punctuation">]</span>
16 changes: 16 additions & 0 deletions test/markup/json5/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"title": "apples",
"count": [12000, 20000],
"description": {"text": "...", "sensitive": false}
},
{ // Like JSON, but can also include comments and unquoted identifiers.
title: "oranges",
// Hexadecimal allowed, + before numbers, leading/trailing decimal point, NaN, Infinity
'count': [17500, null, 0x1F2B, +5, -10, .6, 6., NaN, Infinity, +Infinity, -Infinity],
description: {'text': '...', 'sensitive': true} /* Single-quoted strings allowed */,
multi_line: "Strings can\
escape\
new lines"
}, // Trailing commas allowed
]
0