UNPKG

@cliz/translate-weekly

Version:

Docschina weekly translate tool

49 lines (48 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.link = exports.fmt = void 0; const cli_1 = require("@cliz/cli"); const remark = require("remark"); const visit = require("unist-util-visit"); const real_url_1 = require("@znode/real-url"); async function fmt(logger) { const prettierConfigPath = cli_1.api.path.join(__dirname, '../../config/prettier.config.js'); await cli_1.api.$ `yarn prettier --write 'docs/**/*.{md,mdx}' --config ${prettierConfigPath}`; logger.info('✅ fmt done'); } exports.fmt = fmt; async function link(period, logger) { logger.info(`fmt link start (period: ${period})`); const filePath = cli_1.api.path.join(process.cwd(), `docs/${period}.md`); const mdContent = await cli_1.api.fs.readFile(filePath, 'utf-8'); await remark() .use(changeLink) .use({ settings: { bullet: '-', listItemIndent: 'one', rule: '-' } }) .process(mdContent) .then(res => { return cli_1.api.fs.writeFile(filePath, String(res)); }); logger.info('✅ fmt link done'); function changeLink() { return async function transformer(tree, file) { const promises = []; visit(tree, 'link', visitor); await Promise.all(promises); return null; function visitor(node) { if (node.url) { const request = (0, real_url_1.default)(node.url) .then(realUrl => { logger.info(`${node.url} => ${realUrl}`); node.url = realUrl; }) .catch(error => { logger.error(`Fallback origin url => Cannot get real url for ${node.url}`); }); promises.push(request); } } }; } } exports.link = link;