From 2d191bfdbd955a363e7afdc79c7a2b4b11a2e9b7 Mon Sep 17 00:00:00 2001 From: StefanVukovic99 Date: Sun, 5 May 2024 02:30:09 +0200 Subject: add single dictionary handlebars (#814) * add single dictionary handlebars * fix dicts with kanji in title * sort * rename to single-glossary-XYZ * add brief and no dict variants * add docs, only terms no kanji * allow testing single dict handlebars * remove empty comment --- ext/js/data/anki-template-util.js | 51 +++++++++++++++++++++++++++++++++++++++ ext/js/data/anki-util.js | 4 +-- ext/js/data/options-util.js | 12 ++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) (limited to 'ext/js/data') diff --git a/ext/js/data/anki-template-util.js b/ext/js/data/anki-template-util.js index 380d87f9..b9c5ba84 100644 --- a/ext/js/data/anki-template-util.js +++ b/ext/js/data/anki-template-util.js @@ -93,3 +93,54 @@ export function getStandardFieldMarkers(type) { throw new Error(`Unsupported type: ${type}`); } } + +/** + * @param {import('settings').ProfileOptions} options + * @returns {string} + */ +export function getDynamicTemplates(options) { + let dynamicTemplates = '\n'; + for (const dictionary of options.dictionaries) { + if (!dictionary.enabled) { continue; } + dynamicTemplates += ` +{{#*inline "single-glossary-${getKebabCase(dictionary.name)}"}} + {{~> glossary selectedDictionary='${dictionary.name}'}} +{{/inline}} + +{{#*inline "single-glossary-${getKebabCase(dictionary.name)}-no-dictionary"}} + {{~> glossary selectedDictionary='${dictionary.name}' noDictionaryTag=true}} +{{/inline}} + +{{#*inline "single-glossary-${getKebabCase(dictionary.name)}-brief"}} + {{~> glossary selectedDictionary='${dictionary.name}' brief=true}} +{{/inline}} +`; + } + return dynamicTemplates; +} + +/** + * @param {import('settings').DictionariesOptions} dictionaries + * @returns {string[]} The list of field markers. + */ +export function getDynamicFieldMarkers(dictionaries) { + const markers = []; + for (const dictionary of dictionaries) { + if (!dictionary.enabled) { continue; } + markers.push(`single-glossary-${getKebabCase(dictionary.name)}`); + } + return markers; +} + +/** + * @param {string} str + * @returns {string} + */ +function getKebabCase(str) { + return str + .replace(/[\s_\u3000]/g, '-') + .replace(/[^\p{L}\p{N}-]/gu, '') + .replace(/--+/g, '-') + .replace(/^-|-$/g, '') + .toLowerCase(); +} diff --git a/ext/js/data/anki-util.js b/ext/js/data/anki-util.js index a063980f..c076c482 100644 --- a/ext/js/data/anki-util.js +++ b/ext/js/data/anki-util.js @@ -19,7 +19,7 @@ import {isObjectNotArray} from '../core/object-utilities.js'; /** @type {RegExp} @readonly */ -const markerPattern = /\{([\w-]+)\}/g; +const markerPattern = /\{([\p{Letter}\p{Number}_-]+)\}/gu; /** * Gets the root deck name of a full deck name. If the deck is a root deck, @@ -65,7 +65,7 @@ export function getFieldMarkers(string) { * @returns {RegExp} A new `RegExp` instance. */ export function cloneFieldMarkerPattern(global) { - return new RegExp(markerPattern.source, global ? 'g' : ''); + return new RegExp(markerPattern.source, global ? 'gu' : 'u'); } /** diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index d6c667aa..b6fb6686 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -539,7 +539,8 @@ export class OptionsUtil { this._updateVersion30, this._updateVersion31, this._updateVersion32, - this._updateVersion33 + this._updateVersion33, + this._updateVersion34 ]; /* eslint-enable @typescript-eslint/unbound-method */ if (typeof targetVersion === 'number' && targetVersion < result.length) { @@ -1267,6 +1268,15 @@ export class OptionsUtil { await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v33.handlebars'); } + /** + * - Added dynamic handlebars for single dictionaries. + * @type {import('options-util').UpdateFunction} + */ + async _updateVersion34(options) { + await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v34.handlebars'); + } + + /** * @param {string} url * @returns {Promise} -- cgit v1.2.3