From 0506cfbee5443635ebd6ac79054b85f171775039 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 21:21:30 +0100 Subject: initial handlebars patch --- ext/js/data/options-util.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'ext/js') diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 2674701f..7b050416 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -470,7 +470,8 @@ class OptionsUtil { {async: false, update: this._updateVersion17.bind(this)}, {async: false, update: this._updateVersion18.bind(this)}, {async: false, update: this._updateVersion19.bind(this)}, - {async: false, update: this._updateVersion20.bind(this)} + {async: false, update: this._updateVersion20.bind(this)}, + {async: true, update: this._updateVersion21.bind(this)} ]; if (typeof targetVersion === 'number' && targetVersion < result.length) { result.splice(targetVersion); @@ -997,4 +998,9 @@ class OptionsUtil { } return options; } + + async _updateVersion21(options) { + await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v21.handlebars'); + return options; + } } -- cgit v1.2.3 From 9a78edaee74a8355d49ec930c82e118518d5d625 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 21:23:47 +0100 Subject: fix: dumpObject --- docs/templates.md | 4 ++-- ext/js/templates/sandbox/anki-template-renderer.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/js') diff --git a/docs/templates.md b/docs/templates.md index 778b8e2d..c050889f 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -14,7 +14,7 @@ This function can be helpful for debugging values when creating templates.
Syntax: - {{#dumpObject}}<object>{{/dumpObject}} + {{dumpObject object}} * _`object`_
The object to convert. @@ -23,7 +23,7 @@ This function can be helpful for debugging values when creating templates. Example: ```handlebars -
{{#dumpObject}}{{.}}{{/dumpObject}}
+
{{dumpObject .}}
``` Output: diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 789f0942..9e0bc8ed 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -134,8 +134,8 @@ class AnkiTemplateRenderer { // Template helpers - _dumpObject(context, options) { - const dump = JSON.stringify(options.fn(context), null, 4); + _dumpObject(context, object) { + const dump = JSON.stringify(object, null, 4); return this._escape(dump); } -- cgit v1.2.3 From ae91e2442d7b3746c633871ff956a8addd7046b5 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 21:41:45 +0100 Subject: fix: furigana and furiganaPlain --- docs/templates.md | 16 ++--- .../anki-field-templates-upgrade-v21.handlebars | 30 ++++++++ .../default-anki-field-templates.handlebars | 10 +-- ext/js/templates/sandbox/anki-template-renderer.js | 21 ++++-- test/test-options-util.js | 80 +++++++++++++++++++++- 5 files changed, 136 insertions(+), 21 deletions(-) (limited to 'ext/js') diff --git a/docs/templates.md b/docs/templates.md index c050889f..bf5bd897 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -49,8 +49,8 @@ Converts a definition or expression/reading pair to its furigana representation.
Syntax: - {{#furigana}}<definition>{{/furigana}}
- {{#furigana expression reading}}{{/furigana}}
+ {{furigana definition}}
+ {{furigana expression reading}}
* _`definition`_
The definition to convert. @@ -63,8 +63,8 @@ Converts a definition or expression/reading pair to its furigana representation. Example: ```handlebars - {{#furigana}}{{.}}{{/furigana}} - {{#furigana "読む" "よむ"}}{{/furigana}} + {{furigana .}} + {{furigana "読む" "よむ"}} ``` Output: @@ -84,8 +84,8 @@ Converts a definition or expression/reading pair to its simplified furigana repr
Syntax: - {{#furiganaPlain}}<definition>{{/furigana}} - {{#furiganaPlain expression reading}}{{/furiganaPlain}}
+ {{furiganaPlain definition}} + {{furiganaPlain expression reading}}
* _`definition`_
The definition to convert. @@ -98,8 +98,8 @@ Converts a definition or expression/reading pair to its simplified furigana repr Example: ```handlebars - {{~#furiganaPlain~}}{{.}}{{~/furiganaPlain~}} - {{#furiganaPlain "読む" "よむ"}}{{/furiganaPlain}} + {{~furiganaPlain .~}} + {{furiganaPlain "読む" "よむ"}} ``` Output: diff --git a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars index e69de29b..92034995 100644 --- a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars +++ b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars @@ -0,0 +1,30 @@ + +{{<<<<<<<}} +{{~#furigana}}{{{.}}}{{/furigana~}} +{{=======}} +{{~furigana .~}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{#furigana}}{{{definition}}}{{/furigana}} +{{=======}} +{{furigana definition}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{~#furiganaPlain}}{{{.}}}{{/furiganaPlain~}} +{{=======}} +{{~furiganaPlain .~}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}} +{{=======}} +{{furiganaPlain definition}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{~#furigana expression reading~}}{{~/furigana~}} +{{=======}} +{{~furigana expression reading~}} +{{>>>>>>>}} diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars index 31d5d13f..751236f4 100644 --- a/ext/data/templates/default-anki-field-templates.handlebars +++ b/ext/data/templates/default-anki-field-templates.handlebars @@ -78,22 +78,22 @@ {{#*inline "furigana"}} {{~#if merge~}} {{~#each definition.expressions~}} - {{~#furigana}}{{{.}}}{{/furigana~}} + {{~furigana .~}} {{~#unless @last}}、{{/unless~}} {{~/each~}} {{~else~}} - {{#furigana}}{{{definition}}}{{/furigana}} + {{furigana definition}} {{~/if~}} {{/inline}} {{#*inline "furigana-plain"}} {{~#if merge~}} {{~#each definition.expressions~}} - {{~#furiganaPlain}}{{{.}}}{{/furiganaPlain~}} + {{~furiganaPlain .~}} {{~#unless @last}}、{{/unless~}} {{~/each~}} {{~else~}} - {{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}} + {{furiganaPlain definition}} {{~/if~}} {{/inline}} @@ -255,7 +255,7 @@
  • {{~#if (op "!==" ../definition.type "kanji")~}} {{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}( - {{~#furigana expression reading~}}{{~/furigana~}} + {{~furigana expression reading~}} ) {{/if~}} {{~/if~}} {{~dictionary}}: {{frequency~}} diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 9e0bc8ed..e15bf895 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -132,6 +132,10 @@ class AnkiTemplateRenderer { return Handlebars.Utils.escapeExpression(text); } + _safeString(text) { + return new Handlebars.SafeString(text); + } + // Template helpers _dumpObject(context, object) { @@ -145,14 +149,16 @@ class AnkiTemplateRenderer { let result = ''; for (const {text, reading: reading2} of segs) { - if (reading2.length > 0) { - result += `${text}${reading2}`; + const safeText = this._escape(text); + const safeReading = this._escape(reading2); + if (safeReading.length > 0) { + result += `${safeText}${safeReading}`; } else { - result += text; + result += safeText; } } - return result; + return this._safeString(result); } _furiganaPlain(context, ...args) { @@ -173,12 +179,13 @@ class AnkiTemplateRenderer { } _getFuriganaExpressionAndReading(context, ...args) { - const options = args[args.length - 1]; if (args.length >= 3) { return {expression: args[0], reading: args[1]}; - } else { - const {expression, reading} = options.fn(context); + } else if (args.length === 2) { + const {expression, reading} = args[0]; return {expression, reading}; + } else { + return void 0; } } diff --git a/test/test-options-util.js b/test/test-options-util.js index 068367a2..e9504b0d 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -1217,14 +1217,92 @@ async function testFieldTemplatesUpdate(extDir) { <<>> {{~> (lookup . "marker") ~}}`.trimStart() }, - // block helper update + // block helper update: furigana and furiganaPlain { oldVersion: 20, newVersion: 21, old: ` +{{#*inline "furigana"}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~#furigana}}{{{.}}}{{/furigana~}} + {{~#unless @last}}、{{/unless~}} + {{~/each~}} + {{~else~}} + {{#furigana}}{{{definition}}}{{/furigana}} + {{~/if~}} +{{/inline}} + +{{#*inline "furigana-plain"}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~#furiganaPlain}}{{{.}}}{{/furiganaPlain~}} + {{~#unless @last}}、{{/unless~}} + {{~/each~}} + {{~else~}} + {{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}} + {{~/if~}} +{{/inline}} + +{{#*inline "frequencies"}} + {{~#if (op ">" definition.frequencies.length 0)~}} +
      + {{~#each definition.frequencies~}} +
    • + {{~#if (op "!==" ../definition.type "kanji")~}} + {{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}( + {{~#furigana expression reading~}}{{~/furigana~}} + ) {{/if~}} + {{~/if~}} + {{~dictionary}}: {{frequency~}} +
    • + {{~/each~}} +
    + {{~/if~}} +{{/inline}} + {{~> (lookup . "marker") ~}}`.trimStart(), expected: ` +{{#*inline "furigana"}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~furigana .~}} + {{~#unless @last}}、{{/unless~}} + {{~/each~}} + {{~else~}} + {{furigana definition}} + {{~/if~}} +{{/inline}} + +{{#*inline "furigana-plain"}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~furiganaPlain .~}} + {{~#unless @last}}、{{/unless~}} + {{~/each~}} + {{~else~}} + {{furiganaPlain definition}} + {{~/if~}} +{{/inline}} + +{{#*inline "frequencies"}} + {{~#if (op ">" definition.frequencies.length 0)~}} +
      + {{~#each definition.frequencies~}} +
    • + {{~#if (op "!==" ../definition.type "kanji")~}} + {{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}( + {{~furigana expression reading~}} + ) {{/if~}} + {{~/if~}} + {{~dictionary}}: {{frequency~}} +
    • + {{~/each~}} +
    + {{~/if~}} +{{/inline}} + {{~> (lookup . "marker") ~}}`.trimStart() } ]; -- cgit v1.2.3 From 376a1b096874e601296321fa6307836e2736a05c Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 21:52:31 +0100 Subject: fix: formatGlossary --- docs/templates.md | 4 +- .../anki-field-templates-upgrade-v21.handlebars | 18 ++++++ .../default-anki-field-templates.handlebars | 6 +- ext/js/templates/sandbox/anki-template-renderer.js | 5 +- test/test-options-util.js | 74 ++++++++++++++++++++++ 5 files changed, 99 insertions(+), 8 deletions(-) (limited to 'ext/js') diff --git a/docs/templates.md b/docs/templates.md index bf5bd897..ce1e8d7e 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -657,7 +657,7 @@ structured-content generation.
    Syntax: - {{#formatGlossary dictionary}}{{{definitionEntry}}}{{/pitchCategories}}
    + {{formatGlossary dictionary definitionEntry}}
    * _`dictionary`_
    The dictionary that the glossary entry belongs to. @@ -668,7 +668,7 @@ structured-content generation. Example: ```handlebars - {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}} + {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}} ``` Output: diff --git a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars index 92034995..99db50b6 100644 --- a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars +++ b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars @@ -1,4 +1,22 @@ +{{<<<<<<<}} +{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}} +{{=======}} +{{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}} +{{=======}} +{{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}} +{{>>>>>>>}} + +{{<<<<<<<}} +
      {{#each glossary}}
    • {{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}
    • {{/each}}
    +{{=======}} +
      {{#each glossary}}
    • {{formatGlossary ../dictionary .}}
    • {{/each}}
    +{{>>>>>>>}} + {{<<<<<<<}} {{~#furigana}}{{{.}}}{{/furigana~}} {{=======}} diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars index 751236f4..58bc68e2 100644 --- a/ext/data/templates/default-anki-field-templates.handlebars +++ b/ext/data/templates/default-anki-field-templates.handlebars @@ -21,11 +21,11 @@ {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}} {{~/unless~}} {{~#if (op "<=" glossary.length 1)~}} - {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}} + {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}} {{~else if @root.compactGlossaries~}} - {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}} + {{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}} {{~else~}} -
      {{#each glossary}}
    • {{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}
    • {{/each}}
    +
      {{#each glossary}}
    • {{formatGlossary ../dictionary .}}
    • {{/each}}
    {{~/if~}} {{~#set "previousDictionary" dictionary~}}{{~/set~}} {{/inline}} diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index e15bf895..62bf71b8 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -497,7 +497,7 @@ class AnkiTemplateRenderer { this._normalizeHtml(container, styleApplier, datasetKeyIgnorePattern); const result = container.innerHTML; container.textContent = ''; - return result; + return this._safeString(result); } _normalizeHtml(root, styleApplier, datasetKeyIgnorePattern) { @@ -550,9 +550,8 @@ class AnkiTemplateRenderer { return instance; } - _formatGlossary(context, dictionary, options) { + _formatGlossary(context, dictionary, content, options) { const data = options.data.root; - const content = options.fn(context); if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } if (!(typeof content === 'object' && content !== null)) { return ''; } switch (content.type) { diff --git a/test/test-options-util.js b/test/test-options-util.js index e9504b0d..c2780650 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -1303,6 +1303,80 @@ async function testFieldTemplatesUpdate(extDir) { {{~/if~}} {{/inline}} +{{~> (lookup . "marker") ~}}`.trimStart() + }, + // block helper update: formatGlossary + { + oldVersion: 20, + newVersion: 21, + old: ` +{{#*inline "glossary-single"}} + {{~#unless brief~}} + {{~#scope~}} + {{~#set "any" false}}{{/set~}} + {{~#each definitionTags~}} + {{~#if (op "||" (op "!" @root.compactTags) (op "!" redundant))~}} + {{~#if (get "any")}}, {{else}}({{/if~}} + {{name}} + {{~#set "any" true}}{{/set~}} + {{~/if~}} + {{~/each~}} + {{~#unless noDictionaryTag~}} + {{~#if (op "||" (op "!" @root.compactTags) (op "!==" dictionary (get "previousDictionary")))~}} + {{~#if (get "any")}}, {{else}}({{/if~}} + {{dictionary}} + {{~#set "any" true}}{{/set~}} + {{~/if~}} + {{~/unless~}} + {{~#if (get "any")}}) {{/if~}} + {{~/scope~}} + {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}} + {{~/unless~}} + {{~#if (op "<=" glossary.length 1)~}} + {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}} + {{~else if @root.compactGlossaries~}} + {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}} + {{~else~}} +
      {{#each glossary}}
    • {{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}
    • {{/each}}
    + {{~/if~}} + {{~#set "previousDictionary" dictionary~}}{{~/set~}} +{{/inline}} + +{{~> (lookup . "marker") ~}}`.trimStart(), + + expected: ` +{{#*inline "glossary-single"}} + {{~#unless brief~}} + {{~#scope~}} + {{~#set "any" false}}{{/set~}} + {{~#each definitionTags~}} + {{~#if (op "||" (op "!" @root.compactTags) (op "!" redundant))~}} + {{~#if (get "any")}}, {{else}}({{/if~}} + {{name}} + {{~#set "any" true}}{{/set~}} + {{~/if~}} + {{~/each~}} + {{~#unless noDictionaryTag~}} + {{~#if (op "||" (op "!" @root.compactTags) (op "!==" dictionary (get "previousDictionary")))~}} + {{~#if (get "any")}}, {{else}}({{/if~}} + {{dictionary}} + {{~#set "any" true}}{{/set~}} + {{~/if~}} + {{~/unless~}} + {{~#if (get "any")}}) {{/if~}} + {{~/scope~}} + {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}} + {{~/unless~}} + {{~#if (op "<=" glossary.length 1)~}} + {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}} + {{~else if @root.compactGlossaries~}} + {{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}} + {{~else~}} +
      {{#each glossary}}
    • {{formatGlossary ../dictionary .}}
    • {{/each}}
    + {{~/if~}} + {{~#set "previousDictionary" dictionary~}}{{~/set~}} +{{/inline}} + {{~> (lookup . "marker") ~}}`.trimStart() } ]; -- cgit v1.2.3 From 68ff30006edcd0dca44326780d34a6ca39f65157 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 21:54:53 +0100 Subject: fix: regexReplace and regexMatch joining the args together without cutting out the options arg can add an '[object Object]' into the result. --- docs/templates.md | 4 ++-- ext/js/templates/sandbox/anki-template-renderer.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/js') diff --git a/docs/templates.md b/docs/templates.md index ce1e8d7e..e0ea0861 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -147,7 +147,7 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr Syntax: {{#regexReplace regex replacement [flags]}}text-to-modify{{/regexReplace}}
    - {{#regexReplace regex replacement [flags] [text-to-modify]...}}{{/regexReplace}}
    + {{regexReplace regex replacement [flags] [text-to-modify]...}}
    * _`regex`_
    The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. @@ -181,7 +181,7 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr Syntax: {{#regexMatch regex [flags]}}text-to-modify{{/regexMatch}}
    - {{#regexMatch regex [flags] [text-to-modify]...}}{{/regexMatch}}
    + {{regexMatch regex [flags] [text-to-modify]...}}
    * _`regex`_
    The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 62bf71b8..5d8ebd38 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -226,7 +226,7 @@ class AnkiTemplateRenderer { const options = args[argCount]; let value = typeof options.fn === 'function' ? options.fn(context) : ''; if (argCount > 3) { - value = `${args.slice(3).join('')}${value}`; + value = `${args.slice(3, -1).join('')}${value}`; } if (argCount > 1) { try { @@ -250,7 +250,7 @@ class AnkiTemplateRenderer { const options = args[argCount]; let value = typeof options.fn === 'function' ? options.fn(context) : ''; if (argCount > 2) { - value = `${args.slice(2).join('')}${value}`; + value = `${args.slice(2, -1).join('')}${value}`; } if (argCount > 0) { try { -- cgit v1.2.3 From a454cc3ca1c34e51de13fe21f375f1f47d96a1e9 Mon Sep 17 00:00:00 2001 From: praschke Date: Mon, 16 Oct 2023 23:02:41 +0100 Subject: remove kanjiLinks and sanitizeCssClass --- docs/templates.md | 57 ---------------------- ext/js/templates/sandbox/anki-template-renderer.js | 20 -------- 2 files changed, 77 deletions(-) (limited to 'ext/js') diff --git a/docs/templates.md b/docs/templates.md index f021dea6..86c60db9 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -842,60 +842,3 @@ Converts hiragana text to katakana. ヨミチャン ヨミチャン ヨミチャン ```
    - - -## Legacy Helpers - -Yomichan has historically used Handlebars templates to generate the HTML used on the search page and results popup. -To simplify the and improve Yomichan's capabilities, the HTML elements are now generated directly using a different process. - -As such, there are several leftover Handlebars helpers that do not have much utility for Anki templates, but are kept for compatibility purposes. - - -### `kanjiLinks` - -Replaces kanji characters in the text with linkified versions. - -
    - Syntax: - - {{#kanjiLinks}}text{{/kanjiLinks}} -
    -
    - Example: - - ```handlebars - {{#kanjiLinks}}読む{{/kanjiLinks}} - ``` - - Output: - ```html - む - ``` - - Preview: -
    -
    - - -### `sanitizeCssClass` - -Sanitizes text so it can be used as a CSS class name. - -
    - Syntax: - - {{#sanitizeCssClass}}text{{/sanitizeCssClass}} -
    -
    - Example: - - ```handlebars - {{#sanitizeCssClass}}some text with many types of characters !@#$%^ 読む{{/sanitizeCssClass}} - ``` - - Output: - ```html - some_text_with_many_types_of_characters________読む - ``` -
    diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 5d8ebd38..766c7798 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -68,9 +68,7 @@ class AnkiTemplateRenderer { ['dumpObject', this._dumpObject.bind(this)], ['furigana', this._furigana.bind(this)], ['furiganaPlain', this._furiganaPlain.bind(this)], - ['kanjiLinks', this._kanjiLinks.bind(this)], ['multiLine', this._multiLine.bind(this)], - ['sanitizeCssClass', this._sanitizeCssClass.bind(this)], ['regexReplace', this._regexReplace.bind(this)], ['regexMatch', this._regexMatch.bind(this)], ['mergeTags', this._mergeTags.bind(this)], @@ -189,20 +187,6 @@ class AnkiTemplateRenderer { } } - _kanjiLinks(context, options) { - const jp = this._japaneseUtil; - let result = ''; - for (const c of options.fn(context)) { - if (jp.isCodePointKanji(c.codePointAt(0))) { - result += `${c}`; - } else { - result += c; - } - } - - return result; - } - _stringToMultiLineHtml(string) { return string.split('\n').join('
    '); } @@ -211,10 +195,6 @@ class AnkiTemplateRenderer { return this._stringToMultiLineHtml(options.fn(context)); } - _sanitizeCssClass(context, options) { - return options.fn(context).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_'); - } - _regexReplace(context, ...args) { // Usage: // {{#regexReplace regex string [flags] [content]...}}content{{/regexReplace}} -- cgit v1.2.3 From 757707539690b6aec45e9de8cd37fdfc907d8843 Mon Sep 17 00:00:00 2001 From: praschke Date: Sun, 22 Oct 2023 17:23:04 +0100 Subject: warn about custom templates in the welcome page --- ext/css/settings.css | 7 +++++++ ext/js/background/backend.js | 20 ++++++-------------- ext/js/data/options-util.js | 27 +++++++++++++++++++++++++++ ext/js/pages/welcome-main.js | 7 +++++++ ext/welcome.html | 27 ++++++++++++++++++++++----- 5 files changed, 69 insertions(+), 19 deletions(-) (limited to 'ext/js') diff --git a/ext/css/settings.css b/ext/css/settings.css index eaebc3af..a2618d88 100644 --- a/ext/css/settings.css +++ b/ext/css/settings.css @@ -2167,6 +2167,13 @@ button.hotkey-list-item-enabled-button[data-scope-count='0'] { display: none; } +.warn-custom-templates-notification { + border: 1px solid var(--danger-color); +} +:root:not([data-warn-custom-templates=true]) .warn-custom-templates-notification { + display: none; +} + .test-anki-note-viewer-container { margin-top: 0.85em; display: flex; diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 565f4abf..755010a3 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -2099,20 +2099,12 @@ class Backend { } async _openWelcomeGuidePageOnce() { - if (isObject(chrome.storage) && isObject(chrome.storage.session)) { - // Chrome - chrome.storage.session.get(['openedWelcomePage']).then((result) => { - if (!result.openedWelcomePage) { - this._openWelcomeGuidePage(); - chrome.storage.session.set({'openedWelcomePage': true}); - } - }); - } else { - // Firefox (storage.session is not supported yet) - // NOTE: This means that the welcome page will repeatedly open in Firefox - // until they support storage.session. - this._openWelcomeGuidePage(); - } + chrome.storage.session.get(['openedWelcomePage']).then((result) => { + if (!result.openedWelcomePage) { + this._openWelcomeGuidePage(); + chrome.storage.session.set({'openedWelcomePage': true}); + } + }); } async _openWelcomeGuidePage() { diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 7b050416..1f2ffb05 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -1001,6 +1001,33 @@ class OptionsUtil { async _updateVersion21(options) { await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v21.handlebars'); + + let customTemplates = false; + for (const {options: profileOptions} of options.profiles) { + if (profileOptions.anki.fieldTemplates !== null) { + customTemplates = true; + } + } + + if (customTemplates && isObject(chrome.storage)) { + chrome.storage.session.set({'needsCustomTemplatesWarning': true}); + await this._createTab(chrome.runtime.getURL('/welcome.html')); + chrome.storage.session.set({'openedWelcomePage': true}); + } + return options; } + + _createTab(url) { + return new Promise((resolve, reject) => { + chrome.tabs.create({url}, (tab) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(tab); + } + }); + }); + } } diff --git a/ext/js/pages/welcome-main.js b/ext/js/pages/welcome-main.js index 521ce2c2..8039dae5 100644 --- a/ext/js/pages/welcome-main.js +++ b/ext/js/pages/welcome-main.js @@ -58,6 +58,13 @@ async function setupGenericSettingsController(genericSettingController) { setupEnvironmentInfo(); + chrome.storage.session.get({'needsCustomTemplatesWarning': false}).then((result) => { + if (result.needsCustomTemplatesWarning) { + document.documentElement.dataset.warnCustomTemplates = 'true'; + chrome.storage.session.remove(['needsCustomTemplatesWarning']); + } + }); + const preparePromises = []; const modalController = new ModalController(); diff --git a/ext/welcome.html b/ext/welcome.html index 14e98367..56167866 100644 --- a/ext/welcome.html +++ b/ext/welcome.html @@ -25,6 +25,19 @@

    Welcome to Yomitan!

    + +
    +
    +
    +

    + There are custom Anki templates in your settings. Note that some syntax has changed from previous versions of Yomitan. + Please ensure that your custom templates are using the updated syntax. +

    +
    +
    +
    + +

    Here are some basics to get started

    @@ -49,10 +62,9 @@
    Yomitan requires one or more dictionaries to be installed in order to look up terms, kanji, and other information. - Several downloadable dictionaries can be found on the Yomitan homepage, - allowing you to choose the dictionaries most relevant for you. + Several downloadable dictionaries can be found on the Yomitan homepage. Dictionaries can be configured using the button below, - or later from the the Settings page. + or later from the Settings page.
    @@ -67,12 +79,17 @@
    - You can also import an exported collection of dictionaries to migrate from a different device or browser from the Backup section of the Settings page. + You can also import an exported collection of dictionaries from the Backup section of the Settings page.

    - If you are migrating from Yomichan, you may be particularly interested in migrating your data from Yomichan into Yomitan. + If you are migrating from Yomichan, you may be interested in importing your data into Yomitan. Please follow instructions from Yomitan's README for that. + +

    + + If you are using or planning to use custom templates for Anki note creation, note that some syntax has changed from Yomichan and Yomibaba. + Please ensure that your custom templates are using the updated syntax.
    -- cgit v1.2.3