forked from ikatyang/emoji-cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.ts
120 lines (97 loc) · 3.09 KB
/
markdown.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { name as repoName, repository } from '../package.json'
const RESOURCE_1 = '[GitHub Emoji API](https://api.github.com/emojis)'
const RESOURCE_2 =
'[Unicode Full Emoji List](https://unicode.org/emoji/charts/full-emoji-list.html)'
const COLUMNS = 2
const TOC_NAME = 'Table of Contents'
type GithubEmojiIds = Array<string[]>
export function generateCheatSheet(categorizedGithubEmojiIds: {
[category: string]: { [subCategory: string]: GithubEmojiIds }
}) {
const lineTexts = []
lineTexts.push(`# ${repoName}`)
lineTexts.push('')
lineTexts.push(
`[](https://github.com/${repository}/actions?query=workflow%3A%22Up+to+Date%22)`,
)
lineTexts.push('')
lineTexts.push(
`This cheat sheet is automatically generated from ${RESOURCE_1} and ${RESOURCE_2}.`,
)
lineTexts.push('')
const categories = Object.keys(categorizedGithubEmojiIds)
lineTexts.push(`## ${TOC_NAME}`)
lineTexts.push('')
lineTexts.push(...generateToc(categories))
lineTexts.push('')
for (const category of categories) {
lineTexts.push(`### ${category}`)
lineTexts.push('')
const subCategorizedGithubEmojiIds = categorizedGithubEmojiIds[category]
const subCategories = Object.keys(subCategorizedGithubEmojiIds)
if (subCategories.length > 1) {
lineTexts.push(...generateToc(subCategories))
lineTexts.push('')
}
for (const subCategory of subCategories) {
if (subCategory) {
lineTexts.push(`#### ${subCategory}`)
lineTexts.push('')
}
lineTexts.push(
...generateTable(
subCategorizedGithubEmojiIds[subCategory],
`[top](#${getHeaderId(category)})`,
`[top](#${getHeaderId(TOC_NAME)})`,
),
)
lineTexts.push('')
}
}
return lineTexts.join('\n')
}
function generateToc(headers: string[]) {
return headers.map(header => `- [${header}](#${getHeaderId(header)})`)
}
function getHeaderId(header: string) {
return header
.toLowerCase()
.replace(/ /g, '-')
.replace(/[^a-z0-9-]/g, '')
}
function generateTable(
githubEmojiIds: GithubEmojiIds,
leftText: string,
rightText: string,
) {
const lineTexts = []
let header = ''
let delimiter = ''
header += '| '
delimiter += '| - '
for (let i = 0; i < COLUMNS && i < githubEmojiIds.length; i++) {
header += `| ico | shortcode `
delimiter += '| :-: | - '
}
header += '| |'
delimiter += '| - |'
lineTexts.push(header, delimiter)
for (let i = 0; i < githubEmojiIds.length; i += COLUMNS) {
let lineText = `| ${leftText} `
for (let j = 0; j < COLUMNS; j++) {
if (i + j < githubEmojiIds.length) {
const emojiIds = githubEmojiIds[i + j]
const emojiId = emojiIds[0]
lineText += `| :${emojiId}: | \`:${emojiId}:\` `
for (let k = 1; k < emojiIds.length; k++) {
lineText += `<br /> \`:${emojiIds[k]}:\` `
}
} else if (githubEmojiIds.length > COLUMNS) {
lineText += '| | '
}
}
lineText += `| ${rightText} |`
lineTexts.push(lineText)
}
return lineTexts
}