Refactor dictNoteFormat

This commit is contained in:
toasted-nutbread 2020-02-25 20:38:18 -05:00
parent 3795af6cb5
commit 2e0b3a6ccc

View File

@ -388,41 +388,40 @@ dictFieldFormat.markers = new Set([
]); ]);
async function dictNoteFormat(definition, mode, options, templates) { async function dictNoteFormat(definition, mode, options, templates) {
const note = {fields: {}, tags: options.anki.tags}; const isKanji = (mode === 'kanji');
let fields = []; const tags = options.anki.tags;
const modeOptions = isKanji ? options.anki.kanji : options.anki.terms;
const modeOptionsFieldEntries = Object.entries(modeOptions.fields);
if (mode === 'kanji') { const note = {
fields = options.anki.kanji.fields; fields: {},
note.deckName = options.anki.kanji.deck; tags,
note.modelName = options.anki.kanji.model; deckName: modeOptions.deck,
} else { modelName: modeOptions.model
fields = options.anki.terms.fields; };
note.deckName = options.anki.terms.deck;
note.modelName = options.anki.terms.model;
if (definition.audio) { for (const [fieldName, fieldValue] of modeOptionsFieldEntries) {
const audio = { note.fields[fieldName] = await dictFieldFormat(fieldValue, definition, mode, options, templates);
}
if (!isKanji && definition.audio) {
const audioFields = [];
for (const [fieldName, fieldValue] of modeOptionsFieldEntries) {
if (fieldValue.includes('{audio}')) {
audioFields.push(fieldName);
}
}
if (audioFields.length > 0) {
note.audio = {
url: definition.audio.url, url: definition.audio.url,
filename: definition.audio.filename, filename: definition.audio.filename,
skipHash: '7e2c2f954ef6051373ba916f000168dc', skipHash: '7e2c2f954ef6051373ba916f000168dc',
fields: [] fields: audioFields
}; };
for (const name in fields) {
if (fields[name].includes('{audio}')) {
audio.fields.push(name);
}
}
if (audio.fields.length > 0) {
note.audio = audio;
}
} }
} }
for (const name in fields) {
note.fields[name] = await dictFieldFormat(fields[name], definition, mode, options, templates);
}
return note; return note;
} }