aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-08 22:08:11 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-08 22:25:03 -0500
commit4d7940e8e4ceb0b90439fc9726339f796ee23940 (patch)
tree9aac5f220a0095feeb04d06e27309967483b791b /ext/bg/js
parentfa963722a79c26c1642bbf81a2e153bd0c1a3fe8 (diff)
Optimize dictFieldFormat
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/dictionary.js81
1 files changed, 43 insertions, 38 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index 191058c1..e786c4e2 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -327,45 +327,50 @@ function dictFieldSplit(field) {
}
async function dictFieldFormat(field, definition, mode, options) {
- const markers = [
- 'audio',
- 'character',
- 'cloze-body',
- 'cloze-prefix',
- 'cloze-suffix',
- 'dictionary',
- 'expression',
- 'furigana',
- 'furigana-plain',
- 'glossary',
- 'glossary-brief',
- 'kunyomi',
- 'onyomi',
- 'reading',
- 'screenshot',
- 'sentence',
- 'tags',
- 'url'
- ];
-
- for (const marker of markers) {
- const data = {
- marker,
- definition,
- group: options.general.resultOutputMode === 'group',
- merge: options.general.resultOutputMode === 'merge',
- modeTermKanji: mode === 'term-kanji',
- modeTermKana: mode === 'term-kana',
- modeKanji: mode === 'kanji',
- compactGlossaries: options.general.compactGlossaries
- };
-
- const html = await apiTemplateRender(options.anki.fieldTemplates, data, true);
- field = field.replace(`{${marker}}`, html);
- }
-
- return field;
+ const data = {
+ marker: null,
+ definition,
+ group: options.general.resultOutputMode === 'group',
+ merge: options.general.resultOutputMode === 'merge',
+ modeTermKanji: mode === 'term-kanji',
+ modeTermKana: mode === 'term-kana',
+ modeKanji: mode === 'kanji',
+ compactGlossaries: options.general.compactGlossaries
+ };
+ const markers = dictFieldFormat.markers;
+ const pattern = /\{([\w\-]+)\}/g;
+ return await stringReplaceAsync(field, pattern, async (g0, marker) => {
+ if (!markers.has(marker)) {
+ return g0;
+ }
+ data.marker = marker;
+ try {
+ return await apiTemplateRender(options.anki.fieldTemplates, data, true);
+ } catch (e) {
+ return `{${marker}-render-error}`;
+ }
+ });
}
+dictFieldFormat.markers = new Set([
+ 'audio',
+ 'character',
+ 'cloze-body',
+ 'cloze-prefix',
+ 'cloze-suffix',
+ 'dictionary',
+ 'expression',
+ 'furigana',
+ 'furigana-plain',
+ 'glossary',
+ 'glossary-brief',
+ 'kunyomi',
+ 'onyomi',
+ 'reading',
+ 'screenshot',
+ 'sentence',
+ 'tags',
+ 'url'
+]);
async function dictNoteFormat(definition, mode, options) {
const note = {fields: {}, tags: options.anki.tags};