gemanki
v1.0.2
Published
Generate Anki .apkg decks from Node.js (ESM)
Maintainers
Readme
gemanki
Create Anki .apkg decks from Node.js.
gemanki is a small ESM library for generating Anki package. It writes the Anki SQLite collection with sql.js and returns the final .apkg archive as a Node Buffer.
Requirements
- Node.js 18 or newer
- ESM project or dynamic
import()
Installation
npm install gemankiFor local development in this repository:
npm install
npm testUsage
import { writeFile } from "node:fs/promises";
import initSqlJs from "sql.js";
import { Deck, Model, Package } from "gemanki";
const SQL = await initSqlJs();
const apkg = new Package();
apkg.setSqlJs(new SQL.Database());
const deck = new Deck(1234567890, "German Vocabulary");
const basicModel = new Model({
id: 987654321,
name: "Basic",
flds: [{ name: "Front" }, { name: "Back" }],
req: [[0, "all", [0]]],
tmpls: [
{
name: "Card 1",
qfmt: "{{Front}}",
afmt: "{{FrontSide}}<hr id=\"answer\">{{Back}}",
},
],
});
deck.addNote(basicModel.note(["Hallo", "Hello"], ["german"]));
deck.addNote(basicModel.note({ Front: "Danke", Back: "Thanks" }));
apkg.addDeck(deck);
const buffer = await apkg.writeToBuffer();
await writeFile("german-vocabulary.apkg", buffer);The generated .apkg can be imported directly into Anki.
API
new Package()
Container for one or more decks.
setSqlJs(db)sets asql.jsdatabase instance, for examplenew SQL.Database().addDeck(deck)adds aDeck.writeToBuffer()returns aPromise<Buffer>containing the.apkgarchive.
new Deck(id, name, desc = "")
Creates an Anki deck definition.
idshould be a stable positive integer.nameis the deck name shown in Anki.addNote(note)appends a note to the deck.
new Model(props)
Creates an Anki note type. Required properties:
id: stable positive integer model IDname: model nameflds: field definitions, for example[{ name: "Front" }, { name: "Back" }]tmpls: card templates withqfmtandafmtreq: card generation rules, for example[[0, "all", [0]]]
Use model.note(fields, tags?, guid?) to create notes. fields can be either an array in model field order or an object keyed by field name.
Limitations
- Media files are not supported yet. The generated package includes an empty Anki
mediamanifest. - This package targets Node.js ESM only.
Publishing Checklist
Before publishing a new version:
npm test
npm pack --dry-run
npm publishCheck that the package name is available for your npm account and update package.json metadata such as author, repository, homepage, and bugs once the public repository URL is known.
License
MIT
