Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ use process() method to write new content #43

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
import Notify from 'notify';
import spacetime from 'spacetime';
import { Environment, Template, ConfigureOptions } from 'nunjucks';
Expand Down Expand Up @@ -312,16 +312,23 @@ export default class ReadwiseMirror extends Plugin {

const abstractFile = vault.getAbstractFileByPath(path);

// Delete old instance of file
if (abstractFile) {
// Overwrite existing file with remote changes, or
// Create new file if not existing
if (abstractFile && abstractFile instanceof TFile) {
// File exists
try {
await vault.delete(abstractFile);
await vault.process(abstractFile, function(data) {
// Simply return new contents to overwrite file
return contents;
});
} catch (err) {
console.error(`Readwise: Attempted to delete file ${path} but no file was found`, err);
console.error(`Readwise: Attempt to overwrite file ${path} failed`, err);
}
} else {
// File does not exist
vault.create(path, contents);
}

vault.create(path, contents);

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/node": "^14.14.37",
"obsidian": "^0.12.0",
"obsidian": "^1.1.1",
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
Expand Down