From 16104f0fd265ee3d955241f3a6f26b40f6a31d4a Mon Sep 17 00:00:00 2001 From: JichouP Date: Thu, 2 Mar 2023 17:20:57 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20use=20`path.join`=20to=20concat?= =?UTF-8?q?=20paths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/convertImage.ts | 4 ++-- src/export.ts | 24 ++++++++++++++++-------- src/main.ts | 8 ++++---- src/preview.ts | 4 ++-- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/convertImage.ts b/src/convertImage.ts index a9e87ae..59ba3a5 100644 --- a/src/convertImage.ts +++ b/src/convertImage.ts @@ -1,6 +1,6 @@ import { access, readFile } from 'fs/promises'; import { FileSystemAdapter } from 'obsidian'; -import { normalize } from 'path'; +import { join, normalize } from 'path'; import mimes from 'mime/lite'; @@ -37,7 +37,7 @@ async function convertToBase64(path: string): Promise { const basePath = ( this.app.vault.adapter as FileSystemAdapter ).getBasePath(); - return readFileAsBase64(normalize(`${basePath}/${path}`)); + return readFileAsBase64(normalize(join(basePath, path))); } try { diff --git a/src/export.ts b/src/export.ts index 2f1c7cf..e6d8c93 100644 --- a/src/export.ts +++ b/src/export.ts @@ -8,7 +8,7 @@ import { unified } from 'unified'; import rehypeParse from 'rehype-parse/lib'; import rehypeRemark from 'rehype-remark'; import remarkStringify from 'remark-stringify'; -import { normalize } from 'path'; +import { join, normalize } from 'path'; export async function exportSlide( file: TFile, @@ -16,12 +16,14 @@ export async function exportSlide( basePath: string, themeDir: string, ) { - const exportDir = `${ - process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'] - }/Downloads`; + const exportDir = join( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME']!, + 'Downloads', + ); if (!file) return; - const filePath = normalize(`${basePath}/${file.path}`); - const tmpPath = `${exportDir}/${file.basename}.tmp`; + const filePath = normalize(join(basePath, file.path)); + const tmpPath = join(exportDir, `${file.basename}.tmp`); let frontMatter; @@ -55,9 +57,15 @@ export async function exportSlide( let cmd: string; try { await access(themeDir); - cmd = `npx -y @marp-team/marp-cli@latest --stdin false --allow-local-files --theme-set "${themeDir}" -o "${exportDir}/${file.basename}.${ext}" -- "${tmpPath}"`; + cmd = `npx -y @marp-team/marp-cli@latest --stdin false --allow-local-files --theme-set "${themeDir}" -o "${join( + exportDir, + file.basename, + )}.${ext}" -- "${tmpPath}"`; } catch (e) { - cmd = `npx -y @marp-team/marp-cli@latest --stdin false --allow-local-files -o "${exportDir}/${file.basename}.${ext}" -- "${tmpPath}"`; + cmd = `npx -y @marp-team/marp-cli@latest --stdin false --allow-local-files -o "${join( + exportDir, + file.basename, + )}.${ext}" -- "${tmpPath}"`; } new Notice(`Exporting "${file.basename}.${ext}" to "${exportDir}"`, 20000); diff --git a/src/main.ts b/src/main.ts index ba68dff..24c4cfb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,8 +4,8 @@ import { MARP_PREVIEW_VIEW_TYPE, PreviewView } from './preview'; import { MarpSettingTab } from './settingTab'; import { readdir, readFile } from 'fs/promises'; import { marp } from './marp'; -import { normalize } from 'path'; import { existsSync } from 'fs'; +import { join, normalize } from 'path'; export default class MarpPlugin extends Plugin { settings: MarpPluginSettings; @@ -46,14 +46,14 @@ export default class MarpPlugin extends Plugin { const { themeDir } = this.settings; const isCss = (filename: string) => filename.split('.').at(-1) === 'css'; - if (themeDir && existsSync(`${basePath}/${themeDir}`)) { + if (themeDir && existsSync(join(basePath, themeDir))) { const themePaths = ( - await readdir(normalize(`${basePath}/${themeDir}`), { + await readdir(normalize(join(basePath, themeDir)), { withFileTypes: true, }) ) .filter(f => f.isFile() && isCss(f.name)) - .map(v => normalize(`${basePath}/${themeDir}/${v.name}`)); + .map(v => normalize(join(basePath, themeDir, v.name))); const cssContents = await Promise.all( themePaths.map(path => readFile(path, { encoding: 'utf-8' })), diff --git a/src/preview.ts b/src/preview.ts index c040ee1..8c3451f 100644 --- a/src/preview.ts +++ b/src/preview.ts @@ -9,7 +9,7 @@ import { convertHtml } from './convertImage'; import { exportSlide } from './export'; import { marp } from './marp'; import { MarpPluginSettings } from './settings'; -import { normalize } from 'path'; +import { join } from 'path'; export const MARP_PREVIEW_VIEW_TYPE = 'marp-preview-view'; @@ -49,7 +49,7 @@ export class PreviewView extends ItemView implements PreviewViewState { const basePath = ( this.app.vault.adapter as FileSystemAdapter ).getBasePath(); - const themeDir = normalize(`${basePath}/${this.settings.themeDir}`); + const themeDir = join(basePath, this.settings.themeDir); this.addAction('download', 'Export as PDF', () => { if (this.file) { exportSlide(this.file, 'pdf', basePath, themeDir);