generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): split functionality for better testability
- Loading branch information
1 parent
2e1d747
commit a76650d
Showing
8 changed files
with
102 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { App, Vault } from 'obsidian'; | ||
import path from 'path'; | ||
import { ICombinedBooksAndHighlights } from '../types'; | ||
import { AppleBooksHighlightsImportPluginSettings } from '../settings'; | ||
import { renderHighlightsTemplate } from './renderHighlightsTemplate'; | ||
|
||
export default class SaveHighlights { | ||
private app: App; | ||
private vault: Vault; | ||
private settings: AppleBooksHighlightsImportPluginSettings; | ||
|
||
constructor(app: App, settings: AppleBooksHighlightsImportPluginSettings) { | ||
this.app = app; | ||
this.vault = this.app.vault; | ||
this.settings = settings; | ||
} | ||
|
||
async saveHighlightsToVault(highlights: ICombinedBooksAndHighlights[]): Promise<void> { | ||
const highlightsFolderPath = this.vault.getAbstractFileByPath( | ||
this.settings.highlightsFolder | ||
); | ||
|
||
const isBackupEnabled = this.settings.backup; | ||
|
||
// // Backup highlights folder if backup is enabled | ||
if (highlightsFolderPath) { | ||
if (isBackupEnabled) { | ||
const highlightsFilesToBackup = (await this.vault.adapter.list(highlightsFolderPath.path)).files; | ||
|
||
const highlightsBackupFolder = `${this.settings.highlightsFolder}-bk-${Date.now()}`; | ||
|
||
await this.vault.createFolder(highlightsBackupFolder); | ||
|
||
highlightsFilesToBackup.forEach(async (file: string) => { | ||
const fileName = path.basename(file); | ||
|
||
await this.vault.adapter.copy(file, path.join(highlightsBackupFolder, fileName)) | ||
}); | ||
} | ||
|
||
await this.vault.delete(highlightsFolderPath, true); | ||
} | ||
|
||
await this.vault.createFolder(this.settings.highlightsFolder); | ||
|
||
highlights.forEach(async (highlight: ICombinedBooksAndHighlights) => { | ||
const renderedTemplate = await renderHighlightsTemplate(highlight, this.settings.template); | ||
const filePath = path.join(this.settings.highlightsFolder, `${highlight.bookTitle}.md`); | ||
|
||
await this.vault.create( | ||
filePath, | ||
renderedTemplate | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters