From 3d79ff0d41af418dbcaab9fc244c7c5c9008203b Mon Sep 17 00:00:00 2001 From: Haril Song Date: Thu, 9 Mar 2023 11:26:50 +0900 Subject: [PATCH] fix: o2-53 create folder in assets/img without resource (#55) --- src/jekyll/ResourceLinkConverter.ts | 12 +++++++----- src/tests/ResourceLinkConverter.test.ts | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/jekyll/ResourceLinkConverter.ts b/src/jekyll/ResourceLinkConverter.ts index 4f1cfea1..3db0d313 100644 --- a/src/jekyll/ResourceLinkConverter.ts +++ b/src/jekyll/ResourceLinkConverter.ts @@ -18,11 +18,12 @@ export class ResourceLinkConverter extends AbstractConverter { convert(input: string): string { const jekyllSetting = this.plugin.settings.jekyllSetting(); const resourcePath = `${jekyllSetting.resourcePath()}/${this.title}`; - fs.mkdirSync(resourcePath, { recursive: true }); - const relativeResourcePath = jekyllSetting.jekyllRelativeResourcePath; - - extractImageName(input)?.forEach((resourceName) => { + const resourceNames = extractResourceNames(input); + if (!(resourceNames === undefined || resourceNames.length === 0)) { + fs.mkdirSync(resourcePath, { recursive: true }); + } + resourceNames?.forEach((resourceName) => { fs.copyFile( `${(vaultAbsolutePath(this.plugin))}/${jekyllSetting.attachmentsFolder}/${resourceName}`, `${resourcePath}/${(resourceName.replace(/\s/g, '-'))}`, @@ -36,6 +37,7 @@ export class ResourceLinkConverter extends AbstractConverter { ); }); + const relativeResourcePath = jekyllSetting.jekyllRelativeResourcePath; const replacer = (match: string, p1: string) => `![image](/${relativeResourcePath}/${this.title}/${p1.replace(/\s/g, '-')})`; @@ -45,7 +47,7 @@ export class ResourceLinkConverter extends AbstractConverter { } } -export function extractImageName(content: string) { +export function extractResourceNames(content: string) { const regExpMatchArray = content.match(ObsidianRegex.IMAGE_LINK); return regExpMatchArray?.map( (value) => { diff --git a/src/tests/ResourceLinkConverter.test.ts b/src/tests/ResourceLinkConverter.test.ts index 6435e448..e2df765e 100644 --- a/src/tests/ResourceLinkConverter.test.ts +++ b/src/tests/ResourceLinkConverter.test.ts @@ -1,4 +1,4 @@ -import { extractImageName } from "../jekyll/ResourceLinkConverter"; +import { extractResourceNames } from "../jekyll/ResourceLinkConverter"; jest.mock('obsidian', () => ({}), { virtual: true }); @@ -10,7 +10,7 @@ describe("extract image name", () => { test ![[image.png]] `; - const result = extractImageName(context); + const result = extractResourceNames(context); expect(result).toEqual(['test.png', 'image.png']); });