Skip to content

A more efficient way of updating tags + Scan current file only #584

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default class MyPlugin extends Plugin {
return allTFiles;
}

async scanVault() {
async scanVault(file:TFile) {
new Notice('Scanning vault, check console for details...');
console.info("Checking connection to Anki...")
try {
Expand All @@ -197,20 +197,23 @@ export default class MyPlugin extends Plugin {
const data: ParsedSettings = await settingToData(this.app, this.settings, this.fields_dict)
const scanDir = this.app.vault.getAbstractFileByPath(this.settings.Defaults["Scan Directory"])
let manager = null;
if (scanDir !== null) {
let markdownFiles = [];
if (scanDir instanceof TFolder) {
console.info("Using custom scan directory: " + scanDir.path)
markdownFiles = this.getAllTFilesInFolder(scanDir);
} else {
new Notice("Error: incorrect path for scan directory " + this.settings.Defaults["Scan Directory"])
return
}
manager = new FileManager(this.app, data, markdownFiles, this.file_hashes, this.added_media)
} else {
manager = new FileManager(this.app, data, this.app.vault.getMarkdownFiles(), this.file_hashes, this.added_media);
}

if (file !== undefined) {
manager = new FileManager(this.app, data, [file], this.file_hashes, this.added_media);
} else {
if (scanDir !== null) {
let markdownFiles = [];
if (scanDir instanceof TFolder) {
console.info("Using custom scan directory: " + scanDir.path)
markdownFiles = this.getAllTFilesInFolder(scanDir);
} else {
new Notice("Error: incorrect path for scan directory " + this.settings.Defaults["Scan Directory"])
return
}
manager = new FileManager(this.app, data, markdownFiles, this.file_hashes, this.added_media)
} else {
manager = new FileManager(this.app, data, this.app.vault.getMarkdownFiles(), this.file_hashes, this.added_media);
}
}
await manager.initialiseFiles()
await manager.requests_1()
this.added_media = Array.from(manager.added_media_set)
Expand Down Expand Up @@ -253,14 +256,22 @@ export default class MyPlugin extends Plugin {
this.addSettingTab(new SettingsTab(this.app, this));

this.addRibbonIcon('anki', 'Obsidian_to_Anki - Scan Vault', async () => {
await this.scanVault()
await this.scanVault(undefined)
})

this.addCommand({
id: 'anki-scan-vault',
name: 'Scan Vault',
callback: async () => {
await this.scanVault()
await this.scanVault(undefined)
}
})

this.addCommand({
id: 'anki-scan-file',
name: 'Scan Current File',
callback: async () => {
await this.scanVault(this.app.workspace.getActiveFile())
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 3 additions & 12 deletions src/anki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,10 @@ export function changeDeck(card_ids: number[], deck: string): AnkiConnectRequest
)
}

export function removeTags(note_ids: number[], tags: string): AnkiConnectRequest {
export function updateNoteTags(note_id: number, tags: string[]): AnkiConnectRequest {
return request(
'removeTags', {
notes: note_ids,
tags: tags
}
)
}

export function addTags(note_ids: number[], tags: string): AnkiConnectRequest {
return request(
'addTags', {
notes: note_ids,
'updateNoteTags', {
note: note_id,
tags: tags
}
)
Expand Down
13 changes: 3 additions & 10 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,12 @@ abstract class AbstractFile {
return AnkiConnect.changeDeck(this.card_ids, this.target_deck)
}

getClearTags(): AnkiConnect.AnkiConnectRequest {
let IDs: number[] = []
for (let parsed of this.notes_to_edit) {
IDs.push(parsed.identifier)
}
return AnkiConnect.removeTags(IDs, this.tags.join(" "))
}

getAddTags(): AnkiConnect.AnkiConnectRequest {
getUpdateTags(): AnkiConnect.AnkiConnectRequest {
let actions: AnkiConnect.AnkiConnectRequest[] = []
for (let parsed of this.notes_to_edit) {
let tags = parsed.note.tags.join(" ") + " " + this.global_tags
actions.push(
AnkiConnect.addTags([parsed.identifier], parsed.note.tags.join(" ") + " " + this.global_tags)
AnkiConnect.updateNoteTags(parsed.identifier, tags.split(" "))
)
}
return AnkiConnect.multi(actions)
Expand Down
16 changes: 7 additions & 9 deletions src/files-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,22 @@ export class FileManager {
let temp: AnkiConnect.AnkiConnectRequest[] = []
console.info("Requesting cards to be moved to target deck...")
for (let file of this.ownFiles) {
temp.push(file.getChangeDecks())
let deck = file.getChangeDecks()
if (deck.params.cards && deck.params.cards.length) {
temp.push(deck)
}
}
requests.push(AnkiConnect.multi(temp))
temp = []
console.info("Requesting tags to be replaced...")
for (let file of this.ownFiles) {
let rem = file.getClearTags()
if(rem.params.notes.length) {
temp.push(rem)
let update = file.getUpdateTags()
if(update.params.actions.length) {
temp.push(update)
}
}
requests.push(AnkiConnect.multi(temp))
temp = []
for (let file of this.ownFiles) {
temp.push(file.getAddTags())
}
requests.push(AnkiConnect.multi(temp))
temp = []
await AnkiConnect.invoke('multi', {actions: requests})
console.info("All done!")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-to-anki-plugin",
"name": "Obsidian_to_Anki",
"version": "3.4.2",
"version": "3.6.0",
"minAppVersion": "0.9.20",
"description": "This is an Anki integration plugin! Designed for efficient bulk exporting.",
"author": "Pseudonium",
Expand Down
Loading