From 97a635c2de9f0320c372d581abe14758457488d1 Mon Sep 17 00:00:00 2001 From: Elio Date: Tue, 5 Oct 2021 20:20:31 +0200 Subject: [PATCH] Making sure paths are parsed for Windows --- src/commands/Dashboard.ts | 10 +++++----- src/commands/Folders.ts | 10 ++++++---- .../components/Header/Breadcrumb.tsx | 14 ++++++++------ src/helpers/ArticleHelper.ts | 1 + src/helpers/ImageHelper.ts | 5 +++-- src/helpers/MediaLibrary.ts | 5 +++-- src/helpers/parseWinPath.ts | 4 ++-- 7 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index c6c90428..62f8588d 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -290,7 +290,7 @@ export class Dashboard { selectedFolder = ''; } - const relSelectedFolderPath = selectedFolder ? selectedFolder.substring((wsFolder?.fsPath || "").length + 1) : ''; + const relSelectedFolderPath = selectedFolder ? selectedFolder.substring((parseWinPath(wsFolder?.fsPath || "")).length + 1) : ''; let allMedia: MediaInfo[] = []; @@ -361,20 +361,20 @@ export class Dashboard { if (selectedFolder) { if (existsSync(selectedFolder)) { - allFolders = readdirSync(selectedFolder, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(selectedFolder, dir.name)) as string); + allFolders = readdirSync(selectedFolder, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(selectedFolder, dir.name))); } } 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 => parseWinPath(join(contentPath, dir.name)) as string); + const subFolders = readdirSync(contentPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(contentPath, dir.name))); allContentFolders = [...allContentFolders, ...subFolders]; } } - const staticPath = join(wsFolder?.fsPath || "", staticFolder || ""); + const staticPath = join(parseWinPath(wsFolder?.fsPath || ""), staticFolder || ""); if (staticPath && existsSync(staticPath)) { - allFolders = readdirSync(staticPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(staticPath, dir.name)) as string); + allFolders = readdirSync(staticPath, { withFileTypes: true }).filter(dir => dir.isDirectory()).map(dir => parseWinPath(join(staticPath, dir.name))); } } diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index e5cc4d6e..163cacc3 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -10,6 +10,7 @@ import { Settings } from "../helpers"; import { existsSync, mkdirSync } from 'fs'; import { format } from 'date-fns'; import { Dashboard } from './Dashboard'; +import { parseWinPath } from '../helpers/parseWinPath'; export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; @@ -26,7 +27,7 @@ export class Folders { let startPath = ""; if (data?.selectedFolder) { - startPath = data.selectedFolder.replace((wsFolder?.fsPath || ""), ""); + startPath = data.selectedFolder.replace(parseWinPath(wsFolder?.fsPath || ""), ""); } else if (staticFolder) { startPath = `/${staticFolder}`; } @@ -51,7 +52,7 @@ export class Folders { let parentFolders: string[] = []; for (const folder of folders) { - const folderPath = join(wsFolder?.fsPath || "", parentFolders.join("/"), folder); + const folderPath = join(parseWinPath(wsFolder?.fsPath || ""), parentFolders.join("/"), folder); parentFolders.push(folder); @@ -179,6 +180,7 @@ export class Folders { return projectFolder; } + return undefined; } @@ -283,7 +285,7 @@ export class Folders { */ private static absWsFolder(folder: ContentFolder, wsFolder?: Uri) { const isWindows = process.platform === 'win32'; - let absPath = folder.path.replace(WORKSPACE_PLACEHOLDER, wsFolder?.fsPath || ""); + let absPath = folder.path.replace(WORKSPACE_PLACEHOLDER, parseWinPath(wsFolder?.fsPath || "")); absPath = isWindows ? absPath.split('/').join('\\') : absPath; return absPath; } @@ -296,7 +298,7 @@ export class Folders { */ private static relWsFolder(folder: ContentFolder, wsFolder?: Uri) { const isWindows = process.platform === 'win32'; - let absPath = folder.path.replace(wsFolder?.fsPath || "", WORKSPACE_PLACEHOLDER); + let absPath = folder.path.replace(parseWinPath(wsFolder?.fsPath || ""), WORKSPACE_PLACEHOLDER); absPath = isWindows ? absPath.split('\\').join('/') : absPath; return absPath; } diff --git a/src/dashboardWebView/components/Header/Breadcrumb.tsx b/src/dashboardWebView/components/Header/Breadcrumb.tsx index 13ac21dd..24c70f1d 100644 --- a/src/dashboardWebView/components/Header/Breadcrumb.tsx +++ b/src/dashboardWebView/components/Header/Breadcrumb.tsx @@ -3,6 +3,7 @@ import { basename, join } from 'path'; import * as React from 'react'; import { useRecoilState, useRecoilValue } from 'recoil'; import { HOME_PAGE_NAVIGATION_ID } from '../../../constants'; +import { parseWinPath } from '../../../helpers/parseWinPath'; import { SelectedMediaFolderAtom, SettingsAtom } from '../../state'; export interface IBreadcrumbProps {} @@ -19,10 +20,11 @@ export const Breadcrumb: React.FunctionComponent = (props: Rea React.useEffect(() => { const { wsFolder, staticFolder, contentFolders } = settings; - const isValid = (folderPath: string) => { + const isValid = (folderPath: string) => { if (staticFolder) { - const staticPath = join(wsFolder, staticFolder); - const relPath = folderPath.replace(staticPath, ''); + const staticPath = parseWinPath(join(wsFolder, staticFolder)) as string; + const relPath = folderPath.replace(staticPath, '') as string; + if (relPath.length > 1 && folderPath.startsWith(staticPath)) { return true; } else if (relPath.length === 0) { @@ -31,7 +33,7 @@ export const Breadcrumb: React.FunctionComponent = (props: Rea } for (let i = 0; i < contentFolders.length; i++) { - const contentFolder = contentFolders[i]; + const contentFolder = parseWinPath(contentFolders[i]) as string; const relContentPath = folderPath.replace(contentFolder, ''); return relContentPath.length > 1 && folderPath.startsWith(contentFolder); } @@ -42,10 +44,10 @@ export const Breadcrumb: React.FunctionComponent = (props: Rea if (!selectedFolder) { setFolders([]); } else { - const relPath = selectedFolder.replace(settings.wsFolder, ''); + const relPath = parseWinPath(selectedFolder.replace(parseWinPath(settings.wsFolder) as string, '')) as string; const folderParts = relPath.split('/').filter(f => f); const allFolders: string[] = []; - let previousFolder = settings.wsFolder; + let previousFolder = parseWinPath(settings.wsFolder) as string; for (const part of folderParts) { const folder = join(previousFolder, part); diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 1936e4c3..63db4cad 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -201,6 +201,7 @@ export class ArticleHelper { 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); diff --git a/src/helpers/ImageHelper.ts b/src/helpers/ImageHelper.ts index b56eeee7..3bb402ac 100644 --- a/src/helpers/ImageHelper.ts +++ b/src/helpers/ImageHelper.ts @@ -5,6 +5,7 @@ import { existsSync } from 'fs'; import { Folders } from '../commands/Folders'; import { Settings } from './SettingsHelper'; import { SETTINGS_CONTENT_STATIC_FOLDER } from '../constants'; +import { parseWinPath } from './parseWinPath'; export class ImageHelper { @@ -51,7 +52,7 @@ export class ImageHelper { const wsFolder = Folders.getWorkspaceFolder(); const staticFolder = Settings.get(SETTINGS_CONTENT_STATIC_FOLDER); - const staticPath = join(wsFolder?.fsPath || "", staticFolder || "", value); + const staticPath = join(parseWinPath(wsFolder?.fsPath || ""), staticFolder || "", value); const contentFolderPath = filePath ? join(dirname(filePath), value) : null; if (existsSync(staticPath)) { @@ -72,7 +73,7 @@ export class ImageHelper { let relPath = imgValue || ""; if (imgValue) { - relPath = imgValue.split(wsFolder?.fsPath || "").pop() || ""; + relPath = imgValue.split(parseWinPath(wsFolder?.fsPath || "")).pop() || ""; relPath = imgValue.split(staticFolder || "").pop() || ""; } return relPath; diff --git a/src/helpers/MediaLibrary.ts b/src/helpers/MediaLibrary.ts index 584b4828..9a29b308 100644 --- a/src/helpers/MediaLibrary.ts +++ b/src/helpers/MediaLibrary.ts @@ -5,6 +5,7 @@ import { basename, dirname, join, parse } from 'path'; import { Folders, WORKSPACE_PLACEHOLDER } from '../commands/Folders'; import { existsSync, renameSync } from 'fs'; import { Notifications } from './Notifications'; +import { parseWinPath } from './parseWinPath'; interface MediaRecord { description: string; @@ -17,7 +18,7 @@ export class MediaLibrary { private constructor() { const wsFolder = Folders.getWorkspaceFolder(); - this.db = new JsonDB(join(wsFolder?.fsPath || "", '.frontmatter/mediaDb.json'), true, false, '/'); + this.db = new JsonDB(join(parseWinPath(wsFolder?.fsPath || ""), '.frontmatter/mediaDb.json'), true, false, '/'); workspace.onDidRenameFiles(e => { e.files.forEach(f => { @@ -93,7 +94,7 @@ export class MediaLibrary { private parsePath(path: string) { const wsFolder = Folders.getWorkspaceFolder(); const isWindows = process.platform === 'win32'; - let absPath = path.replace(wsFolder?.fsPath || "", WORKSPACE_PLACEHOLDER); + let absPath = path.replace(parseWinPath(wsFolder?.fsPath || ""), WORKSPACE_PLACEHOLDER); absPath = isWindows ? absPath.split('\\').join('/') : absPath; return absPath.toLowerCase(); } diff --git a/src/helpers/parseWinPath.ts b/src/helpers/parseWinPath.ts index b738b46c..6002e669 100644 --- a/src/helpers/parseWinPath.ts +++ b/src/helpers/parseWinPath.ts @@ -1,3 +1,3 @@ -export const parseWinPath = (path: string | undefined) => { - return path?.split(`\\`).join(`/`); +export const parseWinPath = (path: string | undefined): string => { + return path?.split(`\\`).join(`/`) || ''; } \ No newline at end of file