Skip to content

Commit

Permalink
🐛 use path.join to concat paths
Browse files Browse the repository at this point in the history
  • Loading branch information
JichouP committed Mar 2, 2023
1 parent f83baa0 commit 16104f0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/convertImage.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -37,7 +37,7 @@ async function convertToBase64(path: string): Promise<string | null> {
const basePath = (
this.app.vault.adapter as FileSystemAdapter
).getBasePath();
return readFileAsBase64(normalize(`${basePath}/${path}`));
return readFileAsBase64(normalize(join(basePath, path)));
}

try {
Expand Down
24 changes: 16 additions & 8 deletions src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ 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,
ext: 'html' | 'pdf' | 'pptx',
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;

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' })),
Expand Down
4 changes: 2 additions & 2 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 16104f0

Please sign in to comment.