Code Monkey home page Code Monkey logo

anki-apkg-export's Introduction

anki-apkg-export

Build Status

Universal module for generating decks for Anki.

Port of the Ruby gem https://github.com/albertzak/anki2

Install

$ npm install anki-apkg-export --save

Usage

server

const fs = require('fs');
const AnkiExport = require('anki-apkg-export').default;

const apkg = new AnkiExport('deck-name');

apkg.addMedia('anki.png', fs.readFileSync('anki.png'));

apkg.addCard('card #1 front', 'card #1 back');
apkg.addCard('card #2 front', 'card #2 back', { tags: ['nice', 'better card'] });
apkg.addCard('card #3 with image <img src="anki.png" />', 'card #3 back');

apkg
  .save()
  .then(zip => {
    fs.writeFileSync('./output.apkg', zip, 'binary');
    console.log(`Package has been generated: output.pkg`);
  })
  .catch(err => console.log(err.stack || err));

browser

Intended to be used with webpack

const webpack = require('webpack');

module.exports = {
  entry: './index.js',
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      },
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
      },
    })
  ],
  output: {
    path: __dirname,
    filename: 'bundle.js'
  }
};

Required loaders:

import { saveAs } from 'file-saver';
import AnkiExport from 'anki-apkg-export';

const apkg = new AnkiExport('deck-name');

// could be a File from <input /> or a Blob from fetch
// take a look at the example folder for a complete overview
apkg.addMedia('anki.png', file);

apkg.addCard('card #1 front', 'card #1 back');
apkg.addCard('card #2 front', 'card #2 back', { tags: ['nice', 'better card'] });
apkg.addCard('card #3 with image <img src="anki.png" />', 'card #3 back');

apkg
  .save()
  .then(zip => {
    saveAs(zip, 'output.apkg');
  })
  .catch(err => console.log(err.stack || err));

Examples

Changelog

  • v4.0.0 - expose template variables (frontside, backside and css)
  • v3.1.0 - make setting APP_ENV optional
  • v3.0.0 - add tags, ES6 refactor (breaking)
  • v2.0.0 - add media support, update jszip dependency (breaking)
  • v1.0.0 - initial rewrite

Tips

  • issue#25 - Dealing with sql.js memory limits

Related

License

MIT © ewnd9

anki-apkg-export's People

Contributors

amorriscode avatar dependabot[bot] avatar efernandesng avatar ewnd9 avatar hhzl avatar jamsinclair avatar vvscode avatar yakhinvadim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

anki-apkg-export's Issues

Update dependencies

Hey, @ewnd9!

How do you feel about updating dependencies for this project? I'd be glad to do it myself and make a PR if you're busy.

Is there a way to avoid importing duplicates when regenerating identical/updated cards?

I'm using anki-apkg-export to generate an .apkg-file from a google docs document. The idea is to regenerate the .apkg-file and re-import it whenever "cards" have been updated/added in the google docs-document. As it is now all cards, even those that are completely identical will be imported as duplicates (→ makes it hard to keep scheduling info etc). Is there a way to avoid the duplicate problem?

Back card side doesn't have front side repeated

Hi, it's me again :)
After last changes, everything works great, thanks! Apkg file is generated and successfully imported by Anki.
But cards' back side doesn't look as expected.

I looked into anki manual and found this: (https://apps.ankiweb.net/docs/manual.html#basic-templates)

The default back template will look something like this:
{{FrontSide}}
<hr id=answer>
{{Back}}

But cards, generated by anki-apkg-export, has this template:

{{Back}}

Deck templates could be changed in anki settings, but I don't think users should do it manually.
IMO, it would be more convenient for users, if generated decks had default templates. Here's an example of what it looks like:

What do you think?

Memory issue

I'm trying export deck with 841 cards and get

Cannot enlarge memory arrays. Either (1) compile with  -s TOTAL_MEMORY=X  with X higher than the current value 16777216, (2) compile with  -s ALLOW_MEMORY_GROWTH=1  which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with  -s ABORTING_MALLOC=0

Is there any way to work with deck in pipe-style? or the only solution - increase limit ?

Media support

Would be nice if we could add images and sounds files into deck cards.
If you support this feature, I can help to implement :-)

