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", "areSetsEqual": "readonly",
"getSetIntersection": "readonly", "getSetIntersection": "readonly",
"getSetDifference": "readonly", "getSetDifference": "readonly",
"escapeRegExp": "readonly",
"EventDispatcher": "readonly", "EventDispatcher": "readonly",
"EventListenerCollection": "readonly", "EventListenerCollection": "readonly",
"EXTENSION_IS_BROWSER_EDGE": "readonly" "EXTENSION_IS_BROWSER_EDGE": "readonly"

View File

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

View File

@ -150,7 +150,7 @@ class Backend {
await profileConditionsDescriptorPromise; await profileConditionsDescriptorPromise;
this.optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET'); 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 = await optionsLoad();
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options); this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options);

View File

@ -123,6 +123,26 @@ function handlebarsRegexMatch(...args) {
return value; 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() { function handlebarsRegisterHelpers() {
if (Handlebars.partials !== Handlebars.templates) { if (Handlebars.partials !== Handlebars.templates) {
Handlebars.partials = Handlebars.templates; Handlebars.partials = Handlebars.templates;
@ -134,6 +154,7 @@ function handlebarsRegisterHelpers() {
Handlebars.registerHelper('sanitizeCssClass', handlebarsSanitizeCssClass); Handlebars.registerHelper('sanitizeCssClass', handlebarsSanitizeCssClass);
Handlebars.registerHelper('regexReplace', handlebarsRegexReplace); Handlebars.registerHelper('regexReplace', handlebarsRegexReplace);
Handlebars.registerHelper('regexMatch', handlebarsRegexMatch); Handlebars.registerHelper('regexMatch', handlebarsRegexMatch);
Handlebars.registerHelper('mergeTags', handlebarsMergeTags);
} }
} }

View File

@ -111,19 +111,31 @@ const profileOptionsVersionUpdates = [
}, },
(options) => { (options) => {
// Version 14 changes: // Version 14 changes:
// Changed template for Anki audio. // Changed template for Anki audio and tags.
let fieldTemplates = options.anki.fieldTemplates; let fieldTemplates = options.anki.fieldTemplates;
if (typeof fieldTemplates !== 'string') { return; } if (typeof fieldTemplates !== 'string') { return; }
const replacement = '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}'; const replacements = [
let replaced = false; [
fieldTemplates = fieldTemplates.replace(/\{\{#\*inline "audio"\}\}\{\{\/inline\}\}/g, () => { '{{#*inline "audio"}}{{/inline}}',
replaced = true; '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}'
return replacement; ],
}); [
'{{#*inline "tags"}}\n {{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}}\n{{/inline}}',
'{{#*inline "tags"}}\n {{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}}\n{{/inline}}'
]
];
if (!replaced) { for (const [pattern, replacement] of replacements) {
fieldTemplates += '\n\n' + replacement; let replaced = false;
fieldTemplates = fieldTemplates.replace(new RegExp(escapeRegExp(pattern), 'g'), () => {
replaced = true;
return replacement;
});
if (!replaced) {
fieldTemplates += '\n\n' + replacement;
}
} }
options.anki.fieldTemplates = fieldTemplates; options.anki.fieldTemplates = fieldTemplates;

View File

@ -94,6 +94,11 @@ function hasOwn(object, property) {
return Object.prototype.hasOwnProperty.call(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. // toIterable is required on Edge for cross-window origin objects.
function toIterable(value) { function toIterable(value) {
if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') {