Skip to content

Allow JSON to handle comments #2155

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 4 commits into from
Oct 6, 2019
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ New styles:

Improvements:
- fix(Elixir): improve regex for numbers
- JSON: support for comments in JSON (#2016)

## Version 9.15.10
New languages:
Expand Down
13 changes: 10 additions & 3 deletions src/languages/json.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/*
Language: JSON
Language: JSON / JSON with Comments
Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
Category: common, protocols
*/

function(hljs) {
var LITERALS = {literal: 'true false null'};
var ALLOWED_COMMENTS = [
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE
]
var TYPES = [
hljs.QUOTE_STRING_MODE,
hljs.C_NUMBER_MODE
Expand All @@ -25,15 +29,18 @@ function(hljs) {
illegal: '\\n',
},
hljs.inherit(VALUE_CONTAINER, {begin: /:/})
],
].concat(ALLOWED_COMMENTS),
illegal: '\\S'
};
var ARRAY = {
begin: '\\[', end: '\\]',
contains: [hljs.inherit(VALUE_CONTAINER)], // inherit is a workaround for a bug that makes shared modes with endsWithParent compile only the ending of one of the parents
illegal: '\\S'
};
TYPES.splice(TYPES.length, 0, OBJECT, ARRAY);
TYPES.push(OBJECT, ARRAY);
ALLOWED_COMMENTS.forEach(function(rule) {
TYPES.push(rule)
})
return {
contains: TYPES,
keywords: LITERALS,
Expand Down
2 changes: 1 addition & 1 deletion test/markup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function testLanguage(language) {

actual.trim().should.equal(expected.trim());
done();
});
}).catch(function(err) { return done(err) });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful (:

});
});
});
Expand Down
18 changes: 18 additions & 0 deletions test/markup/json/comments.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<span class="hljs-comment">/* multi-line comment before */</span>
[
{
<span class="hljs-attr">"title"</span>: <span class="hljs-string">"apples"</span>, <span class="hljs-comment">// yum</span>
<span class="hljs-attr">"count"</span>: [<span class="hljs-number">12000</span>, <span class="hljs-number">20000</span>], <span class="hljs-comment">/* so many? */</span>
<span class="hljs-attr">"description"</span>: {<span class="hljs-attr">"text"</span>: <span class="hljs-string">"..."</span>, <span class="hljs-attr">"sensitive"</span>: <span class="hljs-literal">false</span>}
},
{
<span class="hljs-attr">"title"</span>: <span class="hljs-string">"oranges"</span>,
<span class="hljs-attr">"count"</span>: [<span class="hljs-number">17500</span>, <span class="hljs-literal">null</span>],
<span class="hljs-attr">"description"</span>: {<span class="hljs-attr">"text"</span>: <span class="hljs-string">"..."</span>, <span class="hljs-attr">"sensitive"</span>: <span class="hljs-literal">false</span>}
}
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// "title" : "brocolli"</span>
<span class="hljs-comment">// }</span>
]
<span class="hljs-comment">/* multi-line
comment after */</span>
18 changes: 18 additions & 0 deletions test/markup/json/comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* multi-line comment before */
[
{
"title": "apples", // yum
"count": [12000, 20000], /* so many? */
"description": {"text": "...", "sensitive": false}
},
{
"title": "oranges",
"count": [17500, null],
"description": {"text": "...", "sensitive": false}
}
// {
// "title" : "brocolli"
// }
]
/* multi-line
comment after */