From d2c930a94d6e445053bcb5e5bb629851165425fc Mon Sep 17 00:00:00 2001 From: StefanVukovic99 Date: Thu, 20 Jun 2024 19:27:02 +0200 Subject: support css file in dictionaries (#1080) * get styles in db * get styles in settings * use styles * fix test * scope * fix comma separated * escape dict name in css selector * g regex * get styles in anki * fix tests * more specificity * whitespace * test importing * test handlebars * add styles to glossary-first --- ext/js/dictionary/dictionary-importer.js | 34 ++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'ext/js/dictionary/dictionary-importer.js') diff --git a/ext/js/dictionary/dictionary-importer.js b/ext/js/dictionary/dictionary-importer.js index 62453a13..d558636e 100644 --- a/ext/js/dictionary/dictionary-importer.js +++ b/ext/js/dictionary/dictionary-importer.js @@ -189,7 +189,25 @@ export class DictionaryImporter { tagMeta: {total: tagList.length}, media: {total: media.length}, }; - const summary = this._createSummary(dictionaryTitle, version, index, {prefixWildcardsSupported, counts}); + + const stylesFileName = 'styles.css'; + const stylesFile = fileMap.get(stylesFileName); + let styles = ''; + if (typeof stylesFile !== 'undefined') { + styles = await this._getData(stylesFile, new TextWriter()); + const cssErrors = this._validateCss(styles); + if (cssErrors.length > 0) { + return { + errors: cssErrors, + result: null, + }; + } + } + + /** @type {import('dictionary-importer').SummaryDetails} */ + const summaryDetails = {prefixWildcardsSupported, counts, styles}; + + const summary = this._createSummary(dictionaryTitle, version, index, summaryDetails); await dictionaryDatabase.bulkAdd('dictionaries', [summary], 0, 1); // Add data @@ -267,13 +285,12 @@ export class DictionaryImporter { * @param {string} dictionaryTitle * @param {number} version * @param {import('dictionary-data').Index} index - * @param {{prefixWildcardsSupported: boolean, counts: import('dictionary-importer').SummaryCounts}} details + * @param {import('dictionary-importer').SummaryDetails} details * @returns {import('dictionary-importer').Summary} */ _createSummary(dictionaryTitle, version, index, details) { const indexSequenced = index.sequenced; - const {prefixWildcardsSupported, counts} = details; - + const {prefixWildcardsSupported, counts, styles} = details; /** @type {import('dictionary-importer').Summary} */ const summary = { title: dictionaryTitle, @@ -283,6 +300,7 @@ export class DictionaryImporter { importDate: Date.now(), prefixWildcardsSupported, counts, + styles, }; const {author, url, description, attribution, frequencyMode, sourceLanguage, targetLanguage} = index; @@ -331,6 +349,14 @@ export class DictionaryImporter { return [termBank, termMetaBank, kanjiBank, kanjiMetaBank, tagBank]; } + /** + * @param {string} css + * @returns {Error[]} + */ + _validateCss(css) { + return css ? [] : [new Error('No styles found')]; + } + /** * @param {import('dictionary-data').TermGlossaryText|import('dictionary-data').TermGlossaryImage|import('dictionary-data').TermGlossaryStructuredContent} data * @param {import('dictionary-database').DatabaseTermEntry} entry -- cgit v1.2.3