Action

Anki Cards from Q&A

Posted by FlohGro, Last update almost 2 years ago - Unlisted

created by @FlohGro / more on my Blog

AnkiMobile Flashcard from Q&A

Creates flashcards in AnkiMobile from drafts formatted like this


Q: test question 1
A: test answer 1

Q: test question 2
A: test answer 2

Q: test question 3
A: test answer 3

Q: test question 4
A: test answer 4

Q: test question 5
A: test answer 5

If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    const DELIM = "Q:";
    
    // optional parameters for the callback url scheme of anki, leave empty to use defaults
    const profileName = ""
    const deckName = ""
    
    let astrCards = draft.content.split("\n" + DELIM);
    
    astrCards.map(strCard =>
    {
    	if (strCard.length > 0)
    	{
    	     
    	    if (!strCard.startsWith(DELIM)) strCard = DELIM + strCard;
    
                const lines = strCard.split('\n')
                const question = lines[0].replace("Q: ", "")
                const answer = lines[1].replace("A: ", "")
    
    				const baseURL = "anki://x-callback-url/addnote?"
                let cb = new CallbackURL()
                cb.baseURL = baseURL;
                // profile parameter
                if(profileName.length > 0) cb.addParameter("profile", profileName);
                // type parameter
                cb.addParameter("type", "Basic");
                // deck parameter
                if(deckName.length >0 ) cb.addParameter("deck", deckName);
                // now the front of the card by using the question variable
                cb.addParameter("fldFront", question);
                // now the back of the card by using the answer variable
                cb.addParameter("fldBack", answer);
                let success = cb.open();
                if (success) {
                    // maybe log the created card 
                    console.log("Card with question \"" + question + "\" created");
                    }
                    else { 
                    // something went wrong or was cancelled
                    console.log(cb.status);
                    if (cb.status == "cancelled") {
                        context.cancel();
                    }
                    else {
                    context.fail();
                    }
                }   
    	}
    });

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.