diff --git a/src/util/yomitan/convertEntryToDetailedDefinition.js b/src/util/yomitan/convertEntryToDetailedDefinition.js index 96a703a..b5a300f 100644 --- a/src/util/yomitan/convertEntryToDetailedDefinition.js +++ b/src/util/yomitan/convertEntryToDetailedDefinition.js @@ -2,6 +2,7 @@ import { convertHeadwordsToSC } from './convertHeadwordsToSC.js'; import { convertSenseToLiSC } from './convertSenseToSC.js'; import { createEntryAttribution } from './createEntryAttribution.js'; import { createEntryImageSC } from './createEntryImageSC.js'; +import { convertEntryToSynAntsSC } from './convertEntryToSynAntsSC.js'; /** * Converts a dictionary entry to a detailed definition. @@ -15,6 +16,7 @@ function convertEntryToDetailedDefinition(entry) { const SCArray = []; // Headword SCArray.push(convertHeadwordsToSC(entry.headwords)); + // Senses with explanation/examples SCArray.push({ tag: 'div', @@ -30,6 +32,11 @@ function convertEntryToDetailedDefinition(entry) { content: entry.senses.map(convertSenseToLiSC), }, }); + + // Synonyms/antonyms + const synAntsSC = convertEntryToSynAntsSC(entry); + SCArray.push(...synAntsSC); + // Image let imageURLs = []; if (entry.tags.some((tag) => tag.name === 'img')) { @@ -39,6 +46,7 @@ function convertEntryToDetailedDefinition(entry) { } imageURLs.push(...validImageURLs); } + // Attribution SCArray.push(createEntryAttribution(entry, imageURLs)); return { diff --git a/src/util/yomitan/convertEntryToSynAntsSC.js b/src/util/yomitan/convertEntryToSynAntsSC.js new file mode 100644 index 0000000..77a7cc7 --- /dev/null +++ b/src/util/yomitan/convertEntryToSynAntsSC.js @@ -0,0 +1,60 @@ +/** + * Converts an entry to a ul list of the element's synonyms and antonyms. + * @param {DictionaryEntry} entry + * @returns {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} + */ +function convertEntryToSynAntsSC(entry) { + let exists = false; + /** + * @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} + */ + const SCArray = []; + /** + * @type {('sim'|'ant')[]} + */ + const tagTypes = ['sim', 'ant']; + for (const type of tagTypes) { + const { SC, exists: typeExists } = convertEntryToSCType(entry, type); + if (typeExists) { + SCArray.push(SC); + } + } + + return SCArray; +} + +/** + * + * @param {DictionaryEntry} entry + * @param {'sim'|'ant'} type + * @returns {{ SC: import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent, exists: boolean}} + */ +function convertEntryToSCType(entry, type) { + const typeTags = entry.tags.filter((tag) => tag.name === type); + if (typeTags.length === 0) { + return { + SC: [], + exists: false, + }; + } + let tagString = typeTags.map((tag) => tag.value).join('・'); + return { + SC: { + tag: 'ul', + data: { + wordshk: `${type}-list`, + }, + lang: 'yue', + content: { + tag: 'li', + data: { + wordshk: type, + }, + content: tagString, + }, + }, + exists: true, + }; +} + +export { convertEntryToSynAntsSC };