Fix "tags" template (#539)
* fix tag templates for merge and group modes * update version upgrade * adjust upgrade replacement order
This commit is contained in:
parent
96ea941fa8
commit
39df44eca4
@ -98,6 +98,7 @@
|
||||
"areSetsEqual": "readonly",
|
||||
"getSetIntersection": "readonly",
|
||||
"getSetDifference": "readonly",
|
||||
"escapeRegExp": "readonly",
|
||||
"EventDispatcher": "readonly",
|
||||
"EventListenerCollection": "readonly",
|
||||
"EXTENSION_IS_BROWSER_EDGE": "readonly"
|
||||
|
@ -151,7 +151,7 @@
|
||||
{{/inline}}
|
||||
|
||||
{{#*inline "tags"}}
|
||||
{{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}}
|
||||
{{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}}
|
||||
{{/inline}}
|
||||
|
||||
{{#*inline "url"}}
|
||||
|
@ -150,7 +150,7 @@ class Backend {
|
||||
await profileConditionsDescriptorPromise;
|
||||
|
||||
this.optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET');
|
||||
this.defaultAnkiFieldTemplates = await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET');
|
||||
this.defaultAnkiFieldTemplates = (await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET')).trim();
|
||||
this.options = await optionsLoad();
|
||||
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options);
|
||||
|
||||
|
@ -123,6 +123,26 @@ function handlebarsRegexMatch(...args) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function handlebarsMergeTags(object, isGroupMode, isMergeMode) {
|
||||
const tagSources = [];
|
||||
if (isGroupMode || isMergeMode) {
|
||||
for (const definition of object.definitions) {
|
||||
tagSources.push(definition.definitionTags);
|
||||
}
|
||||
} else {
|
||||
tagSources.push(object.definitionTags);
|
||||
}
|
||||
|
||||
const tags = new Set();
|
||||
for (const tagSource of tagSources) {
|
||||
for (const tag of tagSource) {
|
||||
tags.add(tag.name);
|
||||
}
|
||||
}
|
||||
|
||||
return [...tags].join(', ');
|
||||
}
|
||||
|
||||
function handlebarsRegisterHelpers() {
|
||||
if (Handlebars.partials !== Handlebars.templates) {
|
||||
Handlebars.partials = Handlebars.templates;
|
||||
@ -134,6 +154,7 @@ function handlebarsRegisterHelpers() {
|
||||
Handlebars.registerHelper('sanitizeCssClass', handlebarsSanitizeCssClass);
|
||||
Handlebars.registerHelper('regexReplace', handlebarsRegexReplace);
|
||||
Handlebars.registerHelper('regexMatch', handlebarsRegexMatch);
|
||||
Handlebars.registerHelper('mergeTags', handlebarsMergeTags);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,13 +111,24 @@ const profileOptionsVersionUpdates = [
|
||||
},
|
||||
(options) => {
|
||||
// Version 14 changes:
|
||||
// Changed template for Anki audio.
|
||||
// Changed template for Anki audio and tags.
|
||||
let fieldTemplates = options.anki.fieldTemplates;
|
||||
if (typeof fieldTemplates !== 'string') { return; }
|
||||
|
||||
const replacement = '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}';
|
||||
const replacements = [
|
||||
[
|
||||
'{{#*inline "audio"}}{{/inline}}',
|
||||
'{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}'
|
||||
],
|
||||
[
|
||||
'{{#*inline "tags"}}\n {{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}}\n{{/inline}}',
|
||||
'{{#*inline "tags"}}\n {{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}}\n{{/inline}}'
|
||||
]
|
||||
];
|
||||
|
||||
for (const [pattern, replacement] of replacements) {
|
||||
let replaced = false;
|
||||
fieldTemplates = fieldTemplates.replace(/\{\{#\*inline "audio"\}\}\{\{\/inline\}\}/g, () => {
|
||||
fieldTemplates = fieldTemplates.replace(new RegExp(escapeRegExp(pattern), 'g'), () => {
|
||||
replaced = true;
|
||||
return replacement;
|
||||
});
|
||||
@ -125,6 +136,7 @@ const profileOptionsVersionUpdates = [
|
||||
if (!replaced) {
|
||||
fieldTemplates += '\n\n' + replacement;
|
||||
}
|
||||
}
|
||||
|
||||
options.anki.fieldTemplates = fieldTemplates;
|
||||
}
|
||||
|
@ -94,6 +94,11 @@ function hasOwn(object, property) {
|
||||
return Object.prototype.hasOwnProperty.call(object, property);
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
|
||||
function escapeRegExp(string) {
|
||||
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
// toIterable is required on Edge for cross-window origin objects.
|
||||
function toIterable(value) {
|
||||
if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') {
|
||||
|
Loading…
Reference in New Issue
Block a user