diff --git a/.vscode/settings.json b/.vscode/settings.json index f903325a..2ba4819d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" + } + ], } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b35928b..9a28ea4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index d49f2f3d..93c58ba0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 0855cc30..c608e4a4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 4eb8356e..b6ea919b 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -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 } diff --git a/src/helpers/FrameworkDetector.ts b/src/helpers/FrameworkDetector.ts index 862c1ca0..d1607e0c 100644 --- a/src/helpers/FrameworkDetector.ts +++ b/src/helpers/FrameworkDetector.ts @@ -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; diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index a2391cfc..9f711b66 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -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(name: string, merging: boolean = false): T | undefined{ + public static get(name: string, merging: boolean = false): T | undefined { + if (!Settings.config) { + return; + } + const configInpection = Settings.config.inspect(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); } }