From 42fbdf9708a10914ec3c4e248e0d0a51b2c498a4 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 31 Oct 2022 09:59:33 +0100 Subject: [PATCH] #412 - Support for sub-folders --- package.json | 2 +- src/extension.ts | 21 +++---------- src/helpers/SettingsHelper.ts | 58 +++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index c32a2db2..01c26b7f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vscode-front-matter-beta", "displayName": "Front Matter CMS", - "description": "Front Matter is a CMS that runs within Visual Studio Code. It gives you the power and control of a full-blown CMS while also providing you the flexibility and speed of the static site generator of your choice like: Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...", + "description": "Front Matter is a CMS that runs within Visual Studio Code. It gives you the power and control of a full-blown CMS while also providing you the flexibility and speed of the static site generator of your choice like: Hugo, Jekyll, Docusaurus, NextJs, Gatsby, and many more...", "icon": "assets/frontmatter-teal-128x128.png", "version": "8.2.0", "preview": false, diff --git a/src/extension.ts b/src/extension.ts index 40ec3644..cc5efb30 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,32 +1,19 @@ import { GitListener } from './listeners/general/GitListener'; import * as vscode from 'vscode'; -import { Telemetry } from './helpers/Telemetry'; -import { ContentType } from './helpers/ContentType'; -import { Dashboard } from './commands/Dashboard'; -import { Article, Settings, StatusListener } from './commands'; -import { Folders } from './commands/Folders'; -import { Preview } from './commands/Preview'; -import { Project } from './commands/Project'; -import { Template } from './commands/Template'; -import { Cache } from './commands/Cache'; import { COMMAND_NAME, TelemetryEvent } from './constants'; -import { TaxonomyType } from './models'; import { MarkdownFoldingProvider } from './providers/MarkdownFoldingProvider'; import { TagType } from './panelWebView/TagType'; import { ExplorerView } from './explorerView/ExplorerView'; -import { Extension } from './helpers/Extension'; -import { DashboardData } from './models/DashboardData'; import { DashboardSettings, debounceCallback, Logger, Settings as SettingsHelper } from './helpers'; -import { Content } from './commands/Content'; import ContentProvider from './providers/ContentProvider'; -import { Wysiwyg } from './commands/Wysiwyg'; -import { Diagnostics } from './commands/Diagnostics'; import { PagesListener } from './listeners/dashboard'; -import { Backers } from './commands/Backers'; import { DataListener, SettingsListener } from './listeners/panel'; import { NavigationType } from './dashboardWebView/models'; import { ModeSwitch } from './services/ModeSwitch'; import { PagesParser } from './services/PagesParser'; +import { ContentType, Telemetry, Extension } from './helpers'; +import { TaxonomyType, DashboardData } from './models'; +import { Backers, Diagnostics, Wysiwyg, Content, Cache, Template, Project, Preview, Folders, Dashboard, Article, Settings, StatusListener } from './commands'; let frontMatterStatusBar: vscode.StatusBarItem; let statusDebouncer: { (fnc: any, time: number): void; }; @@ -37,7 +24,7 @@ export async function activate(context: vscode.ExtensionContext) { const { subscriptions, extensionUri, extensionPath } = context; const extension = Extension.getInstance(context); - Backers.init(context); + Backers.init(context).then(() => {}); if (!extension.checkIfExtensionCanRun()) { return undefined; diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index 6c3a6678..a714cba7 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -480,8 +480,7 @@ export class Settings { } // Array settings - if (relSettingName === SETTING_CUSTOM_SCRIPTS.toLowerCase()) { - + if (Settings.isEqualOrStartsWith(relSettingName, SETTING_CUSTOM_SCRIPTS)) { // Get the correct setting name let settingNameValue = "" if (relSettingName === SETTING_CUSTOM_SCRIPTS.toLowerCase()) { @@ -492,35 +491,32 @@ export class Settings { Settings.globalConfig[`${CONFIG_KEY}.${settingNameValue}`] = [...crntValue, configJson]; } // Content types - else if (relSettingName === SETTING_TAXONOMY_CONTENT_TYPES.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_TAXONOMY_CONTENT_TYPES)) { Settings.updateGlobalConfigArraySetting(SETTING_TAXONOMY_CONTENT_TYPES, "name", configJson); } // Data files - else if (relSettingName === SETTING_DATA_FILES.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_DATA_FILES)) { Settings.updateGlobalConfigArraySetting(SETTING_DATA_FILES, "id", configJson); } // Data folders - else if (relSettingName === SETTING_DATA_FOLDERS.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_DATA_FOLDERS)) { Settings.updateGlobalConfigArraySetting(SETTING_DATA_FOLDERS, "id", configJson); } // Data types - else if (relSettingName === SETTING_DATA_TYPES.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_DATA_TYPES)) { Settings.updateGlobalConfigArraySetting(SETTING_DATA_TYPES, "id", configJson); } // Page folders - else if (relSettingName === SETTING_CONTENT_PAGE_FOLDERS.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_CONTENT_PAGE_FOLDERS)) { Settings.updateGlobalConfigArraySetting(SETTING_CONTENT_PAGE_FOLDERS, "path", configJson); } // Placeholders - else if (relSettingName === SETTING_CONTENT_PLACEHOLDERS.toLowerCase()) { + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_CONTENT_PLACEHOLDERS)) { Settings.updateGlobalConfigArraySetting(SETTING_CONTENT_PLACEHOLDERS, "id", configJson); } // Object settings - else if (relSettingName === SETTING_CONTENT_SNIPPETS.toLowerCase()) { - // Filename is the key - const fileName = parse(configFilePath).name; - const crntValue = Settings.globalConfig[`${CONFIG_KEY}.${SETTING_CONTENT_SNIPPETS}`] || {}; - Settings.globalConfig[`${CONFIG_KEY}.${SETTING_CONTENT_SNIPPETS}`] = { ...crntValue, ...{ [fileName]: configJson } }; + else if (Settings.isEqualOrStartsWith(relSettingName, SETTING_CONTENT_SNIPPETS)) { + Settings.updateGlobalConfigObjectByNameSetting(SETTING_CONTENT_SNIPPETS, configFilePath, configJson); } } catch (e) { Logger.error(`Error reading config file: ${configFile.fsPath}`); @@ -528,6 +524,19 @@ export class Settings { } } + /** + * Check if the setting name is equal or starts with the reference setting name + * @param value + * @param startsWith + * @returns + */ + private static isEqualOrStartsWith(value: string, startsWith: string) { + value = value.toLowerCase(); + startsWith = startsWith.toLowerCase(); + + return value === startsWith || value.startsWith(`${startsWith}.`); + } + /** * Update an array setting in the global config * @param settingName @@ -539,15 +548,32 @@ export class Settings { // Check if folder is already added const itemIdx = crntValue.findIndex((item: any) => item[fieldName] === configJson[fieldName]); - if (itemIdx !== -1) { - crntValue[itemIdx] = configJson; - } else { + if (itemIdx === -1) { crntValue.push(configJson); } Settings.globalConfig[`${CONFIG_KEY}.${settingName}`] = [...crntValue]; } + /** + * Update an object by the file name in the global config + * @param settingName + * @param fileNamepath + * @param configJson + */ + private static updateGlobalConfigObjectByNameSetting(settingName: string, fileNamepath: string, configJson: any): void { + const crntValue = Settings.globalConfig[`${CONFIG_KEY}.${settingName}`] || {}; + + // Filename is the key + const fileName = parse(fileNamepath).name; + + if (!crntValue[fileName]) { + crntValue[fileName] = configJson; + + Settings.globalConfig[`${CONFIG_KEY}.${settingName}`] = { ...crntValue, ...{ [fileName]: configJson } }; + } + } + /** * Create a file creation watcher */