aboutsummaryrefslogtreecommitdiff
path: root/ext/js/data/anki-template-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/data/anki-template-util.js')
-rw-r--r--ext/js/data/anki-template-util.js51
1 files changed, 51 insertions, 0 deletions
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();
+}