Skip to content

Instantly share code, notes, and snippets.

@jzr-supove
Last active February 26, 2025 10:00
Show Gist options
  • Save jzr-supove/a9fab276ceefd4ddd5996e6281114d55 to your computer and use it in GitHub Desktop.
Save jzr-supove/a9fab276ceefd4ddd5996e6281114d55 to your computer and use it in GitHub Desktop.
Obsidian QuickAdd Modal for Adding Words to Vocabulary

Obsidian QuickAdd Modal for Adding Words to Vocabulary

🟑 Prerequisites

  • You should have QuickAdd and Spaced Repetition plugins installed on your Obsidian
  • In the script, change the flashcardsFolder value to the folder where you store your flashcards (default "Flashcards");
    • Folder should have at least one file where you store your flashcards


🟒 Installation

  1. Copy the vocabulary.js script below to your vault (e.g. "Scripts" folder)
  2. Go to QuickAdd --> Manage Macros.
  3. Type 'vocabulary' and click Add Macro
  4. From the macro list, find the newly created macro and click Configure
  5. In config window, navigate to User Scripts section, select the script you've added from the dropdown list and press Add
  6. Close the config window, as well as the macro manager window
  7. In the main menu of QuickAdd plugin, create "Macro" type choice by providing a name for it and pressing Add Choice.
  8. From the choices list, find your newly created choice, and select βš™οΈ (gear icon)
  9. Select the newly created macro from the dropdown list, and close the window
  10. Enable the choice (command) to appear in the command palette by clicking on the ⚑ (high voltage icon)

πŸ”΅ Usage

  • Ribbon Button: Assign this macro to a QuickAdd command and add it as a button.
  • Command Palette: Use "QuickAdd: Add New Word" from the command palette (Ctrl+P)
let QuickAdd;
async function addVocabularyWord(params) {
QuickAdd = params;
const flashcardsFolder = 'Flashcards';
// Get list of flashcard tags (files)
const files = app.vault
.getFiles()
.filter((file) => file.path.startsWith(flashcardsFolder))
.map((file) => file.name.replace('.md', ''));
if (files.length === 0) {
new Notice(`No flashcard files found in '${flashcardsFolder}'!`);
return;
}
// Show dropdown to select tag (file)
const selectedTag = await QuickAdd.quickAddApi.suggester(files, files);
if (!selectedTag) return;
// Show input boxes for Word and Translation
const word = await QuickAdd.quickAddApi.inputPrompt('Enter the Word:');
if (!word) return;
const translation = await QuickAdd.quickAddApi.inputPrompt(
'Enter the Translation:'
);
if (!translation) return;
// Construct the entry
const entry = `\n\n${word} ::: ${translation}`;
// Find the file path for the selected tag
const selectedFile = app.vault.getAbstractFileByPath(
`${flashcardsFolder}/${selectedTag}.md`
);
if (selectedFile) {
// Append to the selected file
app.vault.read(selectedFile).then(async (content) => {
await app.vault.modify(selectedFile, content + entry);
new Notice(`Added: "${word} ::: ${translation}" to ${selectedTag}.md`);
});
} else {
new Notice(`Error: File ${selectedTag}.md not found.`);
}
}
// Register the script in QuickAdd
module.exports = addVocabularyWord;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment