#390: Implement another JSON parser

This commit is contained in:
Elio Struyf
2022-09-02 13:26:25 +02:00
parent af1cc15d3d
commit 5b712e64d7
7 changed files with 54 additions and 11 deletions

15
.vscode/settings.json vendored
View File

@@ -10,4 +10,19 @@
"typescript.tsc.autoDetect": "off",
"eliostruyf.writingstyleguide.terms.isDisabled": true,
"eliostruyf.writingstyleguide.biasFree.isDisabled": true,
"squarl.groups": [
{
"id": "dashboard",
"name": "Dashboard"
}
],
"squarl.bookmarks": [
{
"name": "App.tsx",
"path": "src/dashboardWebView/components/App.tsx",
"description": "Start of dashboard",
"type": "file",
"groupId": "dashboard"
}
],
}

View File

@@ -16,6 +16,7 @@
- [#374](https://github.com/estruyf/vscode-front-matter/issues/374): Hide the front matter section to use the panel instead
- [#383](https://github.com/estruyf/vscode-front-matter/issues/383): Add the item menu to the content list view
- [#385](https://github.com/estruyf/vscode-front-matter/issues/385): If no default value for the draft field is defined, the field value will be set to `true`
- [#390](https://github.com/estruyf/vscode-front-matter/issues/390): Implement another JSON parser in order to be able to parse the `frontmatter.json` file better
### ⚡️ Optimizations

13
package-lock.json generated
View File

@@ -57,6 +57,7 @@
"html-webpack-plugin": "4.5.0",
"image-size": "^1.0.0",
"invariant": "^2.2.4",
"jsonc-parser": "^3.2.0",
"lodash-es": "^4.17.21",
"lodash.omit": "^4.5.0",
"lodash.uniqby": "4.7.0",
@@ -5330,6 +5331,12 @@
"json5": "lib/cli.js"
}
},
"node_modules/jsonc-parser": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz",
"integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==",
"dev": true
},
"node_modules/jsonfile": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",
@@ -15097,6 +15104,12 @@
"minimist": "^1.2.0"
}
},
"jsonc-parser": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz",
"integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==",
"dev": true
},
"jsonfile": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",

View File

@@ -2052,6 +2052,7 @@
"html-webpack-plugin": "4.5.0",
"image-size": "^1.0.0",
"invariant": "^2.2.4",
"jsonc-parser": "^3.2.0",
"lodash-es": "^4.17.21",
"lodash.omit": "^4.5.0",
"lodash.uniqby": "4.7.0",

View File

@@ -1,3 +1,4 @@
import * as jsoncParser from 'jsonc-parser';
import { CustomPlaceholder } from './../models/CustomPlaceholder';
import { Uri, workspace } from 'vscode';
import { MarkdownFoldingProvider } from './../providers/MarkdownFoldingProvider';
@@ -418,7 +419,7 @@ export class ArticleHelper {
// Check if the output needs to be parsed
if (output.includes("{") && output.includes("}")) {
try {
output = JSON.parse(output);
output = jsoncParser.parse(output);
} catch (e) {
// Do nothing
}

View File

@@ -1,3 +1,4 @@
import * as jsoncParser from 'jsonc-parser';
import { existsSync, readFileSync } from "fs";
import jsyaml = require("js-yaml");
import { join, resolve } from "path";
@@ -29,7 +30,7 @@ export class FrameworkDetector {
if (existsSync(pkgFile)) {
let packageJson: any = readFileSync(pkgFile, "utf8");
if (packageJson) {
packageJson = typeof packageJson === "string" ? JSON.parse(packageJson) : packageJson;
packageJson = typeof packageJson === "string" ? jsoncParser.parse(packageJson) : packageJson;
dependencies = packageJson.dependencies || null;
devDependencies = packageJson.devDependencies || null;

View File

@@ -11,6 +11,7 @@ import { existsSync, readFileSync, watch, writeFileSync } from 'fs';
import { Extension } from './Extension';
import { debounceCallback } from './DebounceCallback';
import { Logger } from './Logger';
import * as jsoncParser from 'jsonc-parser';
export class Settings {
public static globalFile = "frontmatter.json";
@@ -99,7 +100,7 @@ export class Settings {
const file = await workspace.openTextDocument(e.uri);
if (file) {
const fileContents = file.getText();
const json = JSON.parse(fileContents);
const json = jsoncParser.parse(fileContents);
configDebouncer(() => callback(json), 200);
// callback(json)
}
@@ -133,7 +134,11 @@ export class Settings {
/**
* Retrieve a setting from global and local config
*/
public static get<T>(name: string, merging: boolean = false): T | undefined{
public static get<T>(name: string, merging: boolean = false): T | undefined {
if (!Settings.config) {
return;
}
const configInpection = Settings.config.inspect<T>(name);
let setting = undefined;
@@ -170,7 +175,7 @@ export class Settings {
if (updateGlobal) {
if (fmConfig && existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
Settings.globalConfig = jsoncParser.parse(localConfig);
Settings.globalConfig[`${CONFIG_KEY}.${name}`] = value;
writeFileSync(fmConfig, JSON.stringify(Settings.globalConfig, null, 2), 'utf8');
@@ -399,13 +404,19 @@ export class Settings {
* Read the global config file
*/
private static readConfig() {
const fmConfig = Settings.projectConfigPath;
if (fmConfig && existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
commands.executeCommand('setContext', CONTEXT.isEnabled, true);
} else {
try {
const fmConfig = Settings.projectConfigPath;
if (fmConfig && existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = jsoncParser.parse(localConfig);
commands.executeCommand('setContext', CONTEXT.isEnabled, true);
} else {
Settings.globalConfig = undefined;
}
} catch (e) {
Settings.globalConfig = undefined;
Notifications.error(`Error reading "frontmatter.json" config file. Check [output window](command:${COMMAND_NAME.showOutputChannel}) for more details.`);
Logger.error((e as Error).message);
}
}