From e8c67c75fd8dc01cc752149c4bb03ca253d8d1fe Mon Sep 17 00:00:00 2001 From: Elio Date: Tue, 5 Oct 2021 19:58:25 +0200 Subject: [PATCH] Fix file path issues for windows + open file with parsing error --- src/commands/Dashboard.ts | 12 ++++------ .../components/Media/Item.tsx | 5 +--- src/helpers/ArticleHelper.ts | 24 +++++++++++++++---- src/helpers/Notifications.ts | 12 +++++----- src/helpers/parseWinPath.ts | 3 +++ 5 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 src/helpers/parseWinPath.ts diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index b35bf928..c6c90428 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -23,6 +23,7 @@ import { DashboardData } from '../models/DashboardData'; import { ExplorerView } from '../explorerView/ExplorerView'; import { MediaLibrary } from '../helpers/MediaLibrary'; import imageSize from 'image-size'; +import { parseWinPath } from '../helpers/parseWinPath'; export class Dashboard { private static webview: WebviewPanel | null = null; @@ -331,12 +332,7 @@ export class Dashboard { Dashboard.media = Object.assign([], allMedia); - // Filter the media let files: MediaInfo[] = Dashboard.media; - if (relSelectedFolderPath) { - const folderPath = `${relSelectedFolderPath.startsWith("/") ? "" : "/"}${relSelectedFolderPath}${relSelectedFolderPath.endsWith("/") ? "" : "/"}` - files = files.filter(f => f.fsPath.includes(folderPath)); - } // Retrieve the total after filtering and before the slicing happens const total = files.length; @@ -365,20 +361,20 @@ export class Dashboard { if (selectedFolder) { if (existsSync(selectedFolder)) { - allFolders = readdirSync(selectedFolder, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => join(selectedFolder, dir.name)); + allFolders = readdirSync(selectedFolder, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(selectedFolder, dir.name)) as string); } } else { for (const contentFolder of contentFolders) { const contentPath = contentFolder.path; if (contentPath && existsSync(contentPath)) { - const subFolders = readdirSync(contentPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => join(contentPath, dir.name)); + const subFolders = readdirSync(contentPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(contentPath, dir.name)) as string); allContentFolders = [...allContentFolders, ...subFolders]; } } const staticPath = join(wsFolder?.fsPath || "", staticFolder || ""); if (staticPath && existsSync(staticPath)) { - allFolders = readdirSync(staticPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => join(staticPath, dir.name)); + allFolders = readdirSync(staticPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(staticPath, dir.name)) as string); } } diff --git a/src/dashboardWebView/components/Media/Item.tsx b/src/dashboardWebView/components/Media/Item.tsx index c7c567b4..98295957 100644 --- a/src/dashboardWebView/components/Media/Item.tsx +++ b/src/dashboardWebView/components/Media/Item.tsx @@ -4,6 +4,7 @@ import { basename, dirname } from 'path'; import * as React from 'react'; import { useEffect } from 'react'; import { useRecoilState, useRecoilValue } from 'recoil'; +import { parseWinPath } from '../../../helpers/parseWinPath'; import { MediaInfo } from '../../../models/MediaPaths'; import { DashboardMessage } from '../../DashboardMessage'; import { LightboxAtom, PageSelector, SelectedMediaFolderSelector, SettingsSelector, ViewDataSelector } from '../../state'; @@ -26,10 +27,6 @@ export const Item: React.FunctionComponent = ({media}: React.PropsWi const viewData = useRecoilValue(ViewDataSelector); const page = useRecoilValue(PageSelector); - const parseWinPath = (path: string | undefined) => { - return path?.split(`\\`).join(`/`); - } - const getFolder = () => { if (settings?.wsFolder && media.fsPath) { let relPath = media.fsPath.split(settings.wsFolder).pop(); diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 83a1fcde..1936e4c3 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -10,6 +10,8 @@ import { Settings } from '.'; import { parse } from 'date-fns'; import { Notifications } from './Notifications'; import { Article } from '../commands'; +import { basename } from 'path'; +import { EditorHelper } from '@estruyf/vscode'; export class ArticleHelper { @@ -20,7 +22,7 @@ export class ArticleHelper { */ public static getFrontMatter(editor: vscode.TextEditor) { const fileContents = editor.document.getText(); - return ArticleHelper.parseFile(fileContents); + return ArticleHelper.parseFile(fileContents, editor.document.fileName); } /** @@ -29,7 +31,7 @@ export class ArticleHelper { */ public static getFrontMatterByPath(filePath: string) { const file = fs.readFileSync(filePath, { encoding: "utf-8" }); - return ArticleHelper.parseFile(file); + return ArticleHelper.parseFile(file, filePath); } /** @@ -167,7 +169,7 @@ export class ArticleHelper { * @param fileContents * @returns */ - private static parseFile(fileContents: string): matter.GrayMatterFile | null { + private static parseFile(fileContents: string, fileName: string): matter.GrayMatterFile | null { try { const commaSeparated = Settings.get(SETTING_COMMA_SEPARATED_FIELDS); @@ -192,7 +194,21 @@ export class ArticleHelper { } } } catch (error: any) { - Notifications.error(`There seems to be an issue parsing your Front Matter. ERROR: ${error.message || error}`); + const items = [{ + title: "Check file", + action: async () => { + console.log(fileName); + await EditorHelper.showFile(fileName) + } + }]; + Notifications.error(`There seems to be an issue parsing the content its front matter. FileName: ${basename(fileName)}. ERROR: ${error.message || error}`, ...items).then((result: any) => { + if (result?.title) { + const item = items.find(i => i.title === result.title); + if (item) { + item.action(); + } + } + }); } return null; } diff --git a/src/helpers/Notifications.ts b/src/helpers/Notifications.ts index b601ca41..db46e1b1 100644 --- a/src/helpers/Notifications.ts +++ b/src/helpers/Notifications.ts @@ -4,15 +4,15 @@ import { EXTENSION_NAME } from "../constants"; export class Notifications { - public static info(message: string) { - window.showInformationMessage(`${EXTENSION_NAME}: ${message}`); + public static info(message: string, items?: any): Thenable { + return window.showInformationMessage(`${EXTENSION_NAME}: ${message}`, items); } - public static warning(message: string) { - window.showWarningMessage(`${EXTENSION_NAME}: ${message}`); + public static warning(message: string, items?: any): Thenable { + return window.showWarningMessage(`${EXTENSION_NAME}: ${message}`, items); } - public static error(message: string) { - window.showErrorMessage(`${EXTENSION_NAME}: ${message}`); + public static error(message: string, items?: any): Thenable { + return window.showErrorMessage(`${EXTENSION_NAME}: ${message}`, items); } } \ No newline at end of file diff --git a/src/helpers/parseWinPath.ts b/src/helpers/parseWinPath.ts new file mode 100644 index 00000000..b738b46c --- /dev/null +++ b/src/helpers/parseWinPath.ts @@ -0,0 +1,3 @@ +export const parseWinPath = (path: string | undefined) => { + return path?.split(`\\`).join(`/`); + } \ No newline at end of file