-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.js
127 lines (108 loc) · 3.48 KB
/
preview.js
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
121
122
123
124
125
126
127
const express = require('express');
const { codePointToUnicode } = require('./lib/conversions');
const { shortnameToUnicode, unicodeToShortname } = require('./lib/utils');
const { render: renderEmoji } = require('./lib');
const { getVersion } = require('./tasks/utils/unicode');
const { getUnicodeSpec } = require('./tasks/utils/data');
const { collection } = require('./vendor/emojis.json');
const PORT = 3000;
const app = express();
const renderOptions = { cdn: '/images', className: 'emoji' };
const previewSize = 20;
const renderHTML = (content) => `
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Emoji preview</title>
<style>
html {
font-size: ${previewSize}px;
}
nav {
margin: 60px auto;
text-align: center;
}
nav a {
margin: 10px;
padding: 10px;
display: block;
}
article {
margin-top: 60px;
}
.emoji {
width: ${previewSize}px;
}
</style>
</head>
<body>
${content}
</body>
</html>
`;
const renderList = (list) => (
list.map(([title, items]) => `
<article>
<h2>${title}</h2>
<p>${items.join(' ')}</p>
</article>
`).join('\n')
);
const renderSuggestables = () => {
const filtered = collection.filter(({ suggest }) => suggest);
const unicodeList = filtered.map(({ shortname }) => shortnameToUnicode(shortname));
const imagesList = filtered.map(({ hex }) => (
renderEmoji(codePointToUnicode(hex), renderOptions)
));
const sections = [
['Unicode output', unicodeList],
['Unicode conversion', imagesList],
];
return renderHTML(renderList(sections));
};
const renderCollection = () => {
const unicodeList = collection.map(({ shortname }) => shortnameToUnicode(shortname));
const imagesList = collection.map(({ hex }) => (
renderEmoji(codePointToUnicode(hex), renderOptions)
));
const sections = [
['Unicode output', unicodeList],
['Unicode conversion', imagesList],
];
return renderHTML(renderList(sections));
};
const handleSpec = async (req, res) => {
const spec = await getUnicodeSpec(getVersion());
const specUnicode = spec.map(({ unicode }) => unicode);
const specUnicodeImages = specUnicode.map((unicode) => renderEmoji(unicode, renderOptions));
const specCodePointsImages = spec.map(({ codePoint }) => (
renderEmoji(codePointToUnicode(codePoint), renderOptions)
));
const shortnamesImages = specUnicode
.map(unicodeToShortname)
.map(shortnameToUnicode)
.map((unicode) => renderEmoji(unicode, renderOptions));
const sections = [
['Unicode output', specUnicode],
['Unicode conversion', specUnicodeImages],
['Codepoints conversion', specCodePointsImages],
['Shortnames conversion', shortnamesImages],
];
const html = renderHTML(renderList(sections));
res.send(html);
};
const pages = `
<nav>
<a href="/suggestable">Suggestables</a>
<a href="/collection">Collection</a>
<a href="/spec">Spec</a>
</nav>
`;
app.get('/', (req, res) => res.send(renderHTML(pages)));
app.get('/suggestable', (req, res) => res.send(renderSuggestables()));
app.get('/collection', (req, res) => res.send(renderCollection()));
app.get('/spec', handleSpec);
app.use('/images', express.static('node_modules/emoji-assets/png/128'));
app.get('*', (req, res) => res.status(404).send('Page doesn\'t exist'));
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}!`));