Skip to content

Commit

Permalink
feat: add toggle for backup exist (#45)
Browse files Browse the repository at this point in the history
* feat: add toggle for backup exist
  • Loading branch information
bingryan authored May 5, 2023
1 parent e65be22 commit fce34a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Expand Down Expand Up @@ -49,5 +50,6 @@ tags:
export const DEFAULT_SETTINGS: IbookPluginSettings = {
output: 'ibook',
notExportNoAnnotation: true,
backupWhenExist: true,
template: defaultTemplate
};
23 changes: 17 additions & 6 deletions src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit fce34a4

Please sign in to comment.