const fs = require('fs');
const AnkiExport = require('anki-apkg-export').default;

const apkg = new AnkiExport('deck-name');

apkg.addMedia('picture.jpg', fs.readFileSync('/tmp/picture.jpg'));

apkg.addCard('card #1 front <img src="picture.jpg"/ >', 'card #1 back');
apkg.addCard('card #2 front', 'card #2 back');

const zip = apkg.save();

fs.writeFileSync('./output.apkg', zip, 'binary');
console.log(`Package has been generated: output.pkg`);

http://decks.wikia.com/wiki/Anki_APKG_format_documentation

What will be implementation for this resolve error in webpack bundling?

I have done following steps to create bundle.js but getting following error.

1. Forked the GitHub

2. Change to directory

anki-apkg-export-master/examples/browser

3. npm install

4. npm run build
Getting error
ERROR in Cannot find module babel-core
Installed npm install babel-core

5. Again,
Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3"
Then npm install babel-loader@next

6. Then npm run build

ERROR in ./index.js
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions. In C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-preset-es2015\lib\index.js
    at createDescriptor (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-descriptors.js:178:11)
    at C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-descriptors.js:109:50
    at Array.map (<anonymous>)
    at createDescriptors (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-descriptors.js:109:29)
    at createPresetDescriptors (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-descriptors.js:101:10)
    at presets (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-descriptors.js:47:19)
    at mergeChainOpts (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-chain.js:384:26)
    at C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-chain.js:347:7
    at Generator.next (<anonymous>)
    at buildRootChain (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\config-chain.js:129:29)
    at buildRootChain.next (<anonymous>)
    at loadPrivatePartialConfig (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\partial.js:99:62)
    at loadPrivatePartialConfig.next (<anonymous>)
    at Function.<anonymous> (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\partial.js:125:25)
    at Generator.next (<anonymous>)
    at evaluateSync (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\gensync\index.js:244:28)
    at Function.sync (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\gensync\index.js:84:14)
    at Object.<anonymous> (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\node_modules\@babel\core\lib\config\index.js:43:61)
    at Object.<anonymous> (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:82:26)
    at Generator.next (<anonymous>)
    at step (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:3:221)
    at _next (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:3:409)
    at C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:3:477
    at new Promise (<anonymous>)
    at Object.<anonymous> (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:3:97)
    at Object._loader (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:148:18)
    at Object.loader (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:36:18)
    at Object.<anonymous> (C:\Users\manik\Desktop\anki-apkg-export-master\anki-apkg-export-master\examples\browser\node_modules\babel-loader\lib\index.js:31:12)

What will be implementation to resolve issues? Or have I done any mistake in installing?

Cannot find module 'script-loader!sql.js' from 'node_modules/anki-apkg-export/dist/index.js'

I get this error when I try to run the lib in Docker (image: lts-slim)

How to reproduce

  • Create a docker image with lts-slim
  • Try to require('anki-apkg-export').default
  • You'll get the error.

What I expected

  • The lib should be required with no issues.

I realized that the dist/index.js at line 23 tries to use script-loader if window is not undefined or the APP_ENV is browser. Neither should be true in my case as I'm running this on the server. I logged the value of window in Docker and for some reason its object and NOT undefined.

I'm not sure what is the correct solution here. Can someone help please?

Avoid using random / new Date() on generation

@ewnd9 What about removing random generation / new Date during work? It makes harder to compare values and give no pros. We can just have incremented counter for each deck

Then we'll be able to compare decks with pre-generated in binary. And we'll be able to use sorting by id on queries

P.S. just note - why export when class import data into Anki deck?

Can't pass APP_ENV to project, built with create-react-app

Hi!
Thank you for this project. I'd like to use it in my web app longman-to-anki to generate anki deck on client side. Unfortunately, my stack doesn't allow me to pass APP_ENV to my app.

