Fix "tags" template (#539)

* fix tag templates for merge and group modes

* update version upgrade

* adjust upgrade replacement order
This commit is contained in:
siikamiika 2020-05-23 03:03:34 +03:00 committed by GitHub
parent 96ea941fa8
commit 39df44eca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 12 deletions

View File

@ -98,6 +98,7 @@
"areSetsEqual": "readonly",
"getSetIntersection": "readonly",
"getSetDifference": "readonly",
"escapeRegExp": "readonly",
"EventDispatcher": "readonly",
"EventListenerCollection": "readonly",
"EXTENSION_IS_BROWSER_EDGE": "readonly"

View File

@ -151,7 +151,7 @@
{{/inline}}
{{#*inline "tags"}}
{{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}}
{{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}}
{{/inline}}
{{#*inline "url"}}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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') {