forked from ikatyang/emoji-cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,486 additions
and
2,325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
coverage/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Ika | ||
Copyright (c) Ika <[email protected]> (https://github.com/ikatyang) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,34 +2,38 @@ | |
"name": "emoji-cheat-sheet", | ||
"version": "0.0.0-dev", | ||
"private": true, | ||
"author": "ikatyang", | ||
"license": "MIT", | ||
"repository": "https://github.com/ikatyang/emoji-cheat-sheet", | ||
"repository": "ikatyang/emoji-cheat-sheet", | ||
"homepage": "https://github.com/ikatyang/emoji-cheat-sheet#readme", | ||
"author": { | ||
"name": "Ika", | ||
"email": "[email protected]", | ||
"url": "https://github.com/ikatyang" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"lint": "tslint -p ./tsconfig.json", | ||
"test": "jest -c ./jest.json", | ||
"generate": "ts-node ./scripts/generate.ts ./README.md" | ||
"lint": "tslint -p . --type-check", | ||
"test": "jest", | ||
"generate": "node ./scripts/generate.js > ./README.md" | ||
}, | ||
"dependencies": { | ||
"cheerio": "^0.22.0", | ||
"request": "^2.82.0" | ||
"dedent": "^0.7.0", | ||
"request": "^2.88.0" | ||
}, | ||
"devDependencies": { | ||
"@types/cheerio": "0.22.11", | ||
"@types/jest": "21.1.10", | ||
"@types/dedent": "0.7.0", | ||
"@types/jest": "24.0.11", | ||
"@types/node": "8.10.44", | ||
"@types/request": "2.48.1", | ||
"jest": "21.2.1", | ||
"jest-playback": "1.0.1", | ||
"prettier": "1.14.3", | ||
"prettier-config-ikatyang": "1.1.1", | ||
"ts-jest": "21.2.4", | ||
"ts-node": "4.1.0", | ||
"tslint": "5.14.0", | ||
"tslint-config-ikatyang": "2.5.1", | ||
"tslint-config-prettier": "1.18.0", | ||
"jest": "24.7.1", | ||
"jest-playback": "2.0.2", | ||
"prettier": "1.16.4", | ||
"tslint": "5.15.0", | ||
"tslint-plugin-prettier": "2.0.1", | ||
"typescript": "2.9.2" | ||
"typescript": "3.4.2" | ||
}, | ||
"engines": { | ||
"node": ">= 8" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
yarn run generate | ||
yarn generate | ||
|
||
git config --global user.name ikatyang-bot | ||
git config --global user.email [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
const $ = require("cheerio"); | ||
const dedent = require("dedent"); | ||
const request = require("request"); | ||
const packageJson = require("../package.json"); | ||
|
||
const apiUrl = "https://api.github.com/emojis"; | ||
const sheetUrl = "http://www.emoji-cheat-sheet.com"; | ||
|
||
const columns = 2; | ||
|
||
/** | ||
* @typedef {string} EmojiId | ||
* @typedef {{ [category: string]: EmojiId[] }} EmojiData | ||
*/ | ||
|
||
const tocName = "Table of Contents"; | ||
const topName = "top"; | ||
const topHref = "#table-of-contents"; | ||
|
||
async function generateCheatSheet() { | ||
return buildTable(await getData()); | ||
} | ||
|
||
/** | ||
* @returns {Promise<EmojiData>} | ||
*/ | ||
async function getData() { | ||
const apiHtml = await fetchHtml(apiUrl); | ||
const sheetHtml = await fetchHtml(sheetUrl); | ||
|
||
const apiJson = /** @type {Record<EmojiId, string>} */ (JSON.parse(apiHtml)); | ||
|
||
const emojiIds = Object.keys(apiJson); | ||
const emojiData = /** @type {EmojiData} */ ({}); | ||
|
||
const $html = $.load(sheetHtml).root(); | ||
$html.find("h2").each((_, $category) => { | ||
const localEmojiIds = /** @type {string[]} */ ([]); | ||
const category = $($category).text(); | ||
$html | ||
.find(`#emoji-${category.toLowerCase()} li .name`) | ||
.each((_, $emoji) => { | ||
const emoji = $($emoji).text(); | ||
const index = emojiIds.indexOf(emoji); | ||
if (index !== -1) { | ||
localEmojiIds.push(...emojiIds.splice(index, 1)); | ||
} | ||
}); | ||
emojiData[category] = localEmojiIds; | ||
}); | ||
|
||
if (emojiIds.length !== 0) { | ||
emojiData["Uncategorized"] = emojiIds; | ||
} | ||
|
||
return emojiData; | ||
} | ||
|
||
/** | ||
* @param {EmojiData} emojiData | ||
* @returns {string} | ||
*/ | ||
function buildTable(emojiData) { | ||
const travisRepoUrl = `https://travis-ci.org/${packageJson.repository}`; | ||
const travisBadgeUrl = `${travisRepoUrl}.svg?branch=master`; | ||
const categories = Object.keys(emojiData); | ||
return dedent(` | ||
# ${packageJson.name} | ||
[![build](${travisBadgeUrl})](${travisRepoUrl}) | ||
This cheat sheet is automatically generated from ${[ | ||
["GitHub Emoji API", apiUrl], | ||
["Emoji Cheat Sheet", sheetUrl] | ||
] | ||
.map(([siteName, siteUrl]) => `[${siteName}](${siteUrl})`) | ||
.join(" and ")}. | ||
## ${tocName} | ||
${categories | ||
.map(category => `- [${category}](#${category.toLowerCase()})`) | ||
.join("\n")} | ||
${categories | ||
.map(category => { | ||
const emojis = emojiData[category]; | ||
return dedent(` | ||
### ${category} | ||
${buildTableHead()} | ||
${buildTableContent(emojis)} | ||
`); | ||
}) | ||
.join("\n".repeat(2))} | ||
`); | ||
} | ||
|
||
/** | ||
* @param {string[]} emojis | ||
*/ | ||
function buildTableContent(emojis) { | ||
let tableContent = ""; | ||
for (let i = 0; i < emojis.length; i += columns) { | ||
const rowEmojis = emojis.slice(i, i + columns); | ||
while (rowEmojis.length < columns) { | ||
rowEmojis.push(""); | ||
} | ||
tableContent += `| [${topName}](${topHref}) |${rowEmojis | ||
.map(x => (x.length !== 0 ? ` :${x}: | \`:${x}:\` ` : " | ")) | ||
.join("|")}|\n`; | ||
} | ||
return tableContent; | ||
} | ||
|
||
function buildTableHead() { | ||
return dedent(` | ||
| |${" ico | emoji |".repeat(columns)} | ||
| - |${" --- | ----- |".repeat(columns)} | ||
`); | ||
} | ||
|
||
/** | ||
* @param {string} url | ||
* @returns {Promise<string>} | ||
*/ | ||
async function fetchHtml(url) { | ||
return new Promise((resolve, reject) => { | ||
const options = /** @type {request.Options} */ ({ url }); | ||
if (url === apiUrl) { | ||
options.headers = { | ||
"User-Agent": "https://github.com/ikatyang/emoji-cheat-sheet" | ||
}; | ||
} | ||
request.get(options, (error, response, html) => { | ||
if (!error && response.statusCode === 200) { | ||
resolve(html); | ||
} else { | ||
reject( | ||
error | ||
? error | ||
: `Unexpected response status code: ${response.statusCode}` | ||
); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
if (require.main === /** @type {unknown} */ (module)) { | ||
generateCheatSheet().then(cheatSheet => console.log(cheatSheet)); | ||
} else { | ||
module.exports = generateCheatSheet; | ||
} |
Oops, something went wrong.