I'm using create-react-app, where all build scripts are hidden, so I don't have access to webpack config.
Create-react-app allows to use environment variables, but only as long, as their name starts with REACT_APP_ (more about it here), so I can't create variable APP_ENV.

Is there any way environment could be inferred without passing environment variable?
Maybe, by checking if there is window object presented? If yes, I'd be glad to send a PR.
If no, do you see any way I can pass it to my project? Maybe I'm missing something.

Future direction of Project?

@ewnd9 I realise this library is an almost identical port of the existing Ruby library. It gets the essential use case of building anki decks correct, simple and easy. You've achieved that well, thanks for everyone's work 🏆

My questions are around future of this project.

  • Firstly I know its hard work, do you want expand the features and keep building the lib?
  • Or do you want keep this project simple and on parity with the original?

I ask this because I want to customise more aspects of the generated decks. Some features/ideas I have, and am happy to help with, are:

  • Exposing a notes api, e.g addNote
  • Decoupling addCard from creating both a note and a card
  • Exposing more customisation options
    • Most card fields can be customised
    • Most note fields can be customised
    • More customisation for the model, template fields etc.

If this not the direction you envision the library heading in, that's ok 😄. I can look into implementing these in another way 👌

Cannot enlarge memory arrays - Possible to modify sql.js file?

Hello,

I attempted to use anki-apkg-export to generate 861 cards, but am reaching the memory limit before they are all added.

The final apkg file is 87KB so I don't know why I am hitting the current ~18MB limit that sql.js has. Regardless, I am wondering if I would be able to either:

  • have anki-apkg-export use a custom sql.js that is compiled to have a TOTAL_MEMORY of 512MB

  • have anki-apkg-export use sql-memory-growth.js instead of sql.js without modifying the dist/index.js file directly

Enlarging the arrays adds overhead that doubles the execution time when compared to increasing the TOTAL_MEMORY, so I would prefer the first option.

I am new to nodejs so perhaps I am off-topic and this is possible with yarn scripts.

Thanks,
David

Import of cards with tags is incomplete

Test hhzl@8397a6d has an example of importing cards with tags.

The tags are imported and show up in the Anki browser when the whole collection is selected. (Anki version 2.0.38 on Linux)
screenshot from 2017-01-16 10 10 02.

However then selecting a tag in the browser no entries are shown
anki_export_tag_empty_in_browser_screenshot from 2017-01-16 10 10 37.

If create a new user in Anki 2.0.38 and import the CSV file the tags are fine.
anki_import_csv_notes_test_screenshot from 2017-01-16 10 15 13

anki_import_notes_with_tags_csv_screenshot from 2017-01-16 10 16 53

It might be an issue with space in the tags field.

The case which does not work

sqlite> select tags,flds,sfld from notes order by tags,sfld;
animal|cock�der Hahn|cock
animal|cow�die Kuh|cow
animal|dog�der Hund|dog
animal|goat�die Ziege|goat
animal|hen�das Henne|hen
animal|hyena�die Hyäne|hyena
animal|lion�der Löwe|lion
food|flour�das Mehl|flour
food|oil�das Öl|oil
food|rice�der Reis|rice
food|salt�das Salz|salt
food|water�das Wasser|water
food fruit|banana�die Banane|banana
food fruit|lemon�die Zitrone|lemon
food fruit|pear�die Birne|pear
food vegetable|carrot�die Karotte|carrot
food vegetable|onion�die Zwiebel|onion
food vegetable|tomato�die Tomate|tomato
nature|a river�ein Fluss|a river
nature|a tree�ein Baum|a tree
nature sky|one star�ein Stern|one star
nature sky|the moon�der Mond|the moon
nature sky|the sun�die Sonne|the sun
nature sky|three stars�drei Sterne|three stars
nature sky|two stars�zwei Sterne|two stars
sqlite> 

The case which works fine in the browser

