genanki-js
A JavaScript implementation for generating Anki decks in browser. This is fork of mkanki.
QuickStart
genanki-js can be used from npm (with a bundler such as Vite, or in Node.js), or with a plain <script> tag.
npm
npm install genanki-js
import { Model, Deck, Package } from "genanki-js"
// or: const { Model, Deck, Package } = require("genanki-js")
JSZip and sql.js are installed as dependencies and loaded automatically, nothing has to be configured. sql.js is only loaded when the first package is generated.
- Browser:
package.writeToFile('deck.apkg')downloads the file. - Node.js: use
package.generate('nodebuffer')and write the result yourself:import { writeFile } from "node:fs/promises" await writeFile("deck.apkg", await p.generate("nodebuffer"))
esbuild and webpack configuration
sql.js contains require("node:fs") and require("node:crypto") calls that only run in Node.js. Vite handles this automatically. For esbuild and webpack, mark them as external:
esbuild src/main.js --bundle --external:node:*
// webpack.config.js
module.exports = {
// ...
externals: { "node:fs": "{}", "node:crypto": "{}" },
}
To use another sql.js build (for example sql-wasm.js with your own locateFile), pass it with setSqlJs:
import initSqlJs from "sql.js"
import { setSqlJs } from "genanki-js"
setSqlJs(await initSqlJs({ locateFile: file => `/assets/${file}` }))
Script tag
Download the genanki zip file from the release page. The zip file contains two folders
dist: This folder containsgenanki.jsfile. If project already configured with sql.js and JSZip then just addgenanki.jsfile to project.sample: This folder contains sql.js, FileSaver.js, JSZip and genanki.js. It is ready to use folder.
Alternatively, genanki.js can also be loaded from CDN.
<script src="https://cdn.jsdelivr.net/npm/genanki-js@2.1.0/dist/genanki.js"></script>
Set up a new project from scratch
Add sql.js and JSZip to the project, then
genanki.jsfrom thedistfolder. FileSaver.js is optional, it is used for downloading when available.Note: mkanki uses
better-sql,fsandarchiver, that make it difficult to be used in browser
<!-- sqlite, sql-wasm-inline.js from sql.js renamed to sql.js -->
<script src='js/sql/sql.js'></script>
<!-- jszip for .apkg -->
<script src="js/jszip.min.js"></script>
<!-- optional, File saver -->
<script src="js/filesaver/FileSaver.min.js"></script>
<!-- for creating and exporting anki package file -->
<script src='genanki.js'></script>
- genanki.js calls the global
initSqlJs()when the first package is generated. If sql.js needs a configuration, for example the location of the.wasmfile, initialize it yourself and pass it to genanki.js
initSqlJs({
locateFile: filename => `js/sql/${filename}`
}).then(function (SQL) {
setSqlJs(SQL);
});
- Now use following
Examplesto generate and export decks.
The classes are available as globals (Model, Deck, Package, ...) and in the genanki namespace (genanki.Model, ...).
Documentation
View Documentation
View more examples here Examples
CSV/TSV to Anki Package
Visit page and select CSV to APKG from top menu.
CSV to APKG
Examples
var m = new Model({
name: "Basic (and reversed card)",
id: "1543634829843",
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}}",
}
],
})
var d = new Deck(1276438724672, "Test Deck")
d.addNote(m.note(['this is front', 'this is back']))
var p = new Package()
p.addDeck(d)
p.writeToFile('deck.apkg')
Cloze
var m = new ClozeModel({
name: "Cloze",
id: "1550428389000",
flds: [
{ name: "Text" },
{ name: "Back Extra" }
],
tmpl: {
qfmt: "{{cloze:Text}}",
afmt: "{{cloze:Text}}<br>{{Back Extra}}",
},
})
var d = new Deck(1550428389001, "Cloze Deck")
d.addNote(m.note(['{{c1::Paris}} is the capital of {{c2::France}}', 'Europe']))
var p = new Package()
p.addDeck(d)
p.writeToFile('cloze-deck.apkg')
Development
npm install
npm run build # bundle genanki/*.js to dist/ (and the demo and sample folders)
npm test
npm run vendor # update sql.js, JSZip and FileSaver in the demo and sample folders
License
genanki-js
GNU Affero General Public License v3
Copyright (c) 2021 Mani