-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement draft embeddable icon links generator #5
- Loading branch information
Showing
5 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,69 @@ | ||
import * as path from 'path' | ||
import sharp from 'sharp' | ||
|
||
import { iconTypesAndEdgesMap } from './constants' | ||
import { normalizeOutputTypes, resolveAndCreateOrUseOutputPath, resolveAndCheckInputFilePath } from './helpers' | ||
import { | ||
normalizeOutputTypes, | ||
resolveAndCreateOrUseOutputPath, | ||
resolveAndCheckInputFilePath, | ||
forEachIconTypeEdgeIncludes, | ||
isValidHexColorString, | ||
} from './helpers' | ||
import { IconType } from './types' | ||
|
||
/** | ||
* Generate favicons in various formats from image. | ||
* @param {string} imageInput File from which icons will be generated. Can be path to input file or buffer. | ||
* @param {IconType | IconType[]} outputTypes Icon types to be generated. Can be a single type or an array of types. null means all types. | ||
* @param {IconType | Array<IconType>} outputIconTypes Icon types to be generated. Can be a single type or an array of types. null means all types. | ||
* @param {string} outputDirectoryPath Directory where to save icons. If not specified it will be `icons/` | ||
*/ | ||
export async function faviconize( | ||
imageInput: string | Buffer, | ||
outputTypes?: IconType | Array<IconType>, | ||
outputIconTypes?: IconType | Array<IconType>, | ||
outputDirectoryPath?: string, | ||
) { | ||
const resolvedImageInput = Buffer.isBuffer(imageInput) ? imageInput : await resolveAndCheckInputFilePath(imageInput) | ||
const normalizedOutputTypes = normalizeOutputTypes(outputTypes) | ||
const normalizedOutputTypes = normalizeOutputTypes(outputIconTypes) | ||
const resolvedOutputPath = await resolveAndCreateOrUseOutputPath(outputDirectoryPath) | ||
|
||
for (const [type, edges] of Object.entries(iconTypesAndEdgesMap)) { | ||
if (normalizedOutputTypes.has(type as IconType)) { | ||
try { | ||
await Promise.all( | ||
edges.map((edge) => { | ||
const size = [edge, edge] | ||
const outputFileAbsolutePath = path.join(resolvedOutputPath, `${type}-${size.join('x')}.png`) | ||
return sharp(resolvedImageInput) | ||
.resize(...size) | ||
.toFile(outputFileAbsolutePath) | ||
}), | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
await forEachIconTypeEdgeIncludes(normalizedOutputTypes, async (type, edge) => { | ||
const size = [edge, edge] | ||
const outputFileAbsolutePath = path.join(resolvedOutputPath, `${type}-${size.join('x')}.png`) | ||
|
||
await sharp(resolvedImageInput) | ||
.resize(...size) | ||
.toFile(outputFileAbsolutePath) | ||
}) | ||
} | ||
|
||
/** | ||
* Generate embeddable favicons link tags. | ||
* @param {IconType | Array<IconType>} outputIconTypes Icon types for whose link tags will be generated. Can be a single type or an array of types. null means all types. | ||
* @param {string} tileColor Optional HEX (`#rrggbb` or `#rgb`) string representing the color of the tile in Microsoft specific integrations. | ||
*/ | ||
export async function generateIconsLinkTags(outputIconTypes?: IconType | Array<IconType>, tileColor?: string) { | ||
const normalizedOutputTypes = normalizeOutputTypes(outputIconTypes) | ||
const linkTags: Array<string> = [] | ||
|
||
if (tileColor) { | ||
if (!isValidHexColorString(tileColor)) { | ||
throw new Error(`Provided tile color (${tileColor}) is not valid hex color string.`) | ||
} | ||
|
||
linkTags.push(`<meta name="msapplication-TileColor" content="${tileColor}">`) | ||
} | ||
|
||
await forEachIconTypeEdgeIncludes(normalizedOutputTypes, (type, edge) => { | ||
const size = [edge, edge] | ||
const fileName = `${type}-${size.join('x')}.png` | ||
const filePath = path.join('icons', fileName) | ||
|
||
if (type === 'msapplication-TileImage') { | ||
linkTags.push(`<meta name="msapplication-TileImage" content="${filePath}">`) | ||
return | ||
} | ||
|
||
linkTags.push(`<link rel="${type}" type="image/png" href="${filePath}" sizes="${size.join('x')}">`) | ||
}) | ||
|
||
return linkTags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import { faviconize } from './faviconize' | ||
export { faviconize } | ||
import { faviconize, generateIconsLinkTags } from './faviconize' | ||
export { faviconize, generateIconsLinkTags } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { faviconize, generateIconsLinkTags } from '../src/faviconize' | ||
|
||
describe(faviconize, () => { | ||
it('is a function', async () => { | ||
console.log(await generateIconsLinkTags(null, '#cccccc')) | ||
expect(typeof faviconize).toBe('function') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters