Skip to content

Commit c3c06a8

Browse files
committed
Make link tags generator & faviconize more consistent #5
1 parent 0bc943a commit c3c06a8

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

packages/faviconize/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export const defaultOutputDirectory = 'icons/'
44

55
export const iconTypesAndEdgesMap: IconTypeAndEdges = {
66
icon: [196, 160, 96, 32, 16],
7-
'msapplication-TileImage': [70, 150, 310],
7+
'msapplication-square[size]logo': [70, 150, 310],
88
'apple-touch-icon': [57, 144, 72, 144, 60, 120, 76, 152],
99
}

packages/faviconize/src/faviconize.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
resolveAndCheckInputFilePath,
88
forEachIconTypeEdgeIncludes,
99
isValidHexColorString,
10+
arrayInsertAt,
1011
} from './helpers'
1112
import { IconType } from './types'
1213

@@ -23,11 +24,17 @@ export async function faviconize(
2324
) {
2425
const resolvedImageInput = Buffer.isBuffer(imageInput) ? imageInput : await resolveAndCheckInputFilePath(imageInput)
2526
const normalizedOutputTypes = normalizeOutputTypes(outputIconTypes)
26-
const resolvedOutputPath = await resolveAndCreateOrUseOutputPath(outputDirectoryPath)
27+
const resolvedOutputDirectoryPath = await resolveAndCreateOrUseOutputPath(outputDirectoryPath)
2728

2829
await forEachIconTypeEdgeIncludes(normalizedOutputTypes, async (type, edge) => {
2930
const size = [edge, edge]
30-
const outputFileAbsolutePath = path.join(resolvedOutputPath, `${type}-${size.join('x')}.png`)
31+
32+
const outputFileName =
33+
type === 'msapplication-square[size]logo'
34+
? `${arrayInsertAt(type.split('[size]'), 1, size.join('x')).join("")}.png`
35+
: `${type}-${size.join('x')}.png`
36+
37+
const outputFileAbsolutePath = path.join(resolvedOutputDirectoryPath, outputFileName)
3138

3239
await sharp(resolvedImageInput)
3340
.resize(...size)
@@ -54,11 +61,16 @@ export async function generateIconsLinkTags(outputIconTypes?: IconType | Array<I
5461

5562
await forEachIconTypeEdgeIncludes(normalizedOutputTypes, (type, edge) => {
5663
const size = [edge, edge]
57-
const fileName = `${type}-${size.join('x')}.png`
64+
65+
const fileName =
66+
type === 'msapplication-square[size]logo'
67+
? `${arrayInsertAt(type.split('[size]'), 1, size.join('x')).join("")}.png`
68+
: `${type}-${size.join('x')}.png`
69+
5870
const filePath = path.join('icons', fileName)
5971

60-
if (type === 'msapplication-TileImage') {
61-
linkTags.push(`<meta name="msapplication-TileImage" content="${filePath}">`)
72+
if (type === 'msapplication-square[size]logo') {
73+
linkTags.push(`<meta name="msapplication-square${size.join('x')}logo" content="${filePath}">`)
6274
return
6375
}
6476

packages/faviconize/src/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ export async function forEachIconTypeEdgeIncludes(
6060
export function isValidHexColorString(color: string) {
6161
return /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(color)
6262
}
63+
64+
export function arrayInsertAt<T>(array: Array<T>, index: number, newItem: T) {
65+
return [...array.slice(0, index), newItem, ...array.slice(index)]
66+
}

packages/faviconize/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type IconType = 'icon' | 'msapplication-TileImage' | 'apple-touch-icon'
1+
export type IconType = 'icon' | 'msapplication-square[size]logo' | 'apple-touch-icon'
22

33
export type IconTypeAndEdges = {
44
[key in IconType]: number[]

0 commit comments

Comments
 (0)