mirror of
https://gitee.com/docsifyjs/docsify.git
synced 2024-12-01 19:50:32 +08:00
f5412dc7b0
* Update linting configuration (eslint, prettier) * Fix lint issues following eslint prettier update * Change ESLint config to allow boolean coercion * Switch to default import name per docs * Fix suppression of error details * Update JSDoc comments * Update waiForFunctin to provide error details --------- Co-authored-by: Koy Zhuang <koy@ko8e24.top>
104 lines
2.9 KiB
JavaScript
104 lines
2.9 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import axios from 'axios';
|
|
|
|
const filePaths = {
|
|
emojiMarkdown: path.resolve(process.cwd(), 'docs', 'emoji.md'),
|
|
emojiJS: path.resolve(
|
|
process.cwd(),
|
|
'src',
|
|
'core',
|
|
'render',
|
|
'emoji-data.js',
|
|
),
|
|
};
|
|
|
|
async function getEmojiData() {
|
|
const emojiDataURL = 'https://api.github.com/emojis';
|
|
|
|
console.info(`- Fetching emoji data from ${emojiDataURL}`);
|
|
|
|
const response = await axios.get(emojiDataURL);
|
|
const baseURL = Object.values(response.data)
|
|
.find(url => /unicode\//)
|
|
.split('unicode/')[0];
|
|
const data = { ...response.data };
|
|
|
|
// Remove base URL from emoji URLs
|
|
Object.entries(data).forEach(
|
|
([key, value]) => (data[key] = value.replace(baseURL, '')),
|
|
);
|
|
|
|
console.info(`- Retrieved ${Object.keys(data).length} emoji entries`);
|
|
|
|
return {
|
|
baseURL,
|
|
data,
|
|
};
|
|
}
|
|
|
|
function writeEmojiPage(emojiData) {
|
|
const isExistingPage = fs.existsSync(filePaths.emojiMarkdown);
|
|
const emojiPage =
|
|
(isExistingPage && fs.readFileSync(filePaths.emojiMarkdown, 'utf8')) ||
|
|
'<!-- START -->\n\n<!-- END -->';
|
|
const emojiRegEx = /(<!--\s*START.*-->\n)([\s\S]*)(\n<!--\s*END.*-->)/;
|
|
const emojiMatch = emojiPage.match(emojiRegEx);
|
|
const emojiMarkdownStart = emojiMatch[1].trim();
|
|
const emojiMarkdown = emojiMatch[2].trim();
|
|
const emojiMarkdownEnd = emojiMatch[3].trim();
|
|
const newEmojiMarkdown = Object.keys(emojiData.data)
|
|
.reduce(
|
|
(preVal, curVal) =>
|
|
(preVal += `:${curVal}: ` + '`' + `:${curVal}:` + '`' + '\n\n'),
|
|
'',
|
|
)
|
|
.trim();
|
|
|
|
if (emojiMarkdown !== newEmojiMarkdown) {
|
|
const newEmojiPage = emojiPage.replace(
|
|
emojiMatch[0],
|
|
`${emojiMarkdownStart}\n\n${newEmojiMarkdown}\n\n${emojiMarkdownEnd}`,
|
|
);
|
|
|
|
fs.writeFileSync(filePaths.emojiMarkdown, newEmojiPage);
|
|
|
|
console.info(
|
|
`- ${!isExistingPage ? 'Created' : 'Updated'}: ${filePaths.emojiMarkdown}`,
|
|
);
|
|
} else {
|
|
console.info(`- No changes: ${filePaths.emojiMarkdown}`);
|
|
}
|
|
}
|
|
|
|
function writeEmojiJS(emojiData) {
|
|
const isExistingPage = fs.existsSync(filePaths.emojiJS);
|
|
const emojiJS = isExistingPage && fs.readFileSync(filePaths.emojiJS, 'utf8');
|
|
const newEmojiJS = [
|
|
'/* eslint-disable */\n',
|
|
'// =============================================================================',
|
|
'// DO NOT EDIT: This file is auto-generated by an /build/emoji.js',
|
|
'// =============================================================================\n',
|
|
`export default ${JSON.stringify(emojiData, {}, 2)}`,
|
|
].join('\n');
|
|
|
|
if (!emojiJS || emojiJS !== newEmojiJS) {
|
|
fs.writeFileSync(filePaths.emojiJS, newEmojiJS);
|
|
|
|
console.info(
|
|
`- ${!isExistingPage ? 'Created' : 'Updated'}: ${filePaths.emojiJS}`,
|
|
);
|
|
} else {
|
|
console.info(`- No changes: ${filePaths.emojiJS}`);
|
|
}
|
|
}
|
|
|
|
console.info('Build emoji');
|
|
|
|
const emojiData = await getEmojiData();
|
|
|
|
writeEmojiPage(emojiData);
|
|
writeEmojiJS(emojiData);
|
|
|
|
console.info('Finish update');
|