From 9c029bc706c120bf74e6581dd29045388f882e82 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Fri, 19 Feb 2021 14:06:43 +0100 Subject: [PATCH] Allow custom slugs (#4918) --- .jsonlintschema | 6 ++++++ index.html | 3 +++ scripts/build-package.js | 10 +++++++--- scripts/templates/index.js | 8 +++++++- tests/icons.test.js | 2 +- tests/index.test.js | 23 ++++++++++++++++------- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/.jsonlintschema b/.jsonlintschema index c6d9fc281..5618db9a9 100644 --- a/.jsonlintschema +++ b/.jsonlintschema @@ -14,6 +14,12 @@ "type": "string", "required": true }, + "slug": { + "description": "The brand name slug (used as filename in icons/)", + "type": "string", + "pattern": "^[a-z0-9\\-]+_[a-z0-9\\-]+$", + "required": false + }, "hex": { "description": "The icons color, as HEX (without #)", "type": "string", diff --git a/index.html b/index.html index 6811967e0..679e9af73 100644 --- a/index.html +++ b/index.html @@ -55,6 +55,9 @@ {% assign filename = filename | replace: ".", "-dot-" %} {% assign filename = filename | replace: "&", "-and-" %} {% assign filename = filename | replace: " ", "" | replace: "!", "" | replace: ":", "" | replace: "’", "" | replace: "'", "" | replace: "°", "" %} + {% if icon.slug %} + {% assign filename = icon.slug %} + {% endif %} {% assign hex = icon.hex %} {% assign hexCharacter1 = hex | slice: 0, 1 %} diff --git a/scripts/build-package.js b/scripts/build-package.js index 761bed61e..59149a6b3 100644 --- a/scripts/build-package.js +++ b/scripts/build-package.js @@ -32,8 +32,12 @@ function escape(value) { return value.replace(/(? { - const filename = titleToSlug(icon.title); + const filename = icon.slug || titleToSlug(icon.title); const svgFilepath = path.resolve(iconsDir, `${filename}.svg`); icon.svg = fs.readFileSync(svgFilepath, UTF8).replace(/\r?\n/, ''); icon.slug = filename; diff --git a/scripts/templates/index.js b/scripts/templates/index.js index cd0370c79..e0d7d987a 100644 --- a/scripts/templates/index.js +++ b/scripts/templates/index.js @@ -9,7 +9,13 @@ Object.defineProperty(icons, "get", { var normalizedName = targetName.toLowerCase(); for (var iconName in icons) { var icon = icons[iconName]; - if (icon.title.toLowerCase() === normalizedName || icon.slug === normalizedName) { + if (icon.slug === normalizedName) { + return icon; + } + } + for (var iconName in icons) { + var icon = icons[iconName]; + if (icon.title.toLowerCase() === normalizedName) { return icon; } } diff --git a/tests/icons.test.js b/tests/icons.test.js index 1e2bc12b6..f551e6407 100644 --- a/tests/icons.test.js +++ b/tests/icons.test.js @@ -2,7 +2,7 @@ const { icons } = require('../_data/simple-icons.json'); const { titleToSlug } = require('../scripts/utils.js'); icons.forEach(icon => { - const filename = titleToSlug(icon.title); + const filename = icon.slug || titleToSlug(icon.title); const subject = require(`../icons/${filename}.js`); test(`${icon.title} has a "title"`, () => { diff --git a/tests/index.test.js b/tests/index.test.js index 9d1725e67..ff9b6cf8e 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -3,7 +3,8 @@ const simpleIcons = require('../index.js'); const { titleToSlug } = require("../scripts/utils.js"); icons.forEach(icon => { - const subject = simpleIcons[icon.title]; + const name = icon.slug || icon.title; + const subject = simpleIcons[name]; test(`${icon.title} has a "title"`, () => { expect(typeof subject.title).toBe('string'); @@ -31,17 +32,25 @@ icons.forEach(icon => { expect(typeof subject.slug).toBe('string'); }); - test(`${icon.title} can be found by it's title`, () => { - const found = simpleIcons.get(icon.title); - expect(found).toBeDefined(); - expect(found.title).toEqual(icon.title); - }); + // NOTE: Icons with custom slugs have a custom slug because their title is + // already taken, so they should not be findable by their title. + if (icon.slug === undefined) { + test(`${icon.title} can be found by it's title`, () => { + const found = simpleIcons.get(icon.title); + expect(found).toBeDefined(); + expect(found.title).toEqual(icon.title); + expect(found.hex).toEqual(icon.hex); + expect(found.source).toEqual(icon.source); + }); + } test(`${icon.title} can be found by it's slug`, () => { - const name = titleToSlug(icon.title); + const name = icon.slug || titleToSlug(icon.title); const found = simpleIcons.get(name); expect(found).toBeDefined(); expect(found.title).toEqual(icon.title); + expect(found.hex).toEqual(icon.hex); + expect(found.source).toEqual(icon.source); }); });