sqlite> select tags,flds,sfld from notes order by tags,sfld;
 animal |cock�der Hahn|cock
 animal |cow�die Kuh|cow
 animal |dog�der Hund|dog
 animal |goat�die Ziege|goat
 animal |hen�das Henne|hen
 animal |hyena�die Hyäne|hyena
 animal |lion�der Löwe|lion
 food |flour�das Mehl|flour
 food |oil�das Öl|oil
 food |rice�der Reis|rice
 food |salt�das Salz|salt
 food |water�das Wasser|water
 food fruit |banana�die Banane|banana
 food fruit |lemon�die Zitrone|lemon
 food fruit |pear�die Birne|pear
 food vegetable |carrot�die Karotte|carrot
 food vegetable |onion�die Zwiebel|onion
 food vegetable |tomato�die Tomate|tomato
 nature |a river�ein Fluss|a river
 nature |a tree�ein Baum|a tree
 nature sky |one star�ein Stern|one star
 nature sky |the moon�der Mond|the moon
 nature sky |the sun�die Sonne|the sun
 nature sky |three stars�drei Sterne|three stars
 nature sky |two stars�zwei Sterne|two stars
sqlite> 

More tests needed to come up with a fix....

The next test will be to manually add a space after the tags field with an update statement and check if this solves the problem.

Add ability to add custom question & answer templates

When learning language, answer template would be more convenient if it would look something like {{Front}}<hr>{{Back}} instead of just showing the back card. Thus would be nice to have control over the question and answer templates.

Difficulties with installing anki-apkg-export

Hello and thanks for this nice library.

I am a beginner in node NPM, webpack etc..... I read your documentation but really struggle to understand how to install and run your library. I am really confused on how to install the different dependencies and how to do the call to all of these scripts from my webpage.

I simply want to reproduice on my website the sample application of https://ewnd9.com/anki-apkg-export-app/.

Could you please add a little bit more information about the required steps to install/configure the required tools and libraries?

It would help a lot, thanks!

Uncaught Error: Cannot find module "script!sql.js"

I tried to fix the issue #18 by checking if window object is present.
I added

 || typeof window !== 'undefined'

here: https://github.com/ewnd9/anki-apkg-export/blob/master/src/index.js#L8
and here: https://github.com/ewnd9/anki-apkg-export/blob/master/src/exporter.js#L56
(but in dist folder) and it worked. But I faced an error:
image
Unfortunately, I don't understand the cause of the error. script and raw loaders are installed. Maybe the reason is in webpack config somewhere, but can't edit it.

Do you have an idea what can cause this error and how I could fix it?

In case you'd like to try it yourself, I created a minimal representation of this case in this repo:
https://github.com/yakhinvadim/anki-apkg-export-error
This is app, created with create-react-app. My changes for this case are in this commit yakhinvadim/anki-apkg-export-error@4fec6c2

Does it support tags? other anki features?

Hi. Can you share info about full anki card format? It there any information about adding media into cards ?

I'm interesting how you see adding tags in existing api ? Special format for text files, or use directory name as tags ( like some/sub/dir1/file.txt => tags ['some', 'sub', 'dir1'] )

Is there way to create Anki deck in browser?

I have used browserify to make npm package available in browser.
index.js

var AnkiExporter = require('anki-apkg-export/dist/exporter');
global.window.AnkiExporter = AnkiExporter;

Then,

browserify index.js -o export.js

Using export.js with this example, give following errors.

<html>

<body>
    <script src="fs.js"></script>
    <script src="export.js"></script>

    <script>
        var apkg = new AnkiExport('deck-name');

        apkg.addCard('card #1 front', 'card #1 back');
        apkg.addCard('card #2 front', 'card #2 back', { tags: ['nice', 'better card'] });
        apkg.addCard('card #3 with image <img src="anki.png" />', 'card #3 back');

        apkg
            .save()
            .then(zip => {
                fs.writeFileSync('./output.apkg', zip, 'binary');
                console.log(`Package has been generated: output.pkg`);
            })
            .catch(err => console.log(err.stack || err));
    </script>
</body>

</html>

i

What I am missing here? Tried other approach also.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.