Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags support + return Promise in Package#writeToFile #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class Model {
this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
}

note(fields, guid = null) {
note(fields, guid = null, tags = '') {
if (Array.isArray(fields)) {
if (fields.length !== this.props.flds.length) {
throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
}
return new Note(this, fields, guid)
return new Note(this, fields, guid, tags)
} else {
const field_names = Object.keys(fields)
const fields_list = []
Expand All @@ -50,7 +50,7 @@ class Model {
if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
fields_list[ord] = fields[field_name]
})
return new Note(this, fields_list, guid)
return new Note(this, fields_list, guid, tags)
}
}
}
Expand Down Expand Up @@ -145,10 +145,11 @@ class Deck {


class Note {
constructor(model, fields, guid = null) {
constructor(model, fields, guid = null, tags = '') {
this.model = model
this.fields = fields
this._guid = guid
this.tags = tags
}

get guid() {
Expand Down Expand Up @@ -187,7 +188,7 @@ class Note {
for (const fname of matches) {
if (!(fname in map)) continue
const ord = map[fname][0]
const re = /{{c(\d+)::.+?}}/gs
const re = /\{\{c(\d+)::.+?\}\}/gs
while (m = re.exec(this.fields[ord])) {
const i = parseInt(m[1])
if (i > 0)
Expand Down Expand Up @@ -321,7 +322,7 @@ class Package {
media_info[i] = m.name
})
archive.append(JSON.stringify(media_info), { name: 'media' })
archive.finalize()
return archive.finalize()
}

write(db) {
Expand Down Expand Up @@ -373,7 +374,7 @@ class Package {
mid: note.model.props.id,
mod: (+now/1000)|0,
usn: -1,
tags: '',
tags: note.tags,
flds: note.fields.join('\x1f'),
sfld: 0,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mkanki",
"version": "1.1.0",
"version": "2.0.0",
"description": "Generate Anki decks. Supports cloze deletion and media.",
"main": "index.js",
"scripts": {},
Expand Down