anki-reader
Version:
A library for reading Anki apkg and collection files.
66 lines (65 loc) • 1.65 kB
JavaScript
import { parseFieldsAndTemplate } from '../util/parser.js';
export class Question {
questionString;
answerString;
template;
model;
question;
answer;
constructor(fields, template, model) {
const { question, answer } = parseFieldsAndTemplate(fields, template);
this.questionString = question;
this.answerString = answer;
this.template = template;
this.model = model;
}
buildFormattedString(rawValue, formattedString) {
return {
rawValue,
formattedString,
latexFormattedString: this.getLatexPre() + formattedString + this.getLatexPost()
};
}
getLatexPre() {
return this.model.getLatexPre();
}
getLatexPost() {
return this.model.getLatexPost();
}
getCss() {
return this.model.getCss();
}
getQuestion() {
if (this.question != null) {
return {
...this.question
};
}
this.question = this.buildFormattedString(this.template.qfmt, this.questionString);
return {
...this.question
};
}
getAnswer() {
if (this.answer != null) {
return {
...this.answer
};
}
this.answer = this.buildFormattedString(this.template.afmt, this.answerString);
return {
...this.answer
};
}
getQuestionString() {
return this.questionString;
}
getAnswerString() {
return this.answerString;
}
getRawTemplate() {
return {
...this.template
};
}
}