diff --git a/README.md b/README.md index d3f194f4..4a6da5e3 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,25 @@ async function generate () { generate(); ``` +### generateInfoData + +> Type: `Boolean` +> Default value: `false` + +Output `./dist/info.json`, The content is as follows: + +```js +{ + "adobe": { + "encodedCode": "\\ea01", + "prefix": "svgtofont", + "className": "svgtofont-adobe", + "unicode": "" + }, + ... +} +``` + ### src > Type: `String` diff --git a/src/index.ts b/src/index.ts index f7fe24b8..8597dbd1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,6 +58,22 @@ export type SvgToFontOptions = { * ``` */ outSVGPath?: boolean; + /** + * Output `./dist/info.json`, The content is as follows: + * @example + * ```js + * { + * "adobe": { + * "encodedCode": "\\ea01", + * "prefix": "svgtofont", + * "className": "svgtofont-adobe", + * "unicode": "" + * }, + * ..... + * } + * ``` + */ + generateInfoData?: boolean; /** * This is the setting for [svgicons2svgfont](https://github.com/nfroidure/svgicons2svgfont/tree/dd713bea4f97afa59f7dba6a21ff7f22db565bcf#api) */ @@ -163,6 +179,15 @@ export type SvgToFontOptions = { typescript?: boolean | TypescriptOptions } +export type IconInfo = { + prefix: string; + symbol: string; + unicode: string; + className: string; + encodedCode: string | number; +} +export type InfoData = Record> + export default async (options: SvgToFontOptions = {}) => { const confPath = path.join(process.cwd(), '.svgtofontrc'); if (fs.pathExistsSync(confPath)) { @@ -197,6 +222,7 @@ export default async (options: SvgToFontOptions = {}) => { // If you generate a font you need to generate a style. if (options.website && !options.css) options.css = true; + const infoDataPath = path.resolve(options.dist, 'info.json'); try { if (options.emptyDist) { await fs.emptyDir(options.dist); @@ -211,8 +237,9 @@ export default async (options: SvgToFontOptions = {}) => { let unicodeHtml: string[] = []; let symbolHtml: string[] = []; const prefix = options.classNamePrefix || options.fontName; - + const infoData: InfoData = {} Object.keys(unicodeObject).forEach(name => { + if (!infoData[name]) infoData[name] = {}; const _code = unicodeObject[name]; let symbolName = options.classNamePrefix + options.symbolNameDelimiter + name let iconPart = symbolName + '">'; @@ -226,7 +253,10 @@ export default async (options: SvgToFontOptions = {}) => { cssString.push(`.${symbolName}:before { content: "\\${encodedCodes.toString(16)}"; }\n`); cssToVars.push(`$${symbolName}: "\\${encodedCodes.toString(16)}";\n`); } - + infoData[name].encodedCode = `\\${encodedCodes.toString(16)}`; + infoData[name].prefix = prefix; + infoData[name].className = symbolName; + infoData[name].unicode = `&#${encodedCodes};`; cssIconHtml.push(`
  • ${name}

  • `); unicodeHtml.push(`
  • ${_code}

    ${name}

    &#${encodedCodes};
  • `); symbolHtml.push(` @@ -238,7 +268,10 @@ export default async (options: SvgToFontOptions = {}) => { `); }); - + if (options.generateInfoData) { + await fs.writeJSON(infoDataPath, infoData, { spaces: 2 }); + log.log(`${color.green('SUCCESS')} Created ${infoDataPath} `); + } const ttf = await createTTF(options); await createEOT(options, ttf); await createWOFF(options, ttf); diff --git a/src/utils.ts b/src/utils.ts index a63a8616..3db007ef 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -46,7 +46,7 @@ export function createSVG(options: SvgToFontOptions = {}): Promise { - log.log(`${color.green('SUCCESS')} ${color.blue('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve(UnicodeObj); }) .on("error", (err) => { @@ -159,7 +159,7 @@ export function createTTF(options: SvgToFontOptions = {}): Promise { if (err) { return reject(err); } - log.log(`${color.green('SUCCESS')} ${color.blue('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve(ttfBuf); }); }); @@ -177,7 +177,7 @@ export function createEOT(options: SvgToFontOptions = {}, ttf: Buffer) { if (err) { return reject(err); } - log.log(`${color.green('SUCCESS')} ${color.blue('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve(eot); }); }); @@ -194,7 +194,7 @@ export function createWOFF(options: SvgToFontOptions = {}, ttf: Buffer) { if (err) { return reject(err); } - log.log(`${color.green('SUCCESS')} ${color.blue('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve(woff); }); }); @@ -211,7 +211,7 @@ export function createWOFF2(options: SvgToFontOptions = {}, ttf: Buffer) { if (err) { return reject(err); } - log.log(`${color.green('SUCCESS')} ${color.blue('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve({ path: DIST_PATH }); @@ -241,7 +241,7 @@ export function createSvgSymbol(options: SvgToFontOptions = {}) { if (err) { return reject(err); } - log.log(`${color.green('SUCCESS')} ${color.blue('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); + log.log(`${color.green('SUCCESS')} ${color.blue_bt('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`); resolve({ path: DIST_PATH, svg: $.html("svg")