Skip to content

Instantly share code, notes, and snippets.

@mehmetnyarar
Created July 26, 2018 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehmetnyarar/5569ba1225b4264da5d16721334f1c14 to your computer and use it in GitHub Desktop.
Save mehmetnyarar/5569ba1225b4264da5d16721334f1c14 to your computer and use it in GitHub Desktop.
Creates a JSON file for fontawesome icons.
/**
* Creates a JSON file for fontawesome icons. Results is an array of following object shape:
* {
* id: "<library>-<iconName>",
* name: "<iconName>",
* label: "<iconLabel>",
* tags: ["tags", "for", "search"],
* css: "<css-class>"
* }
*
* library: "fab" | "far" | "fas" (based on styles key)
* iconName: Name of the icon
* iconLabel: Label of the icon (based on label key)
* tags: Name of the icon + search.terms
* css: Ready-to-use css class
*
* 1. Download the fontawesome
* 2. Copy metadata/icons.json under the same folder of this file
* 3. Run node fa.js
* 4. Output is: fa.json
*
* Example object:
* {
* "id":"fas-calendar-alt",
* "name":"calendar-alt",
* "label":"Alternate Calendar",
* "tags":["calendar","date","event","schedule","time","when","calendar","alt"],
* "css":"fas fa-calendar-alt"
* }
*/
const fs = require('fs');
const raw = fs.readFileSync('./icons.json');
const fa = JSON.parse(raw);
const icons = [];
const styles = {
brands: 'fab',
regular: 'far',
solid: 'fas'
};
const readIconsAsync = () =>
new Promise((resolve) => {
Object.keys(fa).forEach((name) => {
const icon = fa[name];
const tags = icon.search.terms || [];
name
.replace('icon', '')
.replace('theme', '')
.split('-')
.forEach((term) => tags.push(term));
icon.styles.forEach((style) => {
const category = styles[style];
const id = `${category}-${name}`;
const label = icon.label || name;
const css = `${styles[style]} fa-${name}`;
icons.push({ id, name, label, tags, css });
});
resolve();
});
});
readIconsAsync().then(() => {
const json = JSON.stringify(icons);
fs.writeFileSync('fa.json', json);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment