Skip to content

Commit

Permalink
fix(icon): fix github shortcode issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Nov 5, 2021
1 parent f3a3a92 commit b90ab97
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"fast-glob": "^3.2.7",
"fs-extra": "^10.0.0",
"json": "^11.0.0",
"node-emoji": "^1.11.0",
"node-emoji": "github:rhysd/node-emoji#fix-gh-short-codes",
"obsidian": "^0.12.17",
"prettier": "^2.4.1",
"release-it": "^14.11.6",
Expand Down
49 changes: 3 additions & 46 deletions src/modules/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,15 @@ const RE_DASH = /-/g;
import emojiByName from "node-emoji/lib/emoji.json";

const IconSCs: string[] = [...Object.keys(icons)];
const EmojiSCs: string[] = ([...Object.keys(emojiByName)] as string[]).map(
(key) => {
if (key.startsWith("man-")) {
return `${key.substring(4).replace(RE_DASH, "_")}_man`;
} else if (key.startsWith("woman-")) {
return `${key.substring(6).replace(RE_DASH, "_")}_woman`;
} else return key;
},
);
const EmojiSCs: string[] = [...Object.keys(emojiByName)];
export const Shortcodes = ([] as string[]).concat(IconSCs, EmojiSCs);

/** Workaround for #19. :man-*: and :woman-*: are now :*_man: and :*_woman: on GitHub. node-emoji
* does not support the new short codes. Convert new to old.
* TODO: Remove this workaround when this PR is merged and shipped: https://github.com/omnidan/node-emoji/pull/112
*/
const patch = (matchKey: string, gotEmoji: string) => {
if (matchKey.endsWith("_man:") && gotEmoji === matchKey) {
// :foo_bar_man: -> man-foo-bar
const old = "man-" + matchKey.slice(1, -5).replace(RE_UNDERSTORE, "-");
const s = emoji.get(old);
if (s !== old) {
gotEmoji = s;
}
} else if (matchKey.endsWith("_woman:") && gotEmoji === matchKey) {
// :foo_bar_woman: -> woman-foo-bar
const old = "woman-" + matchKey.slice(1, -7).replace(RE_UNDERSTORE, "-");
const s = emoji.get(old);
if (s !== old) {
gotEmoji = s;
}
}
return gotEmoji;
};

/**
* @returns html or emoji text
*/
export const getIcon = (key: string): string | HTMLElement | null => {
key = stripColons(key);
const got = patch(key, emoji.get(key));
const got = emoji.get(key);
if (stripColons(got) !== key) return got;
if (key in icons) {
const src = icons[key as keyof typeof icons];
Expand All @@ -65,19 +34,7 @@ export const getIcon = (key: string): string | HTMLElement | null => {
return null;
};

export const isEmoji = (key: string): boolean => {
let result = emoji.hasEmoji(key);
if (result) return true;
if (key.endsWith("_man:")) {
// :foo_bar_man: -> man-foo-bar
const old = "man-" + key.slice(1, -5).replace(RE_UNDERSTORE, "-");
return emoji.hasEmoji(old);
} else if (key.endsWith("_woman:")) {
// :foo_bar_woman: -> woman-foo-bar
const old = "woman-" + key.slice(1, -7).replace(RE_UNDERSTORE, "-");
return emoji.hasEmoji(old);
} else return false;
};
export const isEmoji = (key: string): boolean => emoji.hasEmoji(key);

/**
* Removes colons on either side
Expand Down

0 comments on commit b90ab97

Please sign in to comment.