Skip to content

Commit ead12e4

Browse files
authored
Merge pull request #35 from andrewbranch/json5-and-loglevel
Support json5 and don’t log warnings by default
2 parents 0ab5a35 + 2f14a27 commit ead12e4

File tree

8 files changed

+50
-41
lines changed

8 files changed

+50
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Add to your `gatsby-config.js` (all options are optional; defaults shown here):
7070
}) => '',
7171
extensionDataDirectory: // Absolute path to the directory where extensions will be downloaded. Defaults to inside node_modules.
7272
path.resolve('extensions'),
73+
logLevel: 'error' // Set to 'warn' to debug if something looks wrong
7374
}
7475
}]
7576
}

lib/vscode/colorThemeData.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
const path = require('path');
99
const fs = require('fs');
10-
const json = require('comment-json');
10+
const JSON5 = require('json5');
1111
const plist = require('plist');
1212

1313
function loadColorTheme(themeLocation, resultRules = [], resultColors = {}) {
1414
let name = path.basename(themeLocation).split('.')[0];
1515
if (path.extname(themeLocation) === '.json') {
1616
const content = fs.readFileSync(themeLocation, 'utf8');
17-
let contentValue = json.parse(content);
17+
let contentValue = JSON5.parse(content);
1818
name = contentValue.name || name;
1919
if (contentValue.include) {
2020
loadColorTheme(path.join(path.dirname(themeLocation), contentValue.include), resultRules, resultColors);

package-lock.json

Lines changed: 12 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
},
3131
"homepage": "https://github.com/andrewbranch/gatsby-remark-vscode#readme",
3232
"devDependencies": {
33-
"@types/comment-json": "^1.1.1",
3433
"@types/decompress": "^4.2.3",
3534
"@types/glob": "^7.1.1",
3635
"@types/jest": "^24.0.11",
36+
"@types/json5": "0.0.30",
3737
"@types/lodash.escape": "^4.0.6",
3838
"@types/lodash.uniq": "^4.5.6",
3939
"@types/node": "^12.0.0",
@@ -45,10 +45,11 @@
4545
"typescript": "^3.4.5"
4646
},
4747
"dependencies": {
48-
"comment-json": "^1.1.3",
4948
"decompress": "^4.2.0",
49+
"json5": "^2.1.0",
5050
"lodash.escape": "^4.0.1",
5151
"lodash.uniq": "^4.5.0",
52+
"loglevel": "^1.6.3",
5253
"oniguruma": "^7.2.0",
5354
"plist": "^3.0.1",
5455
"unist-util-visit": "^1.4.0",

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
const fs = require('fs');
33
const path = require('path');
4+
const logger = require('loglevel');
45
const visit = require('unist-util-visit');
56
const escapeHTML = require('lodash.escape');
67
const lineHighlighting = require('./lineHighlighting');
@@ -20,14 +21,14 @@ const styles = fs.readFileSync(path.resolve(__dirname, '../styles.css'), 'utf8')
2021
* @param {string} rootScopeName
2122
*/
2223
function warnMissingLanguageFile(missingScopeName, rootScopeName) {
23-
console.warn(`No language file was loaded for scope '${missingScopeName}' (requested by '${rootScopeName}').`);
24+
logger.warn(`No language file was loaded for scope '${missingScopeName}' (requested by '${rootScopeName}').`);
2425
}
2526

2627
/**
2728
* @param {string} lang
2829
*/
2930
function warnUnknownLanguage(lang) {
30-
console.warn(
31+
logger.warn(
3132
`Encountered unknown language '${lang}'. If '${lang}' is an alias for a supported language, ` +
3233
`use the 'languageAliases' plugin option to map it to the canonical language name.`
3334
);
@@ -120,6 +121,7 @@ function getStylesFromSettings(settings) {
120121
* @property {boolean=} injectStyles
121122
* @property {(colorValue: string, theme: string) => string=} replaceColor
122123
* @property {string=} extensionDataDirectory
124+
* @property {'trace' | 'debug' | 'info' | 'warn' | 'error'=} logLevel
123125
*/
124126

125127
function createPlugin() {
@@ -140,9 +142,11 @@ function createPlugin() {
140142
getLineClassName = () => '',
141143
injectStyles = true,
142144
replaceColor = x => x,
143-
extensionDataDirectory = path.resolve(__dirname, '../lib/extensions')
145+
extensionDataDirectory = path.resolve(__dirname, '../lib/extensions'),
146+
logLevel = 'error'
144147
} = {}
145148
) {
149+
logger.setLevel(logLevel);
146150
/** @type {Record<string, string>} */
147151
const stylesheets = {};
148152
const nodes = [];

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const fs = require('fs');
33
const util = require('util');
44
const path = require('path');
5-
const json = require('comment-json');
5+
const JSON5 = require('json5');
66
const plist = require('plist');
77
const uniq = require('lodash.uniq');
88

@@ -71,7 +71,7 @@ function sanitizeForClassName(str) {
7171
}
7272

7373
const readFile = util.promisify(fs.readFile);
74-
const requireJson = /** @param {string} pathName */ pathName => json.parse(fs.readFileSync(pathName, 'utf8'));
74+
const requireJson = /** @param {string} pathName */ pathName => JSON5.parse(fs.readFileSync(pathName, 'utf8'));
7575
const requireGrammar = /** @param {string} pathName */ async pathName =>
7676
path.extname(pathName) === '.json' ? requireJson(pathName) : plist.parse(await readFile(pathName, 'utf8'));
7777

test/gatsby-remark-vscode.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ describe('prefers-color-scheme', () => {
245245
}, markdownAST);
246246
});
247247
});
248+
249+
describe('utils', () => {
250+
describe('requireJson', () => {
251+
it('works with json5', () => {
252+
expect(() => realUtils.requireJson(path.resolve(__dirname, 'json5.tmTheme.json'))).not.toThrow();
253+
});
254+
});
255+
});

test/json5.tmTheme.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "vscode://schemas/color-theme",
3+
"name": "json5",
4+
"tokenColors": [
5+
{
6+
scope: [
7+
"meta.embedded",
8+
"source.groovy.embedded",
9+
],
10+
"settings": {
11+
"foreground": "#D4D4D4" // a nice gray
12+
}
13+
},
14+
],
15+
}

0 commit comments

Comments
 (0)