Skip to content

Commit

Permalink
Sync media in Obsidian directly to Anki instead of using URL link. (#21)
Browse files Browse the repository at this point in the history
* Sync media in Obsidian directly to Anki
instead of using url link.

---------

Co-authored-by: Songchen Tan <[email protected]>
  • Loading branch information
JYC333 and tansongchen authored Feb 8, 2023
1 parent b239309 commit f0bdc20
Show file tree
Hide file tree
Showing 6 changed files with 781 additions and 4,512 deletions.
32 changes: 24 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizePath, Notice, Plugin } from 'obsidian';
import Anki, { AnkiError } from 'src/anki';
import Note, { NoteManager } from 'src/note';
import { MediaManager } from 'src/media';
import locale from 'src/lang';
import { NoteDigest, NoteState, NoteTypeDigest, NoteTypeState } from 'src/state';
import AnkiSynchronizerSettingTab, { Settings, DEFAULT_SETTINGS } from 'src/setting';
Expand All @@ -16,6 +17,7 @@ interface Data {
export default class AnkiSynchronizer extends Plugin {
anki = new Anki();
settings = DEFAULT_SETTINGS;
mediaManager = new MediaManager();
noteManager = new NoteManager(this.settings);
noteState = new NoteState(this);
noteTypeState = new NoteTypeState(this);
Expand All @@ -40,17 +42,17 @@ export default class AnkiSynchronizer extends Plugin {
configureUI() {
// Add import note types command
this.addCommand({
id: 'import',
id: "import",
name: locale.importCommandName,
callback: async () => await this.importNoteTypes()
callback: async () => await this.importNoteTypes(),
});
this.addRibbonIcon('enter', locale.importCommandName, async () => await this.importNoteTypes());

// Add synchronize command
this.addCommand({
id: 'synchronize',
id: "synchronize",
name: locale.synchronizeCommandName,
callback: async () => await this.synchronize()
callback: async () => await this.synchronize(),
});
this.addRibbonIcon(
'sheets-in-box',
Expand All @@ -68,7 +70,7 @@ export default class AnkiSynchronizer extends Plugin {
version: version,
settings: this.settings,
noteState: Object.fromEntries(this.noteState),
noteTypeState: Object.fromEntries(this.noteTypeState)
noteTypeState: Object.fromEntries(this.noteTypeState),
});
}

Expand All @@ -79,7 +81,7 @@ export default class AnkiSynchronizer extends Plugin {

// Retrieve template information from Obsidian core plugin "Templates"
getTemplatePath() {
const templatesPlugin = (this.app as any).internalPlugins?.plugins['templates'];
const templatesPlugin = (this.app as any).internalPlugins?.plugins["templates"];
if (!templatesPlugin?.enabled) {
new Notice(locale.templatesNotEnabledNotice);
return;
Expand Down Expand Up @@ -138,7 +140,21 @@ export default class AnkiSynchronizer extends Plugin {
const content = await this.app.vault.cachedRead(file);
const frontmatter = this.app.metadataCache.getFileCache(file)?.frontmatter;
if (!frontmatter) continue;
const note = this.noteManager.validateNote(file, frontmatter, content, this.noteTypeState);
const media = this.app.metadataCache.getFileCache(file)?.embeds;
if (media) {
for (const item of media) {
this.noteState.handleAddMedia(
this.mediaManager.parseMedia(item, this.app.vault, this.app.metadataCache)
);
}
}
const [note, mediaNameMap] = this.noteManager.validateNote(
file,
frontmatter,
content,
media,
this.noteTypeState
);
if (!note) continue;
if (note.nid === 0) {
// new file
Expand All @@ -148,7 +164,7 @@ export default class AnkiSynchronizer extends Plugin {
continue;
}
note.nid = nid;
this.app.vault.modify(file, this.noteManager.dump(note));
this.app.vault.modify(file, this.noteManager.dump(note, mediaNameMap));
}
state.set(note.nid, [note.digest(), note]);
}
Expand Down
Loading

0 comments on commit f0bdc20

Please sign in to comment.