diff --git a/.gitignore b/.gitignore index 13ef3ae..951a2db 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ /node_modules /coverage /build -/svg_list.json \ No newline at end of file +/resources.json \ No newline at end of file diff --git a/build.js b/build.js index 653919d..af71baf 100644 --- a/build.js +++ b/build.js @@ -61,15 +61,15 @@ svn.commands.checkout(FA_SVG_FOLDER_URL, svgDir, (err) => { console.log('\tReading SVGs...'); - getFileTree(svgDir, (err, list) => { + getFileTree(svgDir, (err, files) => { if (err) throw err; - const svgs = {}; + const list = {}; let count = 0; - list.filter(x => /\.svg$/.test(x)).forEach(svgFile => { + files.filter(x => /\.svg$/.test(x)).forEach(svgFile => { const data = fs.readFileSync(svgFile, {encoding: 'utf8'}); const match1 = data.match(/viewBox="0 0 (\d+) 512"/); if (!match1) @@ -81,10 +81,10 @@ svn.commands.checkout(FA_SVG_FOLDER_URL, svgDir, (err) => { const type = path.basename(path.dirname(svgFile)); const name = path.basename(svgFile, '.svg'); - if (!svgs[type]) - svgs[type] = {}; + if (!list[type]) + list[type] = {}; - svgs[type][name] = { + list[type][name] = { path: match2[1], width: parseInt(match1[1]) }; @@ -93,14 +93,21 @@ svn.commands.checkout(FA_SVG_FOLDER_URL, svgDir, (err) => { }); console.log(`\t${count} SVGs loaded`); - Object.keys(svgs).forEach(type => { - console.log(`\t\t${type}: ${Object.keys(svgs[type]).length} SVGs`); + Object.keys(list).forEach(type => { + console.log(`\t\t${type}: ${Object.keys(list[type]).length} SVGs`); }); - const output = path.join(__dirname, 'svg_list.json'); + const out = { + name: 'font-awesome', + height: 512, + index: ['solid', 'regular', 'brands'], + icons: list + }; - fs.writeFileSync(output, JSON.stringify(svgs, null, 4), {encoding: 'utf8'}); - console.log(`\tSVG list saved at "${output}"`); + const outputFile = path.join(__dirname, 'resources.json'); + + fs.writeFileSync(outputFile, JSON.stringify(out, null, 4), {encoding: 'utf8'}); + console.log(`\tSVG resources saved at "${outputFile}"`); }); }); diff --git a/package.json b/package.json index 14e537e..59f8c65 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,12 @@ "files": [ "/src", "/dist", - "/svg_list.json", + "/resources.json", "/build.js" ], "scripts": { "test": "jest --silent", - "prepublishOnly": "browserify --require ./svg_list.json:../svg_list.json -o dist/fa-diagrams-data.js && browserify --exclude ../svg_list.json src/index.js -o dist/fa-diagrams.js && uglifyjs -m -c -o dist/fa-diagrams.min.js -- dist/fa-diagrams.js && uglifyjs -m -c -o dist/fa-diagrams-data.min.js -- dist/fa-diagrams-data.js" + "prepublishOnly": "browserify --require ./resources.json:../resources.json -o dist/fa-diagrams-data.js && browserify --exclude ../resources.json src/index.js -o dist/fa-diagrams.js && uglifyjs -m -c -o dist/fa-diagrams.min.js -- dist/fa-diagrams.js && uglifyjs -m -c -o dist/fa-diagrams-data.min.js -- dist/fa-diagrams-data.js" }, "repository": { "type": "git", diff --git a/src/rendering.js b/src/rendering.js index 876d21e..d509d02 100644 --- a/src/rendering.js +++ b/src/rendering.js @@ -1,11 +1,23 @@ const convert = require('xml-js'); const utils = require('./utils'); -let list = {}; +let resources = { + name: 'error', + height: 512, + index: [], + links: { + 'arrow-head': {}, + 'arrow-head-reverse': {}, + 'line-start': {}, + 'line-end': {}, + 'dashed-step': {} + }, + icons: {} +}; try { - list = require('../svg_list.json'); + resources = require('../resources.json'); } catch (err) { - console.error('fa-diagrams: SVG list could not be loaded', err); + console.error('fa-diagrams: SVG resources could not be loaded', err); } /** @@ -80,6 +92,14 @@ const DEFAULT_OPTIONS = { } }; +const SUBSTITUTES = { + 'font-awesome': { + 'fas': 'solid', + 'far': 'regular', + 'fab': 'brands' + } +}; + const DEFAULT_SCALE = 0.4; const LINK_MARGIN = (1 - DEFAULT_SCALE) / 2; @@ -98,29 +118,24 @@ module.exports = (options) => { if (!name || !name.trim()) return null; - let search = ['solid', 'regular', 'brands']; + let search = utils.ezClone(resources.index); const spl = name.trim().split(' ').map(t => t.indexOf('fa-') === 0 ? t.substr(3) : t); - const checkType = (type, keywords) => { - if (search.length > 1) // else it's already found - keywords.forEach(kw => { - const i = spl.indexOf(kw); - if (i >= 0) { - spl.splice(i, 1); - search = [type]; - } - }); - }; - - checkType('solid', ['fas', 'solid']); - checkType('regular', ['far', 'regular']); - checkType('brands', ['fab', 'brands']); + for (let i = 0; i < spl.length; i++) { + //replace fas by regular for example + if (Object.keys(SUBSTITUTES[resources.name] || {}).includes(spl[i])) { + spl[i] = SUBSTITUTES[resources.name][spl[i]]; + } + if (resources.index.includes(spl[i])) { + search = [spl.splice(i, 1)]; + } + } name = spl[0]; for (let i = 0; i < search.length; i++) { - if (list[search[i]] && list[search[i]][name]) { - return list[search[i]][name]; + if (resources.icons[search[i]] && resources.icons[search[i]][name]) { + return resources.icons[search[i]][name]; } } @@ -143,7 +158,7 @@ module.exports = (options) => { case 'none': return null; default: - return `M12 216${lineStart}h${width * 512 - 146.059}${arrowHead}z`; + return `M12 216${resources.links['arrow-head'].path}h${width * 512 - 146.059}${arrowHead}z`; case 'line': return `M12 216${lineStart}h${width * 512 - 24}${lineEnd}z`; case 'double': diff --git a/test/rendering.test.js b/test/rendering.test.js index eff952d..70db11c 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -62,7 +62,7 @@ describe('getIcon', () => { test('double type', () => { const res = rendering().getIcon('circle far solid'); expect(res).toEqual({ - path: solidCirclePath, + path: regularCirclePath, width: 512 }); });