npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

anki-reader

v0.3.0

Published

A library for reading Anki apkg and collection files.

Downloads

38

Readme

anki-reader

npm version npm downloads

A .apkg and .anki2 file reader module. Compatable node, bun, and browser runtimes.

Usage

Installation

Using npm:

$ npm install anki-reader

Using bun:

$ bun install anki-reader

Usage

After installation, pass in .apkg or .anki2 files to their reader functions. This returns a collection object, which can be used to retrieve deck and card information. Additionally, if a .apkg file is returned, media files are also extracted.

import { readAnkiPackage } from 'anki-reader';

const ankiFile = ...
readAnkiPackage(ankiFile)
  .then((extractedPackage) => {
    const { collection, media } = extractedPackage;

    const decks = collection.getDecks();
    for (const [deckId, deck] of Object.entries(decks)) {
      console.log(deckId, deck.getRawDeck());
      for (const [cardId, card] of Object.entries(deck.getCards())) {
        console.log(cardId, card.getRawCard());
      }
    }

    for (const [name, blob] of Object.entries(media)) {
      console.log(name, blob);
    }
  })

Alternatively, you can read a collection file directly from a URL with readFromUrl like so:

import { readFromUrl } from 'anki-reader';

readFromUrl('http://127.0.0.1:8081/collection.anki2')
  .then((collection) => {
    const decks = collection.getDecks();
    for (const [deckId, deck] of Object.entries(decks)) {
      console.log(deckId, deck.getRawDeck());
      for (const [cardId, card] of Object.entries(deck.getCards())) {
        console.log(cardId, card.getRawCard());
      }
    }
  })

For more in-depth examples, see the story directory.

Browser

If you intend to use anki-reader in a browser runtime, you must configure additional settings so sql.js can locate the wasm file. See the official sql.js documentation and this React example.

React

// App.js
import sqlWasm from "!!file-loader?name=sql-wasm-[contenthash].wasm!sql.js/dist/sql-wasm.wasm";

export default function App() {
  const ankiFile = ...
  readAnkiCollection(ankiFile, {
    sqlConfig: { 
      locateFile: () => sqlWasm 
    },
  }

  ...

}
// webpack.config.js
module.exports = {
    webpack: {
        configure: {
            // See https://github.com/webpack/webpack/issues/6725
            module:{
                rules: [{
                    test: /\.wasm$/,
                    type: 'javascript/auto',
                }]
                ...
            }
        }
    }
};

API

Anki collections are stored as sqlite databases, which is what anki-reader queries. The anki-reader follows similarly to the data structure of the .anki2 database structure, which can be referenced here.

This package also provides methods to get the raw database objects or models. For example, AnkiCollection.getRawCollection() may be used to get the raw collection database, and can then use the sql.js API to query.

Development (WIP)

To install dependencies:

bun install

To run:

Refer to story/README.md

To build typescript:

npm run build

This project was created using bun init in bun v1.0.7. Bun is a fast all-in-one JavaScript runtime.