Skip to content

Commit

Permalink
fix: read unicode from lightweight text source (ikatyang#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
thcrack authored Jul 21, 2023
1 parent 8341846 commit 44790ba
Show file tree
Hide file tree
Showing 8 changed files with 1,097 additions and 652 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,8 +1576,9 @@ This cheat sheet is automatically generated from [GitHub Emoji API](https://api.

| | ico | shortcode | ico | shortcode | |
| - | :-: | - | :-: | - | - |
| [top](#github-custom-emoji) | :atom: | `:atom:` | :basecamp: | `:basecamp:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :basecampy: | `:basecampy:` | :bowtie: | `:bowtie:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :accessibility: | `:accessibility:` | :atom: | `:atom:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :basecamp: | `:basecamp:` | :basecampy: | `:basecampy:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :bowtie: | `:bowtie:` | :dependabot: | `:dependabot:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :electron: | `:electron:` | :feelsgood: | `:feelsgood:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :finnadie: | `:finnadie:` | :fishsticks: | `:fishsticks:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :goberserk: | `:goberserk:` | :godmode: | `:godmode:` | [top](#table-of-contents) |
Expand All @@ -1586,3 +1587,4 @@ This cheat sheet is automatically generated from [GitHub Emoji API](https://api.
| [top](#github-custom-emoji) | :rage2: | `:rage2:` | :rage3: | `:rage3:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :rage4: | `:rage4:` | :shipit: | `:shipit:` | [top](#table-of-contents) |
| [top](#github-custom-emoji) | :suspect: | `:suspect:` | :trollface: | `:trollface:` | [top](#table-of-contents) |

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
"generate": "node ./scripts/generate.js > ./README.md"
},
"dependencies": {
"cheerio": "^0.22.0",
"request": "^2.88.0"
},
"devDependencies": {
"@types/cheerio": "0.22.13",
"@types/jest": "24.0.18",
"@types/node": "12.7.4",
"@types/request": "2.48.2",
Expand Down
500 changes: 458 additions & 42 deletions scripts/__playbacks__/api.github.com/get+emojis+29da119697.nock.json

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

50 changes: 19 additions & 31 deletions scripts/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const $ = require("cheerio");
const request = require("request");

/**
Expand Down Expand Up @@ -32,41 +31,30 @@ async function getGithubEmojiIdMap() {
}

async function getUnicodeEmojiCategoryIterator() {
return getUnicodeEmojiCategoryIteratorFromHtmlText(
await fetch("https://unicode.org/emoji/charts/full-emoji-list.html")
return getUnicodeEmojiCategoryIteratorFromText(
await fetch("https://unicode.org/emoji/charts/full-emoji-list.txt")
);
}

/**
* @param {string} htmlText
* @param {string} text
*/
function* getUnicodeEmojiCategoryIteratorFromHtmlText(htmlText) {
const $html = $.load(htmlText).root();
const $trs = $html
.find("table")
.children()
.toArray();
for (const $tr of $trs) {
if ($tr.firstChild.tagName === "th") {
if ($tr.firstChild.attribs.class === "bighead") {
const value = $tr.firstChild.firstChild.firstChild.nodeValue;
yield { type: "category", value };
} else if ($tr.firstChild.attribs.class === "mediumhead") {
const value = $tr.firstChild.firstChild.firstChild.nodeValue;
yield { type: "subcategory", value };
} else {
// skip column titles
}
} else if ($tr.firstChild.tagName === "td") {
if ($tr.children[4].attribs.class === "chars") {
yield { type: "emoji", value: $tr.children[4].firstChild.nodeValue };
} else {
throw new Error(`Unexpected situation.`);
}
} else {
throw new Error(
`Unexpected tagName ${JSON.stringify($tr.firstChild.tagName)}`
);
function* getUnicodeEmojiCategoryIteratorFromText(text) {
const lines = text.split("\n");
for (const line of lines) {
if (line.startsWith("@@")) {
const value = line.substring(2);
yield { type: "category", value };
} else if (line.startsWith("@")) {
const value = line.substring(1);
yield { type: "subcategory", value };
} else if (line.length) {
const value = line
.split("\t")[0]
.split(" ")
.map(_ => String.fromCodePoint(parseInt(_, 16)))
.join("");
yield { type: "emoji", value };
}
}
}
Expand Down
940 changes: 584 additions & 356 deletions scripts/generate.test.js

Large diffs are not rendered by default.

Loading

0 comments on commit 44790ba

Please sign in to comment.