genanki-js 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/License.md +22 -22
- package/README.md +179 -89
- package/dist/genanki.cjs +1277 -0
- package/dist/genanki.js +1270 -536
- package/dist/genanki.mjs +1277 -0
- package/genanki/anki_hash.js +31 -28
- package/genanki/apkg_col.js +107 -0
- package/genanki/apkg_schema.js +83 -83
- package/genanki/deck.js +12 -12
- package/genanki/default.js +135 -135
- package/genanki/license.js +15 -15
- package/genanki/model.js +61 -58
- package/genanki/note.js +62 -59
- package/genanki/package.js +239 -134
- package/genanki/sha256.js +518 -0
- package/package.json +55 -36
- package/Changelog.md +0 -13
- package/dist/genanki-min.js +0 -1
- package/genanki/import.js +0 -4
- package/gulpfile.js +0 -28
package/genanki/model.js
CHANGED
|
@@ -1,58 +1,61 @@
|
|
|
1
|
-
const MODEL_STD = 0
|
|
2
|
-
const MODEL_CLOZE = 1
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(props) {
|
|
6
|
-
this.props = {
|
|
7
|
-
...defaultModel,
|
|
8
|
-
...props,
|
|
9
|
-
flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
|
|
10
|
-
tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
|
|
11
|
-
mod: new Date().getTime()
|
|
12
|
-
}
|
|
13
|
-
this.fieldNameToOrd = {}
|
|
14
|
-
this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
note(fields, tags, guid = null) {
|
|
18
|
-
if (Array.isArray(fields)) {
|
|
19
|
-
if (fields.length !== this.props.flds.length) {
|
|
20
|
-
throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
|
|
21
|
-
}
|
|
22
|
-
return new Note(this, fields, tags, guid)
|
|
23
|
-
} else {
|
|
24
|
-
const field_names = Object.keys(fields)
|
|
25
|
-
const fields_list = []
|
|
26
|
-
field_names.forEach(field_name => {
|
|
27
|
-
const ord = this.fieldNameToOrd[field_name]
|
|
28
|
-
if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
|
|
29
|
-
fields_list[ord] = fields[field_name]
|
|
30
|
-
})
|
|
31
|
-
return new Note(this, fields_list, tags, guid)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
constructor(props) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
const MODEL_STD = 0
|
|
2
|
+
const MODEL_CLOZE = 1
|
|
3
|
+
|
|
4
|
+
class Model {
|
|
5
|
+
constructor(props) {
|
|
6
|
+
this.props = {
|
|
7
|
+
...defaultModel,
|
|
8
|
+
...props,
|
|
9
|
+
flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
|
|
10
|
+
tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
|
|
11
|
+
mod: new Date().getTime()
|
|
12
|
+
}
|
|
13
|
+
this.fieldNameToOrd = {}
|
|
14
|
+
this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
note(fields, tags, guid = null) {
|
|
18
|
+
if (Array.isArray(fields)) {
|
|
19
|
+
if (fields.length !== this.props.flds.length) {
|
|
20
|
+
throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
|
|
21
|
+
}
|
|
22
|
+
return new Note(this, fields, tags, guid)
|
|
23
|
+
} else {
|
|
24
|
+
const field_names = Object.keys(fields)
|
|
25
|
+
const fields_list = []
|
|
26
|
+
field_names.forEach(field_name => {
|
|
27
|
+
const ord = this.fieldNameToOrd[field_name]
|
|
28
|
+
if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
|
|
29
|
+
fields_list[ord] = fields[field_name]
|
|
30
|
+
})
|
|
31
|
+
return new Note(this, fields_list, tags, guid)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class ClozeModel extends Model {
|
|
37
|
+
constructor(props) {
|
|
38
|
+
// `tmpl` is only used to build the single cloze template,
|
|
39
|
+
// it must not end up in the note type json
|
|
40
|
+
const { tmpl, ...rest } = props
|
|
41
|
+
super({
|
|
42
|
+
type: MODEL_CLOZE,
|
|
43
|
+
css: `
|
|
44
|
+
.card {
|
|
45
|
+
font-family: arial;
|
|
46
|
+
font-size: 20px;
|
|
47
|
+
text-align: center;
|
|
48
|
+
color: black;
|
|
49
|
+
background-color: white;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.cloze {
|
|
53
|
+
font-weight: bold;
|
|
54
|
+
color: blue;
|
|
55
|
+
}
|
|
56
|
+
`,
|
|
57
|
+
tmpls: [{ name: "Cloze", ...tmpl }],
|
|
58
|
+
...rest
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
package/genanki/note.js
CHANGED
|
@@ -1,59 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
constructor(model, fields, tags = null, guid = null) {
|
|
3
|
-
this.model = model
|
|
4
|
-
this.fields = fields
|
|
5
|
-
this.tags = tags
|
|
6
|
-
this._guid = guid
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
get guid() {
|
|
10
|
-
return this._guid ? this._guid : ankiHash(this.fields);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
get cards() {
|
|
14
|
-
if (this.model.props.type === MODEL_STD) {
|
|
15
|
-
const isEmpty = f => {
|
|
16
|
-
return !f || f.toString().trim().length === 0
|
|
17
|
-
}
|
|
18
|
-
const rv = []
|
|
19
|
-
for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
|
|
20
|
-
const op = any_or_all === "any" ? "some" : "every"
|
|
21
|
-
if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
|
|
22
|
-
rv.push(card_ord)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return rv
|
|
26
|
-
} else {
|
|
27
|
-
// the below logic is copied from anki's ModelManager._availClozeOrds
|
|
28
|
-
const ords = new Set()
|
|
29
|
-
const matches = []
|
|
30
|
-
const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
|
|
31
|
-
const percentRe = /<%cloze:(.+?)%>/g
|
|
32
|
-
const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
|
|
33
|
-
let m;
|
|
34
|
-
while (m = curliesRe.exec(qfmt))
|
|
35
|
-
matches.push(m[1])
|
|
36
|
-
while (m = percentRe.exec(qfmt))
|
|
37
|
-
matches.push(m[1])
|
|
38
|
-
const map = {}
|
|
39
|
-
this.model.props.flds.forEach((fld, i) => {
|
|
40
|
-
map[fld.name] = [i, fld]
|
|
41
|
-
})
|
|
42
|
-
for (const fname of matches) {
|
|
43
|
-
if (!(fname in map)) continue
|
|
44
|
-
const ord = map[fname][0]
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
1
|
+
class Note {
|
|
2
|
+
constructor(model, fields, tags = null, guid = null) {
|
|
3
|
+
this.model = model
|
|
4
|
+
this.fields = fields
|
|
5
|
+
this.tags = tags
|
|
6
|
+
this._guid = guid
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get guid() {
|
|
10
|
+
return this._guid ? this._guid : ankiHash(this.fields);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get cards() {
|
|
14
|
+
if (this.model.props.type === MODEL_STD) {
|
|
15
|
+
const isEmpty = f => {
|
|
16
|
+
return !f || f.toString().trim().length === 0
|
|
17
|
+
}
|
|
18
|
+
const rv = []
|
|
19
|
+
for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
|
|
20
|
+
const op = any_or_all === "any" ? "some" : "every"
|
|
21
|
+
if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
|
|
22
|
+
rv.push(card_ord)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return rv
|
|
26
|
+
} else {
|
|
27
|
+
// the below logic is copied from anki's ModelManager._availClozeOrds
|
|
28
|
+
const ords = new Set()
|
|
29
|
+
const matches = []
|
|
30
|
+
const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
|
|
31
|
+
const percentRe = /<%cloze:(.+?)%>/g
|
|
32
|
+
const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
|
|
33
|
+
let m;
|
|
34
|
+
while (m = curliesRe.exec(qfmt))
|
|
35
|
+
matches.push(m[1])
|
|
36
|
+
while (m = percentRe.exec(qfmt))
|
|
37
|
+
matches.push(m[1])
|
|
38
|
+
const map = {}
|
|
39
|
+
this.model.props.flds.forEach((fld, i) => {
|
|
40
|
+
map[fld.name] = [i, fld]
|
|
41
|
+
})
|
|
42
|
+
for (const fname of matches) {
|
|
43
|
+
if (!(fname in map)) continue
|
|
44
|
+
const ord = map[fname][0]
|
|
45
|
+
// matches {{c1::text}} and {{c1,2::text}} (one deletion shown on several cards)
|
|
46
|
+
const re = /{{c(\d+(?:,\d+)*)::[\s\S]+?}}/g
|
|
47
|
+
while (m = re.exec(this.fields[ord])) {
|
|
48
|
+
for (const n of m[1].split(',')) {
|
|
49
|
+
const i = parseInt(n)
|
|
50
|
+
if (i > 0)
|
|
51
|
+
ords.add(i - 1)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (ords.size === 0) {
|
|
56
|
+
// empty clozes use first ord
|
|
57
|
+
return [0]
|
|
58
|
+
}
|
|
59
|
+
return Array.from(ords)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
package/genanki/package.js
CHANGED
|
@@ -1,134 +1,239 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
1
|
+
// Dependencies used to create the package.
|
|
2
|
+
// When genanki.js is loaded with a <script> tag, the global `SQL` (or `initSqlJs`),
|
|
3
|
+
// `JSZip` and `saveAs` (FileSaver.js, optional) are used.
|
|
4
|
+
// The npm builds (ES module / CommonJS) set these from the installed packages.
|
|
5
|
+
const genankiDeps = {
|
|
6
|
+
SQL: null, // initialized sql.js, see setSqlJs()
|
|
7
|
+
sqlPromise: null, // Promise<SQL> while sql.js is being loaded
|
|
8
|
+
loadSqlJs: null, // () => Promise<SQL>
|
|
9
|
+
JSZip: null,
|
|
10
|
+
saveAs: null,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Use an initialized sql.js instance for all packages.
|
|
15
|
+
* `SQL` is the value `initSqlJs()` resolves to.
|
|
16
|
+
*/
|
|
17
|
+
function setSqlJs(SQL) {
|
|
18
|
+
genankiDeps.SQL = SQL
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function resolveSqlJs(packageSQL) {
|
|
22
|
+
if (packageSQL) return Promise.resolve(packageSQL)
|
|
23
|
+
if (genankiDeps.SQL) return Promise.resolve(genankiDeps.SQL)
|
|
24
|
+
if (typeof SQL !== "undefined" && SQL) return Promise.resolve(SQL)
|
|
25
|
+
|
|
26
|
+
if (!genankiDeps.sqlPromise) {
|
|
27
|
+
let load = genankiDeps.loadSqlJs
|
|
28
|
+
if (!load && typeof initSqlJs === "function") {
|
|
29
|
+
load = () => initSqlJs()
|
|
30
|
+
}
|
|
31
|
+
if (!load) {
|
|
32
|
+
return Promise.reject(new Error("sql.js is not loaded. Add sql.js to the page and assign the result " +
|
|
33
|
+
"of initSqlJs() to a global `SQL` variable, or pass it with setSqlJs(SQL)."))
|
|
34
|
+
}
|
|
35
|
+
genankiDeps.sqlPromise = load().then(sql => {
|
|
36
|
+
genankiDeps.SQL = sql
|
|
37
|
+
return sql
|
|
38
|
+
}, err => {
|
|
39
|
+
genankiDeps.sqlPromise = null
|
|
40
|
+
throw err
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
return genankiDeps.sqlPromise
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveJSZip() {
|
|
47
|
+
if (genankiDeps.JSZip) return genankiDeps.JSZip
|
|
48
|
+
if (typeof JSZip !== "undefined" && JSZip) return JSZip
|
|
49
|
+
throw new Error("JSZip is not loaded. Add jszip.min.js to the page.")
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function saveFile(content, filename) {
|
|
53
|
+
const saver = genankiDeps.saveAs || (typeof saveAs === "function" ? saveAs : null)
|
|
54
|
+
if (saver) {
|
|
55
|
+
saver(content, filename)
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// FileSaver.js is optional, download with a temporary link instead
|
|
60
|
+
if (typeof document !== "undefined" && typeof URL !== "undefined" && URL.createObjectURL) {
|
|
61
|
+
const url = URL.createObjectURL(content)
|
|
62
|
+
const a = document.createElement("a")
|
|
63
|
+
a.href = url
|
|
64
|
+
a.download = filename
|
|
65
|
+
a.style.display = "none"
|
|
66
|
+
document.body.appendChild(a)
|
|
67
|
+
a.click()
|
|
68
|
+
document.body.removeChild(a)
|
|
69
|
+
setTimeout(() => URL.revokeObjectURL(url), 40000)
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw new Error("writeToFile() can only download files in a browser. " +
|
|
74
|
+
"Use package.generate('nodebuffer') or package.generate('uint8array') and write the result to a file.")
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class Package {
|
|
78
|
+
constructor() {
|
|
79
|
+
this.decks = []
|
|
80
|
+
this.media = []
|
|
81
|
+
this.SQL = null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Use an initialized sql.js instance for this package.
|
|
86
|
+
*/
|
|
87
|
+
setSqlJs(SQL) {
|
|
88
|
+
this.SQL = SQL
|
|
89
|
+
return this
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
addDeck(deck) {
|
|
93
|
+
// adding the same deck twice would duplicate all of its notes
|
|
94
|
+
if (!this.decks.includes(deck)) {
|
|
95
|
+
this.decks.push(deck)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
addMedia(data, name) {
|
|
100
|
+
this.media.push({ name, data })
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
addMediaFile(filename, name = null) {
|
|
104
|
+
this.media.push({ name: name || filename, filename })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Create the .apkg file content.
|
|
109
|
+
* `type` is any JSZip output type: "blob" (default), "uint8array", "arraybuffer", "base64",
|
|
110
|
+
* or "nodebuffer" (Node.js only).
|
|
111
|
+
* Returns a Promise.
|
|
112
|
+
*/
|
|
113
|
+
generate(type = "blob") {
|
|
114
|
+
return resolveSqlJs(this.SQL).then(SQL => {
|
|
115
|
+
const ZipClass = resolveJSZip()
|
|
116
|
+
|
|
117
|
+
const db = new SQL.Database()
|
|
118
|
+
let data
|
|
119
|
+
try {
|
|
120
|
+
db.run(APKG_SCHEMA)
|
|
121
|
+
this.write(db)
|
|
122
|
+
data = db.export()
|
|
123
|
+
} finally {
|
|
124
|
+
db.close()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const zip = new ZipClass()
|
|
128
|
+
zip.file("collection.anki2", data)
|
|
129
|
+
|
|
130
|
+
const media_info = {}
|
|
131
|
+
|
|
132
|
+
this.media.forEach((m, i) => {
|
|
133
|
+
if (m.filename != null) {
|
|
134
|
+
zip.file(i.toString(), m.filename)
|
|
135
|
+
} else {
|
|
136
|
+
zip.file(i.toString(), m.data)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
media_info[i] = m.name
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
zip.file('media', JSON.stringify(media_info))
|
|
143
|
+
|
|
144
|
+
return zip.generateAsync({ type, mimeType: "application/apkg" })
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Download the package as `filename` (browser only).
|
|
150
|
+
* Returns a Promise that resolves with the generated Blob.
|
|
151
|
+
*/
|
|
152
|
+
writeToFile(filename) {
|
|
153
|
+
return this.generate("blob").then(content => {
|
|
154
|
+
saveFile(content, filename)
|
|
155
|
+
return content
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
write(db) {
|
|
161
|
+
const now = new Date
|
|
162
|
+
const models = {}
|
|
163
|
+
const decks = {}
|
|
164
|
+
|
|
165
|
+
// AnkiDroid failed to import subdeck, So add a Default deck
|
|
166
|
+
decks["1"] = {...defaultDeck, id: 1, name: "Default"}
|
|
167
|
+
|
|
168
|
+
this.decks.forEach(d => {
|
|
169
|
+
d.notes.forEach(n => models[n.model.props.id] = n.model.props)
|
|
170
|
+
decks[d.id] = {
|
|
171
|
+
...defaultDeck,
|
|
172
|
+
id: d.id,
|
|
173
|
+
name: d.name,
|
|
174
|
+
desc: d.desc,
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
const col = [
|
|
179
|
+
null, // id
|
|
180
|
+
(+now / 1000) | 0, // crt
|
|
181
|
+
+now, // mod
|
|
182
|
+
+now, // scm
|
|
183
|
+
11, // ver
|
|
184
|
+
0, // dty
|
|
185
|
+
0, // usn
|
|
186
|
+
0, // ls
|
|
187
|
+
JSON.stringify(defaultConf), // conf
|
|
188
|
+
JSON.stringify(models), // models
|
|
189
|
+
JSON.stringify(decks), // decks
|
|
190
|
+
JSON.stringify({ 1: { id: 1, ...defaultDeckConf } }), // dconf
|
|
191
|
+
JSON.stringify({}), // tags
|
|
192
|
+
]
|
|
193
|
+
|
|
194
|
+
db.prepare(`INSERT INTO col
|
|
195
|
+
(id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
|
|
196
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(col)
|
|
197
|
+
|
|
198
|
+
const insert_notes = db.prepare(`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
|
|
199
|
+
VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`)
|
|
200
|
+
|
|
201
|
+
const insert_cards = db.prepare(`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
|
|
202
|
+
VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')`)
|
|
203
|
+
|
|
204
|
+
for (const deck of this.decks) {
|
|
205
|
+
for (const note of deck.notes) {
|
|
206
|
+
var tags = note.tags == null ? '' : note.tags.join(' ')
|
|
207
|
+
insert_notes.run(
|
|
208
|
+
[
|
|
209
|
+
note.guid, // guid
|
|
210
|
+
note.model.props.id, // mid
|
|
211
|
+
(+now / 1000) | 0, // mod
|
|
212
|
+
-1, // usn
|
|
213
|
+
tags, // tags
|
|
214
|
+
note.fields.join('\x1f'), // flds
|
|
215
|
+
0, // sfld
|
|
216
|
+
])
|
|
217
|
+
|
|
218
|
+
var rowID = db.exec("select last_insert_rowid();")
|
|
219
|
+
var note_id = rowID[0]['values'][0][0];
|
|
220
|
+
|
|
221
|
+
for (const card_ord of note.cards) {
|
|
222
|
+
insert_cards.run(
|
|
223
|
+
[
|
|
224
|
+
note_id, // nid
|
|
225
|
+
deck.id, // did
|
|
226
|
+
card_ord, // ord
|
|
227
|
+
(+now / 1000) | 0, // mod
|
|
228
|
+
-1, // usn
|
|
229
|
+
0, // type 0=new, 1=learning, 2=due
|
|
230
|
+
0, // queue -1 for suspended
|
|
231
|
+
])
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
insert_notes.free()
|
|
237
|
+
insert_cards.free()
|
|
238
|
+
}
|
|
239
|
+
}
|