From 7502c3668eb4a554546f03680f7a88176155e42a Mon Sep 17 00:00:00 2001 From: MarvNC Date: Sun, 21 Jan 2024 16:38:40 -0800 Subject: [PATCH] Add attribution footer link --- src/util/yomitan/createEntryAttribution.js | 43 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/util/yomitan/createEntryAttribution.js b/src/util/yomitan/createEntryAttribution.js index 120c19a..a1e2310 100644 --- a/src/util/yomitan/createEntryAttribution.js +++ b/src/util/yomitan/createEntryAttribution.js @@ -4,6 +4,41 @@ * @returns {import("yomichan-dict-builder/dist/types/yomitan/termbank").StructuredContent} */ function createEntryAttribution(entry) { + /** + * @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]} + */ + const contentAttributionSCArray = [ + { + tag: 'a', + href: `https://words.hk/zidin/v/${entry.id}`, + content: '粵典 words.hk', + }, + ]; + if (entry.tags.length > 0) { + // Find reference tag if exists + const referenceTag = entry.tags.find((tag) => tag.name === 'ref'); + if (referenceTag) { + let urlDomain = ''; + try { + const url = new URL(referenceTag.value); + urlDomain = url.hostname; + } catch (error) { + console.error(`Invalid URL: ${referenceTag.value}`); + } + + contentAttributionSCArray.unshift( + { + tag: 'a', + href: referenceTag.value, + content: `參考: ${urlDomain}`, + }, + { + tag: 'span', + content: ' | ', + } + ); + } + } return { tag: 'div', data: { @@ -16,13 +51,7 @@ function createEntryAttribution(entry) { // The examples/definitions above have marginBottom set marginTop: '-0.4em', }, - content: [ - { - tag: 'a', - href: `https://words.hk/zidin/v/${entry.id}`, - content: '粵典 words.hk', - }, - ], + content: contentAttributionSCArray, }; }