Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ 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.addCard(['card #1 front', 'card #1 back']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can do the same without breaking an interface?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinsilvestre just take list of params and check last for object type

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()
Expand Down Expand Up @@ -83,9 +83,9 @@ const apkg = new AnkiExport('deck-name');
// 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.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()
Expand Down
4 changes: 2 additions & 2 deletions examples/browser-media-ajax/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fetch('https://raw.githubusercontent.com/ewnd9/anki-apkg-export/39ebdd664ab23b52
})
.then(function(myBlob) {
apkg.addMedia('anki.png', myBlob);
apkg.addCard('card #1 with image <img src="anki.png" />', 'card #1 back');
apkg.addCard(['card #1 with image <img src="anki.png" />', 'card #1 back']);

return apkg.save()
return apkg.save();
})
.then(function(zip) {
saveAs(zip, 'output.apkg');
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-media-file-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ input.onchange = function(e) {
const file = e.target.result;

apkg.addMedia('anki.png', file);
apkg.addCard('card #1 with image <img src="anki.png" />', 'card #1 back');
apkg.addCard(['card #1 with image <img src="anki.png" />', 'card #1 back']);

apkg
.save()
Expand Down
4 changes: 2 additions & 2 deletions examples/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import AnkiExport from '../../src';

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

apkg.addCard('card #1 front', 'card #1 back');
apkg.addCard('card #2 front', 'card #2 back');
apkg.addCard(['card #1 front', 'card #1 back']);
apkg.addCard(['card #2 front', 'card #2 back']);

apkg
.save()
Expand Down
6 changes: 3 additions & 3 deletions examples/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const apkg = new AnkiExport('deck-name-node');

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

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

apkg
.save()
Expand Down
27 changes: 11 additions & 16 deletions src/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,24 @@ export default class {

media.forEach((item, i) => zip.file(i, item.data));

if (process.env.APP_ENV === 'browser' || typeof window !== 'undefined') {
return zip.generateAsync(Object.assign({}, { type: 'blob' }, options));
} else {
return zip.generateAsync(
Object.assign(
{},
{
const baseOptions =
process.env.APP_ENV === 'browser' || typeof window !== 'undefined'
? { type: 'blob' }
: {
type: 'nodebuffer',
base64: false,
compression: 'DEFLATE'
},
options
)
);
}
};
return zip.generateAsync(Object.assign(baseOptions, options));
}

addMedia(filename, data) {
this.media.push({ filename, data });
}

addCard(front, back, { tags } = {}) {
addCard(fields, { tags, sortField } = {}) {
const { topDeckId, topModelId, separator } = this;
const joinedFields = fields.join(separator);
const now = Date.now();
const note_id = this._getId('notes', 'id', now);

Expand All @@ -86,9 +81,9 @@ export default class {
':mod': this._getId('notes', 'mod', now), // integer not null,
':usn': -1, // integer not null,
':tags': strTags, // text not null,
':flds': front + separator + back, // text not null,
':sfld': front, // integer not null,
':csum': this._checksum(front + separator + back), //integer not null,
':flds': joinedFields, // text not null,
':sfld': sortField || fields[0], // integer not null,
':csum': this._checksum(joinedFields), //integer not null,
':flags': 0, // integer not null,
':data': '' // text not null,
});
Expand Down
32 changes: 12 additions & 20 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const createField = (name, i) => ({
name,
media: [],
sticky: false,
rtl: false,
ord: i,
font: 'Arial',
size: 20
});

export default function createTemplate(
{
fields = ['Front', 'Back'],
questionFormat = '{{Front}}',
answerFormat = '{{FrontSide}}\n\n<hr id="answer">\n\n{{Back}}',
css = '.card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\nbackground-color: white;\n}\n'
Expand Down Expand Up @@ -29,26 +40,7 @@ export default function createTemplate(
did: 1435588830424,
usn: -1,
req: [[0, 'all', [0]]],
flds: [
{
name: 'Front',
media: [],
sticky: false,
rtl: false,
ord: 0,
font: 'Arial',
size: 20
},
{
name: 'Back',
media: [],
sticky: false,
rtl: false,
ord: 1,
font: 'Arial',
size: 20
}
],
flds: fields.map(createField),
sortf: 0,
latexPre:
'\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n',
Expand Down
2 changes: 1 addition & 1 deletion test/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import Zip from 'jszip';
import mkdirp from 'mkdirp';

export const addCards = (apkg, list) => list.forEach(({ front, back }) => apkg.addCard(front, back));
export const addCards = (apkg, list) => list.forEach(({ front, back }) => apkg.addCard([front, back]));

export const unzipDeckToDir = (pathToDeck, pathToUnzipTo) => {
mkdirp.sync(pathToUnzipTo);
Expand Down
8 changes: 4 additions & 4 deletions test/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('Exporter.addCard', t => {
const [front, back] = ['Test Front', 'Test back'];
const exporterUpdateSpy = sinon.spy(exporter, '_update');

exporter.addCard(front, back);
exporter.addCard([front, back]);

t.is(exporterUpdateSpy.callCount, 2, 'should made two requests');

Expand Down Expand Up @@ -87,7 +87,7 @@ test('Exporter.addCard with options (tags is array)', t => {
const tags = ['tag1', 'tag2', 'multiple words tag'];
const exporterUpdateSpy = sinon.spy(exporter, '_update');

exporter.addCard(front, back, { tags });
exporter.addCard([front, back], { tags });

t.is(exporterUpdateSpy.callCount, 2, 'should made two requests');

Expand All @@ -110,7 +110,7 @@ test('Exporter.addCard with options (tags is string)', t => {
const [front, back, tags] = ['Test Front', 'Test back', 'Some string with_delimiters'];
const exporterUpdateSpy = sinon.spy(exporter, '_update');

exporter.addCard(front, back, { tags });
exporter.addCard([front, back], { tags });

t.is(exporterUpdateSpy.callCount, 2, 'should made two requests');

Expand Down Expand Up @@ -138,7 +138,7 @@ test('Exporter._getId', async t => {
const numberOfCards = 5;
const [front, back] = ['Test Front', 'Test back'];
for (let i = 0; i < numberOfCards; i++) {
exporter.addCard(front, back);
exporter.addCard([front, back]);
}

const noteIdsResult = exporter.db.exec('SELECT id from notes');
Expand Down
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ test('equals to sample', async t => {

apkg.addMedia('anki.png', fs.readFileSync(__dirname + '/fixtures/anki.png'));

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

const zip = await apkg.save();
fs.writeFileSync(dest, zip, 'binary');
Expand Down Expand Up @@ -92,9 +92,9 @@ test('check internal structure on adding card with tags', async t => {
const [front1, back1, tags1] = ['Card front side 1', 'Card back side 1', ['some', 'tag', 'tags with multiple words']];
const [front2, back2, tags2] = ['Card front side 2', 'Card back side 2', 'some strin_tags'];
const [front3, back3] = ['Card front side 3', 'Card back side 3'];
apkg.addCard(front1, back1, { tags: tags1 });
apkg.addCard(front2, back2, { tags: tags2 });
apkg.addCard(front3, back3);
apkg.addCard([front1, back1], { tags: tags1 });
apkg.addCard([front2, back2], { tags: tags2 });
apkg.addCard([front3, back3]);

const zip = await apkg.save();
fs.writeFileSync(decFile, zip, 'binary');
Expand Down