Skip to content

Commit

Permalink
feat(icons): get medium sized icon
Browse files Browse the repository at this point in the history
  • Loading branch information
ovlb committed Oct 24, 2022
1 parent 1474a6c commit 26a6219
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
36 changes: 26 additions & 10 deletions _src/_data/_utils/img.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,34 @@ const supportedFormats = new Map([

const getFileExtension = (fileName) => fileName.split('.').pop()

async function makeIcon(icon) {
const format = supportedFormats.get(getFileExtension(icon.href.pathname))
/**
*
*
* @param {Array} icons
* @return {String}
*/
async function makeIcon(icons) {
const sized = icons.filter(({ sizes }) => sizes !== null)
let icon = null
let width = null

if (sized.length) {
icon = sized.sort((a, b) => a.sizes.localeCompare(b.sizes))[
Math.ceil(sized.length / 2)
]
width = parseInt(icon.sizes)
} else {
icon = icons[0]
}

if (!format) {
icon.parsedIcon = icon.href.href
const format = supportedFormats.get(getFileExtension(icon.url.pathname))

return icon
if (!format) {
return icon.url.href
}
const parsed = await Image(icon.href.href, {
width: [null],

const parsed = await Image(icon.url.href, {
width: [width],
formats: [format],
urlPath: '/icons/',
outputDir: './dist/icons/',
Expand All @@ -28,9 +46,7 @@ async function makeIcon(icon) {
},
})

icon.parsedIcon = parsed[format][0].url

return icon
return parsed[format][0].url
}

module.exports = { makeIcon }
4 changes: 2 additions & 2 deletions _src/_data/feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ module.exports = async function () {
console.groupEnd()
}

if (content.meta.icon) {
content.meta.icon = await makeIcon(content.meta.icon)
if (content.meta.icons.length) {
content.meta.parsedIcon = await makeIcon(content.meta.icons)
}

for (const post of content.posts) {
Expand Down
4 changes: 2 additions & 2 deletions _src/index.11ty.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module.exports = class Homepage {
(feed) =>
`<li style="display: flex; align-items: center;">
${
feed.data.feed.meta.icon
? `<img src="${feed.data.feed.meta.icon.parsedIcon}" alt="" style="display: inline-block; width: 1rem; margin-right: 0.5rem;"/>`
feed.data.feed.meta.parsedIcon
? `<img src="${feed.data.feed.meta.parsedIcon}" alt="" style="display: inline-block; width: 1rem; margin-right: 0.5rem;"/>`
: ''
}
<a href="${feed.data.permalink}">${
Expand Down

0 comments on commit 26a6219

Please sign in to comment.