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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"arraybuffer-equal": "1.0.4",
"ava": "^2.4.0",
"ava": "^6.2.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
Expand All @@ -27,7 +27,7 @@
"prettier": "^1.19.1",
"proxyquire": "^2.1.3",
"sinon": "^8.0.2",
"sqlite3": "4.1.1"
"sqlite3": "^5.1.7"
},
"author": "ewnd9 <ewndnine@gmail.com>",
"keywords": [
Expand Down
30 changes: 23 additions & 7 deletions src/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ export default class {
this.media.push({ filename, data });
}

addCard(front, back, { tags } = {}) {
addCard(...fields) {
const options = typeof fields[fields.length - 1] === 'object' ? fields.pop() : undefined;
const { sortField, tags } = options || {};
const { topDeckId, topModelId, separator } = this;
const now = Date.now();
const note_guid = this._getNoteGuid(topDeckId, front, back);

const joinedFields = fields.join(separator);
const note_guid = this._getNoteGuid(topDeckId, joinedFields);
const note_id = this._getNoteId(note_guid, now);

let strTags = '';
Expand All @@ -80,16 +84,28 @@ export default class {
strTags = this._tagsToStr(tags);
}

let intSortField = 0;
if (typeof sortField === 'number') {
if (Number.isSafeInteger(sortField)) {
intSortField = Math.max(Math.min(sortField, fields.length - 1), 0);
}
} else if (typeof sortField === 'string') {
const index = fields.indexOf(sortField);
if (index !== -1) {
intSortField = index;
}
}
Comment on lines +92 to +97
Copy link
Author

@AldoMX AldoMX Dec 7, 2024

Choose a reason for hiding this comment

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

The else if (typeof sortField === 'string') {} branch was added to preserve the behavior from #31, I can remove it if requested.


this._update('insert or replace into notes values(:id,:guid,:mid,:mod,:usn,:tags,:flds,:sfld,:csum,:flags,:data)', {
':id': note_id, // integer primary key,
':guid': note_guid, // text not null,
':mid': topModelId, // integer not null,
':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': intSortField, // integer not null,
':csum': this._checksum(joinedFields), //integer not null,
':flags': 0, // integer not null,
':data': '' // text not null,
});
Expand Down Expand Up @@ -154,8 +170,8 @@ export default class {
return rowObj.id || this._getId('notes', 'id', ts);
}

_getNoteGuid(topDeckId, front, back) {
return sha1(`${topDeckId}${front}${back}`);
_getNoteGuid(topDeckId, joinedFields) {
return sha1(`${topDeckId}${this.separator}${joinedFields}`);
}

_getCardId(note_id, ts) {
Expand Down
30 changes: 10 additions & 20 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 @@ -27,26 +28,15 @@ 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((name, i) => ({
name,
media: [],
sticky: false,
rtl: false,
ord: i,
font: 'Arial',
size: 20
})),
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
8 changes: 4 additions & 4 deletions test/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test('Exporter.addCard', t => {
'insert or replace into notes values(:id,:guid,:mid,:mod,:usn,:tags,:flds,:sfld,:csum,:flags,:data)'
);
const notesUpdate = exporterUpdateSpy.args[0][1];
t.is(notesUpdate[':sfld'], front);
t.is(notesUpdate[':sfld'], 0);
t.is(notesUpdate[':flds'], front + separator + back);
t.is(notesUpdate[':mid'], topModelId);

Expand Down Expand Up @@ -93,7 +93,7 @@ test('Exporter.addCard with options (tags is array)', t => {
);
const notesUpdate = exporterUpdateSpy.args[0][1];
const notesTags = notesUpdate[':tags'].split(' ');
t.is(notesUpdate[':sfld'], front);
t.is(notesUpdate[':sfld'], 0);
t.is(notesUpdate[':flds'], front + separator + back);
t.is(notesUpdate[':mid'], topModelId);

Expand All @@ -115,7 +115,7 @@ test('Exporter.addCard with options (tags is string)', t => {
'insert or replace into notes values(:id,:guid,:mid,:mod,:usn,:tags,:flds,:sfld,:csum,:flags,:data)'
);
const notesUpdate = exporterUpdateSpy.args[0][1];
t.is(notesUpdate[':sfld'], front);
t.is(notesUpdate[':sfld'], 0);
t.is(notesUpdate[':flds'], front + separator + back);
t.is(notesUpdate[':mid'], topModelId);
t.is(notesUpdate[':tags'], tags);
Expand Down Expand Up @@ -149,7 +149,7 @@ test('Exporter.addCard updates note if it is a duplicate', t => {
const secondNotesUpdate = exporterUpdateSpy.args[2][1];
t.is(notesUpdate[':id'], secondNotesUpdate[':id']);
t.is(notesUpdate[':guid'], secondNotesUpdate[':guid']);
t.is(notesUpdate[':sfld'], front);
t.is(notesUpdate[':sfld'], 0);
t.is(notesUpdate[':flds'], front + separator + back);
t.is(notesUpdate[':mid'], topModelId);

Expand Down
Binary file modified test/fixtures/output.apkg
Binary file not shown.
30 changes: 15 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ test('check internal structure', async t => {
const db = new sqlite3.Database(destUnpackedDb);
const result = await pify(db.all.bind(db))(
`SELECT
notes.sfld as front,
notes.flds as back
notes.sfld as sortField,
notes.flds as fields
from cards JOIN notes where cards.nid = notes.id ORDER BY cards.id`
);
db.close();

// compare content from just created db with original list of cards
const normilizedResult = sortBy(
result.map(({ front, back }) => ({
front,
back: back.split(SEPARATOR).pop()
})),
'front'
);
result.map(({ fields, sortField }) => {
fields = fields.split(SEPARATOR);
return { front: fields[0], back: fields[1], sortField: fields[sortField] };
}),
'sortField'
).map(({ front, back }) => ({ front, back }));

t.deepEqual(normilizedResult, cards);
});
Expand All @@ -99,20 +99,20 @@ test('check internal structure on adding card with tags', async t => {
const db = new sqlite3.Database(`${unzipedDeck}/collection.anki2`);
const results = await pify(db.all.bind(db))(
`SELECT
notes.sfld as front,
notes.flds as back,
notes.sfld as sortField,
notes.flds as fields,
notes.tags as tags
from cards JOIN notes where cards.nid = notes.id ORDER BY front`
from cards JOIN notes where cards.nid = notes.id ORDER BY fields`
);
db.close();

t.deepEqual(results, [
{
front: front1,
back: `${front1}${SEPARATOR}${back1}`,
sortField: 0,
fields: `${front1}${SEPARATOR}${back1}`,
tags: ' ' + tags1.map(tag => tag.replace(/ /g, '_')).join(' ') + ' '
},
{ front: front2, back: `${front2}${SEPARATOR}${back2}`, tags: tags2 },
{ front: front3, back: `${front3}${SEPARATOR}${back3}`, tags: '' }
{ sortField: 0, fields: `${front2}${SEPARATOR}${back2}`, tags: tags2 },
{ sortField: 0, fields: `${front3}${SEPARATOR}${back3}`, tags: '' }
]);
});
Loading