From fce34a4f0e7e6b46c55cea96e82e8b0e06b819da Mon Sep 17 00:00:00 2001 From: bingryan <41174435+bingryan@users.noreply.github.com> Date: Fri, 5 May 2023 22:51:52 +0800 Subject: [PATCH] feat: add toggle for backup exist (#45) * feat: add toggle for backup exist --- src/config.ts | 2 ++ src/export.ts | 23 +++++++++++++++++------ src/settings.ts | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index bcc8ecc..1416896 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,6 +6,7 @@ export const IBOOK_SNAPSHOTS = '~/Library/Containers/com.apple.iBooksX/Data/Docu export interface IbookPluginSettings { output: string; notExportNoAnnotation: boolean; + backupWhenExist: boolean; template: string; } @@ -49,5 +50,6 @@ tags: export const DEFAULT_SETTINGS: IbookPluginSettings = { output: 'ibook', notExportNoAnnotation: true, + backupWhenExist: true, template: defaultTemplate }; diff --git a/src/export.ts b/src/export.ts index 8000274..b69aa91 100644 --- a/src/export.ts +++ b/src/export.ts @@ -2,11 +2,11 @@ import { Annotation } from "@/types"; import { getAllBookId, getBookById, getAnnotationBookId } from "@/api/ibook"; import { IbookPluginSettings } from "@/config"; import { Renderer } from "@/renderer"; -import { htmlToMarkdown } from "obsidian"; +import { htmlToMarkdown, normalizePath } from "obsidian"; import IbookPlugin from "@/plugin"; import * as path from "path"; -import { tryCreateFolder, removeTags,htmlDecode } from "@/utils/misc"; +import { tryCreateFolder, removeTags, htmlDecode } from "@/utils/misc"; export interface IExport { all(): void; @@ -57,9 +57,9 @@ export class IBookExport implements IExport { renderData.library.ZBOOKDESCRIPTION ); } - + /// fix: #36 - if (renderData.library.ZTITLE !== null) { + if (renderData.library.ZTITLE !== null) { renderData.library.ZTITLE = htmlDecode( renderData.library.ZTITLE ); @@ -94,10 +94,21 @@ export class IBookExport implements IExport { }; } - save(contentName: string, content: string) { + async save(contentName: string, content: string) { + const fileName = `${contentName}` + .replace(/:/g, '-') + .replace(/(\r\n|\n|\r|\/|\\\\)/gm, "-") try { + const filePath = normalizePath(path.join(this.plugin.settings.output, `${fileName}.md`)); + const isExist = await this.plugin.app.vault.adapter.exists(filePath); + if (this.plugin.settings.backupWhenExist && isExist) { + // backup file if file already exists + // issue: #44 + const backupPath = normalizePath(path.join(this.plugin.settings.output, `${fileName}-bk-${Date.now()}.md`)); + this.plugin.app.vault.adapter.rename(filePath, backupPath); + } this.plugin.app.vault.create( - path.join(this.plugin.settings.output, `${contentName}.md`), + path.join(this.plugin.settings.output, `${fileName}.md`), content ); } catch (error) { diff --git a/src/settings.ts b/src/settings.ts index 2f68cbf..75d62c8 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -49,8 +49,21 @@ export class IbookSettingTab extends PluginSettingTab { this.plugin.settings.notExportNoAnnotation = value; await this.plugin.saveSettings(); }) - ); + ); + new Setting(containerEl) + .setName("backup old export markdown when exist") + .setDesc( + "if a export book with the same name is found, the previous export markdown will be backed up: bookname.md -> bookname.bk.time.md" + ) + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.backupWhenExist) + .onChange(async (value: boolean) => { + this.plugin.settings.backupWhenExist = value; + await this.plugin.saveSettings(); + }) + ); new Setting(containerEl) .setName('template') .setClass("ibook-template-item")