From 417c467a30570d78c98926e083c9826ea2874826 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Tue, 30 Jan 2024 21:37:54 -0800 Subject: [PATCH] Add image attribution link --- .../convertEntryToDetailedDefinition.js | 10 +++++--- src/util/yomitan/createEntryAttribution.js | 25 ++++++++++++++++++- src/util/yomitan/createEntryImageSC.js | 6 +++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/util/yomitan/convertEntryToDetailedDefinition.js b/src/util/yomitan/convertEntryToDetailedDefinition.js index 843dd5c..96a703a 100644 --- a/src/util/yomitan/convertEntryToDetailedDefinition.js +++ b/src/util/yomitan/convertEntryToDetailedDefinition.js @@ -31,14 +31,16 @@ function convertEntryToDetailedDefinition(entry) { }, }); // Image + let imageURLs = []; if (entry.tags.some((tag) => tag.name === 'img')) { - const imageNode = createEntryImageSC(entry); - if (imageNode.length > 0) { - SCArray.push(imageNode); + const { SCs, validImageURLs } = createEntryImageSC(entry); + if (SCs.length > 0) { + SCArray.push(SCs); } + imageURLs.push(...validImageURLs); } // Attribution - SCArray.push(createEntryAttribution(entry)); + SCArray.push(createEntryAttribution(entry, imageURLs)); return { type: 'structured-content', content: SCArray, diff --git a/src/util/yomitan/createEntryAttribution.js b/src/util/yomitan/createEntryAttribution.js index a1e2310..67194d6 100644 --- a/src/util/yomitan/createEntryAttribution.js +++ b/src/util/yomitan/createEntryAttribution.js @@ -1,9 +1,10 @@ /** * * @param {DictionaryEntry} entry + * @param {string[]} imageURLs * @returns {import("yomichan-dict-builder/dist/types/yomitan/termbank").StructuredContent} */ -function createEntryAttribution(entry) { +function createEntryAttribution(entry, imageURLs) { /** * @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} */ @@ -39,6 +40,28 @@ function createEntryAttribution(entry) { ); } } + + // Add image attributions + if (imageURLs.length > 0) { + for (const imageURL of imageURLs) { + try { + const url = new URL(imageURL); + const urlDomain = url.hostname; + contentAttributionSCArray.unshift( + { + tag: 'a', + href: imageURL, + content: `圖片: ${urlDomain}`, + }, + { + tag: 'span', + content: ' | ', + } + ); + } catch (error) {} + } + } + return { tag: 'div', data: { diff --git a/src/util/yomitan/createEntryImageSC.js b/src/util/yomitan/createEntryImageSC.js index 88da040..f831ec3 100644 --- a/src/util/yomitan/createEntryImageSC.js +++ b/src/util/yomitan/createEntryImageSC.js @@ -5,7 +5,7 @@ import { IMAGE_FOLDER } from '../../constants.js'; /** * @param {DictionaryEntry} entry - * @returns {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} + * @returns {{SCs: import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[], validImageURLs: string[]}} */ function createEntryImageSC(entry) { // Check if entry has images @@ -18,6 +18,7 @@ function createEntryImageSC(entry) { * @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} */ const SCs = []; + const validImageURLs = []; for (const tag of imageTags) { try { const fileName = getImageFileName(tag.value); @@ -33,9 +34,10 @@ function createEntryImageSC(entry) { }, path: filePath, }); + validImageURLs.push(tag.value); } catch (error) {} } - return SCs; + return { SCs, validImageURLs }; } export { createEntryImageSC };