Skip to content

Commit

Permalink
Add image attribution link
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jan 31, 2024
1 parent 4f2660b commit 417c467
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/util/yomitan/convertEntryToDetailedDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 24 additions & 1 deletion src/util/yomitan/createEntryAttribution.js
Original file line number Diff line number Diff line change
@@ -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[]}
*/
Expand Down Expand Up @@ -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: {
Expand Down
6 changes: 4 additions & 2 deletions src/util/yomitan/createEntryImageSC.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -33,9 +34,10 @@ function createEntryImageSC(entry) {
},
path: filePath,
});
validImageURLs.push(tag.value);
} catch (error) {}
}
return SCs;
return { SCs, validImageURLs };
}

export { createEntryImageSC };

0 comments on commit 417c467

Please sign in to comment.