Home

Awesome

mkanki

An API for generating Anki decks.

Anki is a tool for spaced-repetition study.

Example

Here's an example of generating a simple deck with mkanki.

$ npm install --save mkanki
const anki = require('mkanki')
const m = new anki.Model({
  name: "Basic (and reversed card)",
  id: "1542906796044",
  flds: [
    { name: "Front" },
    { name: "Back" }
  ],
  req: [
    [ 0, "all", [ 0 ] ],
    [ 1, "all", [ 1 ] ]
  ],
  tmpls: [
    {
      name: "Card 1",
      qfmt: "{{Front}}",
      afmt: "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
    },
    {
      name: "Card 2",
      qfmt: "{{Back}}",
      afmt: "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}",
    }
  ],
})

const d = new anki.Deck(1542998993960, "hi")

d.addNote(m.note(['this is front', 'this is back']))

const p = new anki.Package()
p.addDeck(d)
p.writeToFile('deck.apkg')

Documentation

First, some information about how Anki works.

In Anki, things to be remembered are called notes. Each note can have several fields—most commonly, a "Front" and a "Back", but the fields can be arbitrary. Each note can potentially correspond to many individual cards, each of which should help you remember one facet of the note.

The thing that describes how those fields are turned into flashcards is called a model. Every note is described by exactly one model. The model defines which fields are allowed, and additionally defines one or more templates, which are written as HTML with mustache-like placeholders.

Finally, each card belongs to a deck. Decks collect cards into logical groups that you might want to study separately from each other.

Models, notes, cards and decks are the fundamental concepts of Anki. In mkanki, cards are implicitly defined by notes and models, and you will only deal with models, notes, and decks.

Model

Anki supports two types of models: standard and cloze. Standard models generate one card per template, while cloze models generate one card per cloze deletion. See the Anki cloze documentation for more on cloze deletion.

new anki.Model(props)

Create a new standard model with the given properties. props is an object with the following fields.

new anki.ClozeModel(props)

Create a new cloze model with the given properties. props is an object with the following fields.

model.note(fieldValues, [guid])

Create a note using this model.

Deck

In mkanki, decks are collections of notes (not cards, as in Anki proper).

new anki.Deck(id, name)

Create a new deck.

deck.addNote(note)

Add a note to this deck. Technically, it is possible for a single note in Anki to generate cards belonging to multiple decks, but mkanki does not support that.

Package

A package collects together decks, notes, and any media objects (images, audio, video, etc.) to be exported into a .apkg file.

new anki.Package()

Create a new empty package.

package.addDeck(deck)

Add a deck to this package.

package.addMedia(data, name)

Add a media file to this package.

package.addMediaFile(filename, [name])

Add a media file from the filesystem to this package.

package.writeToFile(filename)

Serializes the package to a file.

License

mkanki is licensed to everyone under the terms of the GNU Affero General Public License v3. If you'd like to use mkanki under different terms, I'm happy to accommodate you—just email me!