From d2717481abeee8ff7f96c96b5db8050f103b8bab Mon Sep 17 00:00:00 2001 From: Kendrick Ang Date: Sun, 18 Oct 2020 15:20:06 +0800 Subject: [PATCH 1/5] add existence check for octicons --- .../src/lib/markdown-it/markdown-it-icons.js | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/core/src/lib/markdown-it/markdown-it-icons.js b/packages/core/src/lib/markdown-it/markdown-it-icons.js index d2a88e5b29..707f112d59 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-icons.js +++ b/packages/core/src/lib/markdown-it/markdown-it-icons.js @@ -1,22 +1,30 @@ const octicons = require('@primer/octicons'); +const _ = require('lodash'); module.exports = require('markdown-it-regexp')( - /:(fa[brs]|glyphicon|octicon|octiconlight)-([a-z-]+)~?([a-z-]+)?:/, - (match, _) => { - let iconFontType = match[1]; - let iconFontName = match[2]; - let iconClass = match[3]; + /:(fa[brs]|glyphicon|octicon|octiconlight)-([a-z-]+)~?([a-z-]+)?:/, + (match, _) => { + let iconFontType = match[1]; + let iconFontName = match[2]; + let iconClass = match[3]; - if (iconFontType === 'glyphicon') { - return ``; - } else if (iconFontType === 'octicon') { - return iconClass ? octicons[iconFontName].toSVG({"class": iconClass}) - : octicons[iconFontName].toSVG(); - } else if (iconFontType === 'octiconlight') { - return iconClass ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) - : octicons[iconFontName].toSVG({"style": "color: #fff;"}); - } else { // If icon is a Font Awesome icon - return ``; - } + // ensure octicons exist + if (iconFontType === 'octicon' || iconFontType === 'octiconlight') { + if (!octicons.hasOwnProperty(iconFontName)) { + return ``; + } } + + if (iconFontType === 'glyphicon') { + return ``; + } else if (iconFontType === 'octicon') { + return iconClass ? octicons[iconFontName].toSVG({"class": iconClass}) + : octicons[iconFontName].toSVG(); + } else if (iconFontType === 'octiconlight') { + return iconClass ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) + : octicons[iconFontName].toSVG({"style": "color: #fff;"}); + } else { // If icon is a Font Awesome icon + return ``; + } + } ); From f5ab749d7f786773e36f058a89f201fff348ef22 Mon Sep 17 00:00:00 2001 From: Kendrick Ang Date: Mon, 19 Oct 2020 10:41:19 +0800 Subject: [PATCH 2/5] Revert "add existence check for octicons" This reverts commit d2717481abeee8ff7f96c96b5db8050f103b8bab. --- .../src/lib/markdown-it/markdown-it-icons.js | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/packages/core/src/lib/markdown-it/markdown-it-icons.js b/packages/core/src/lib/markdown-it/markdown-it-icons.js index 707f112d59..d2a88e5b29 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-icons.js +++ b/packages/core/src/lib/markdown-it/markdown-it-icons.js @@ -1,30 +1,22 @@ const octicons = require('@primer/octicons'); -const _ = require('lodash'); module.exports = require('markdown-it-regexp')( - /:(fa[brs]|glyphicon|octicon|octiconlight)-([a-z-]+)~?([a-z-]+)?:/, - (match, _) => { - let iconFontType = match[1]; - let iconFontName = match[2]; - let iconClass = match[3]; + /:(fa[brs]|glyphicon|octicon|octiconlight)-([a-z-]+)~?([a-z-]+)?:/, + (match, _) => { + let iconFontType = match[1]; + let iconFontName = match[2]; + let iconClass = match[3]; - // ensure octicons exist - if (iconFontType === 'octicon' || iconFontType === 'octiconlight') { - if (!octicons.hasOwnProperty(iconFontName)) { - return ``; - } + if (iconFontType === 'glyphicon') { + return ``; + } else if (iconFontType === 'octicon') { + return iconClass ? octicons[iconFontName].toSVG({"class": iconClass}) + : octicons[iconFontName].toSVG(); + } else if (iconFontType === 'octiconlight') { + return iconClass ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) + : octicons[iconFontName].toSVG({"style": "color: #fff;"}); + } else { // If icon is a Font Awesome icon + return ``; + } } - - if (iconFontType === 'glyphicon') { - return ``; - } else if (iconFontType === 'octicon') { - return iconClass ? octicons[iconFontName].toSVG({"class": iconClass}) - : octicons[iconFontName].toSVG(); - } else if (iconFontType === 'octiconlight') { - return iconClass ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) - : octicons[iconFontName].toSVG({"style": "color: #fff;"}); - } else { // If icon is a Font Awesome icon - return ``; - } - } ); From c5129d70f6bca38aaea4b311fa58648baf45bed4 Mon Sep 17 00:00:00 2001 From: Kendrick Ang Date: Mon, 19 Oct 2020 10:42:07 +0800 Subject: [PATCH 3/5] add existence check for octicons --- packages/core/src/lib/markdown-it/markdown-it-icons.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/core/src/lib/markdown-it/markdown-it-icons.js b/packages/core/src/lib/markdown-it/markdown-it-icons.js index d2a88e5b29..44118efed5 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-icons.js +++ b/packages/core/src/lib/markdown-it/markdown-it-icons.js @@ -7,6 +7,13 @@ module.exports = require('markdown-it-regexp')( let iconFontName = match[2]; let iconClass = match[3]; + // ensure octicons exist + if (iconFontType === 'octicon' || iconFontType === 'octiconlight') { + if (!octicons.hasOwnProperty(iconFontName)) { + return ``; + } + } + if (iconFontType === 'glyphicon') { return ``; } else if (iconFontType === 'octicon') { From 9e48ade7e432262f7d45e9da6286438a59089074 Mon Sep 17 00:00:00 2001 From: Kendrick Ang Date: Tue, 20 Oct 2020 18:19:54 +0800 Subject: [PATCH 4/5] refactor logic based on comments --- .../src/lib/markdown-it/markdown-it-icons.js | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/core/src/lib/markdown-it/markdown-it-icons.js b/packages/core/src/lib/markdown-it/markdown-it-icons.js index 44118efed5..8e200f5377 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-icons.js +++ b/packages/core/src/lib/markdown-it/markdown-it-icons.js @@ -7,21 +7,26 @@ module.exports = require('markdown-it-regexp')( let iconFontName = match[2]; let iconClass = match[3]; - // ensure octicons exist - if (iconFontType === 'octicon' || iconFontType === 'octiconlight') { - if (!octicons.hasOwnProperty(iconFontName)) { - return ``; - } - } - if (iconFontType === 'glyphicon') { return ``; } else if (iconFontType === 'octicon') { - return iconClass ? octicons[iconFontName].toSVG({"class": iconClass}) - : octicons[iconFontName].toSVG(); + let icon = iconClass + ? octicons[iconFontName].toSVG({"class": iconClass}) + : octicons[iconFontName].toSVG(); + // ensure octicons are valid + if (!octicons.hasOwnProperty(iconFontName)) { + icon = ``; + } + return icon; } else if (iconFontType === 'octiconlight') { - return iconClass ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) - : octicons[iconFontName].toSVG({"style": "color: #fff;"}); + let icon = iconClass + ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) + : octicons[iconFontName].toSVG({"style": "color: #fff;"}); + // ensure octicons are valid + if (!octicons.hasOwnProperty(iconFontName)) { + icon = ``; + } + return icon; } else { // If icon is a Font Awesome icon return ``; } From c5d121f4d187f2a288b5766f342da72d6ba3e268 Mon Sep 17 00:00:00 2001 From: Kendrick Ang Date: Thu, 22 Oct 2020 22:12:48 +0800 Subject: [PATCH 5/5] fix incorrect logic --- .../src/lib/markdown-it/markdown-it-icons.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/markdown-it/markdown-it-icons.js b/packages/core/src/lib/markdown-it/markdown-it-icons.js index 8e200f5377..96dde1dcf2 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-icons.js +++ b/packages/core/src/lib/markdown-it/markdown-it-icons.js @@ -10,23 +10,21 @@ module.exports = require('markdown-it-regexp')( if (iconFontType === 'glyphicon') { return ``; } else if (iconFontType === 'octicon') { - let icon = iconClass - ? octicons[iconFontName].toSVG({"class": iconClass}) - : octicons[iconFontName].toSVG(); // ensure octicons are valid if (!octicons.hasOwnProperty(iconFontName)) { - icon = ``; + return ``; } - return icon; + return iconClass + ? octicons[iconFontName].toSVG({"class": iconClass}) + : octicons[iconFontName].toSVG(); } else if (iconFontType === 'octiconlight') { - let icon = iconClass - ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) - : octicons[iconFontName].toSVG({"style": "color: #fff;"}); // ensure octicons are valid if (!octicons.hasOwnProperty(iconFontName)) { - icon = ``; + return ``; } - return icon; + return iconClass + ? octicons[iconFontName].toSVG({"style": "color: #fff;", "class": iconClass}) + : octicons[iconFontName].toSVG({"style": "color: #fff;"}); } else { // If icon is a Font Awesome icon return ``; }