Skip to content

Commit ba5ee26

Browse files
feat: Emoji build (#1766)
* Fix incorrect file name * Improve build - Display emoji API URL - Display number of emoji entries retrieved from API - Distinguish between creating and updating files - Catch and display errors (gracefully fail for offline work) - Add “DO NOT EDIT” comment to generated output * Add emoji to automated build * Remove emoji plugin from dev index.html
1 parent 35002c9 commit ba5ee26

File tree

5 files changed

+1923
-1900
lines changed

5 files changed

+1923
-1900
lines changed

build/emoji.js

+35-17
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ const filePaths = {
99
'src',
1010
'core',
1111
'render',
12-
'emojify-data.js'
12+
'emoji-data.js'
1313
),
1414
};
1515

1616
async function getEmojiData() {
1717
const emojiDataURL = 'https://api.github.com/emojis';
18+
19+
console.info(`- Fetching emoji data from ${emojiDataURL}`);
20+
1821
const response = await axios.get(emojiDataURL);
1922
const baseURL = Object.values(response.data)
2023
.find(url => /unicode\//)
@@ -26,16 +29,18 @@ async function getEmojiData() {
2629
([key, value]) => (data[key] = value.replace(baseURL, ''))
2730
);
2831

32+
console.info(`- Retrieved ${Object.keys(data).length} emoji entries`);
33+
2934
return {
3035
baseURL,
3136
data,
3237
};
3338
}
3439

3540
function writeEmojiPage(emojiData) {
41+
const isExistingPage = fs.existsSync(filePaths.emojiMarkdown);
3642
const emojiPage =
37-
(fs.existsSync(filePaths.emojiMarkdown) &&
38-
fs.readFileSync(filePaths.emojiMarkdown, 'utf8')) ||
43+
(isExistingPage && fs.readFileSync(filePaths.emojiMarkdown, 'utf8')) ||
3944
`<!-- START -->\n\n<!-- END -->`;
4045
const emojiRegEx = /(<!--\s*START.*-->\n)([\s\S]*)(\n<!--\s*END.*-->)/;
4146
const emojiMatch = emojiPage.match(emojiRegEx);
@@ -53,39 +58,52 @@ function writeEmojiPage(emojiData) {
5358
if (emojiMarkdown !== newEmojiMarkdown) {
5459
const newEmojiPage = emojiPage.replace(
5560
emojiMatch[0],
56-
`${emojiMarkdownStart}\n${newEmojiMarkdown}\n${emojiMarkdownEnd}`
61+
`${emojiMarkdownStart}\n\n${newEmojiMarkdown}\n\n${emojiMarkdownEnd}`
5762
);
5863

5964
fs.writeFileSync(filePaths.emojiMarkdown, newEmojiPage);
60-
console.info(`- Created new file: ${filePaths.emojiMarkdown}`);
65+
66+
console.info(
67+
`- ${!isExistingPage ? 'Created' : 'Updated'}: ${filePaths.emojiMarkdown}`
68+
);
6169
} else {
62-
console.info(`- No changes to file: ${filePaths.emojiMarkdown}`);
70+
console.info(`- No changes: ${filePaths.emojiMarkdown}`);
6371
}
6472
}
6573

6674
function writeEmojiJS(emojiData) {
67-
const emojiJS =
68-
fs.existsSync(filePaths.emojiJS) &&
69-
fs.readFileSync(filePaths.emojiJS, 'utf8');
70-
const newEmojiJS = `export default ${JSON.stringify(emojiData, {}, 2)}`;
75+
const isExistingPage = fs.existsSync(filePaths.emojiJS);
76+
const emojiJS = isExistingPage && fs.readFileSync(filePaths.emojiJS, 'utf8');
77+
const newEmojiJS = [
78+
'/* eslint-disable */\n',
79+
'// =============================================================================',
80+
'// DO NOT EDIT: This file is auto-generated by an /build/emoji.js',
81+
'// =============================================================================\n',
82+
`export default ${JSON.stringify(emojiData, {}, 2)}`,
83+
].join('\n');
7184

7285
if (!emojiJS || emojiJS !== newEmojiJS) {
7386
fs.writeFileSync(filePaths.emojiJS, newEmojiJS);
74-
console.info(`- Created new file: ${filePaths.emojiJS}`);
87+
88+
console.info(
89+
`- ${!isExistingPage ? 'Created' : 'Updated'}: ${filePaths.emojiJS}`
90+
);
7591
} else {
76-
console.info(`- No changes to file: ${filePaths.emojiJS}`);
92+
console.info(`- No changes: ${filePaths.emojiJS}`);
7793
}
7894
}
7995

8096
(async () => {
81-
console.log('Build emoji');
97+
console.info('Build emoji');
8298

8399
try {
84100
const emojiData = await getEmojiData();
85101

86-
writeEmojiPage(emojiData);
87-
writeEmojiJS(emojiData);
88-
} catch (e) {
89-
console.error(e);
102+
if (emojiData) {
103+
writeEmojiPage(emojiData);
104+
writeEmojiJS(emojiData);
105+
}
106+
} catch (err) {
107+
console.warn(`- Error: ${err.message}`);
90108
}
91109
})();

docs/emoji.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Below is a complete list of emoji shorthand codes. Docsify can be configured to
44

55
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(15em, 1fr));">
66

7-
<!-- START: auto-generated emoji markdown (do not edit between these comments) -->
7+
<!-- START: Auto-generated content (/build/emoji.js) -->
88

99
:100: `:100:`
1010

@@ -3754,6 +3754,6 @@ Below is a complete list of emoji shorthand codes. Docsify can be configured to
37543754

37553755
:zzz: `:zzz:`
37563756

3757-
<!-- END: auto-generated emoji markdown -->
3757+
<!-- END: Auto-generated content (/build/emoji.js) -->
37583758

37593759
</div>

index.html

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
</script>
116116
<script src="/lib/docsify.js"></script>
117117
<script src="/lib/plugins/search.js"></script>
118-
<script src="/lib/plugins/emoji.js"></script>
119118
<script src="/lib/plugins/front-matter.js"></script>
120119
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-bash.min.js"></script>
121120
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-markdown.min.js"></script>

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"build:cover": "node build/cover.js",
2525
"build:css:min": "mkdirp lib/themes && npm run css -- -o lib/themes && node build/mincss.js",
2626
"build:css": "mkdirp themes && npm run css -- -o themes",
27-
"build:emoji": "node ./build/emoji.js && eslint ./src/core/render/emojify-data.js --fix --quiet",
27+
"build:emoji": "node ./build/emoji.js",
2828
"build:js": "cross-env NODE_ENV=production node build/build.js",
2929
"build:ssr": "node build/ssr.js",
3030
"build:test": "npm run build && npm test",
31-
"build": "rimraf lib themes && run-s build:js build:css build:css:min build:ssr build:cover",
31+
"build": "rimraf lib themes && run-s build:js build:css build:css:min build:ssr build:cover build:emoji",
3232
"css": "node build/css",
3333
"dev:ssr": "run-p serve:ssr watch:*",
3434
"dev": "run-p serve watch:*",

0 commit comments

Comments
 (0)