Skip to content

Commit

Permalink
🔧 chore: Clean package
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 21, 2023
1 parent 3827300 commit c234534
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/lobe-cli-shebang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
"files": [
"dist"
],
"scripts": {
"build": "npm run type-check && pkgroll --minify -p tsconfig.prod.json --env.NODE_ENV=production && npm run shebang",
"dev": "pkgroll -p tsconfig.prod.json --env.NODE_ENV=development --watch",
"link": "npm run build && npm link -f",
"prepare": "npm run build",
"shebang": "node ./dist/cli.js -t ./dist/cli.js",
"start": "node ./dist/cli.js",
"test": "vitest --passWithNoTests",
"test:coverage": "vitest run --coverage --passWithNoTests",
"type-check": "tsc --noEmit"
},
"dependencies": {
"commander": "^11",
"consola": "^3",
Expand Down
14 changes: 13 additions & 1 deletion packages/lobe-cli-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lobehub/cli-ui",
"version": "1.9.1",
"version": "1.9.2",
"description": "Lobe CLI uikits",
"homepage": "https://github.comlobehub/lobe-cli-toolbox/tree/master/packages/lobe-cli-ui",
"bugs": {
Expand All @@ -24,6 +24,18 @@
"files": [
"dist"
],
"scripts": {
"build": "npm run type-check && pkgroll --minify -p tsconfig.prod.json --env.NODE_ENV=production",
"dev": "pkgroll -p tsconfig.prod.json --env.NODE_ENV=development --watch",
"example": "NODE_NO_WARNINGS=1 node --loader=ts-node/esm",
"prepare": "npm run build",
"start": "node ./dist/cli.js",
"test": "npm run type-check",
"test:coverage": "vitest run --coverage --passWithNoTests",
"test:ui": "vitest --passWithNoTests",
"test:update": "vitest -u",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@inkjs/ui": "^1",
"arr-rotate": "^1",
Expand Down
12 changes: 11 additions & 1 deletion packages/lobe-commit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@
"files": [
"dist"
],
"scripts": {
"build": "npm run type-check && pkgroll --minify -p tsconfig.prod.json --env.NODE_ENV=production && npm run shebang",
"dev": "pkgroll -p tsconfig.prod.json --env.NODE_ENV=development --watch",
"link": "npm run build && npm link -f",
"shebang": "lobe-shebang -t ./dist/cli.js",
"start": "node ./dist/cli.js",
"test": "vitest --passWithNoTests",
"test:coverage": "vitest run --coverage --passWithNoTests",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@inkjs/ui": "^1",
"@lobehub/cli-ui": "1.9.1",
"@lobehub/cli-ui": "latest",
"chalk": "^5",
"commander": "^11",
"conf": "^12",
Expand Down
12 changes: 11 additions & 1 deletion packages/lobe-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@
"files": [
"dist"
],
"scripts": {
"build": "npm run type-check && pkgroll --minify -p tsconfig.prod.json --env.NODE_ENV=production && npm run shebang",
"dev": "pkgroll -p tsconfig.prod.json --env.NODE_ENV=development --watch",
"link": "npm run build && npm link -f",
"shebang": "lobe-shebang -t ./dist/cli.js",
"start": "node ./dist/cli.js",
"test": "vitest --passWithNoTests",
"test:coverage": "vitest run --coverage --passWithNoTests",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@inkjs/ui": "^1",
"@lobehub/cli-ui": "1.9.1",
"@lobehub/cli-ui": "latest",
"chalk": "^5",
"commander": "^11",
"conf": "^12",
Expand Down
48 changes: 48 additions & 0 deletions packages/lobe-i18n/src/core/TranslateMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { checkMdString } from '@/utils/checkMdString';
import {
convertMarkdownToMdast,
convertMdastToMarkdown,
convertMdastToPureObject,
convertPureObjectToMdast,
} from '@/utils/convertMarkdown';
import { findObjPaths } from '@/utils/findObjPaths';

export class TranslateMarkdown {
async run(md: string) {
// 第一步:将 markdown 转换为 mdast 对象
const mdast = await convertMarkdownToMdast(md);

// 第二步:将 markdown 转换为 纯对象
const mdPureObj = convertMdastToPureObject(mdast);

// 第三部:提取纯对象中 type 为 text 的节点, 并输出对象路径

// const mdLocale = this.genMdI18nLocale(mdPureObj);

// 第四部:翻译字符串,然后用 lodash set 将翻译后的字符串设置到纯对象中

// 第五部:将纯对象转换为 mdast 对象
const newMdast = convertPureObjectToMdast(mdPureObj);

// 第六部:将 mdast 对象转换为 markdown
return await convertMdastToMarkdown(newMdast);
}

// getLocale(md:string) {
//
// }

genMdI18nLocale(obj: { path: string; value: string }[]) {
const toc = findObjPaths(obj, 'text').filter(
(item) => !checkMdString(item.value, ['\n', 'Note', 'Important', 'Warn']),
);

const locale: { [key: string]: string } = {};

for (const item of toc) {
locale[item.path] = item.value;
}

return locale;
}
}
8 changes: 8 additions & 0 deletions packages/lobe-i18n/src/utils/checkMdString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const checkMdString = (str: string, features: string[]) => {
if (features.includes(str)) return true;
// 创建一个正则表达式,包含所有的标点符号、空格和emoji
// 表情的Unicode范围是:U+1F300到U+1F5FF,U+1F600到U+1F64F,U+1F680到U+1F6FF,U+1F700到U+1F77F
let regex =
/^[\s\p{P}\u{1F300}-\u{1F5FF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{1F700}-\u{1F77F}]*$/u;
return regex.test(str.replaceAll(' ', ''));
};
53 changes: 53 additions & 0 deletions packages/lobe-i18n/src/utils/convertMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { unified } from 'unified';

import { isNumeric } from '@/utils/isNumeric';

export const convertMarkdownToMdast = async (md: string) => {
return await unified().use(remarkParse).use(remarkGfm).parse(md);
};

export const convertMdastToMarkdown = async (json: any) => {
return await unified().use(remarkStringify).use(remarkGfm).stringify(json);
};

export const convertMdastToPureObject = (obj: any) => {
if (!obj || typeof obj !== 'object') {
return obj;
}

let newObj: any = Array.isArray(obj) ? {} : {};
for (let key in obj) {
if (key === 'children' && Array.isArray(obj[key])) {
newObj[key] = obj[key].length > 0 ? {} : [];
for (let i = 0; i < obj[key].length; i++) {
newObj[key][i] = convertMdastToPureObject(obj[key][i]);
}
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
newObj[key] = convertMdastToPureObject(obj[key]);
} else {
newObj[key] = obj[key];
}
}
return newObj;
};

export const convertPureObjectToMdast = (obj: any) => {
if (typeof obj !== 'object' || obj === null) {
return obj;
}

let newObj: any = Array.isArray(obj) ? [] : {};
for (let key in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(key)) {
if (isNumeric(key) && !Array.isArray(newObj)) {
newObj = Object.values(newObj);
}
newObj[key] = convertPureObjectToMdast(obj[key]);
}
}
return newObj;
};
6 changes: 1 addition & 5 deletions packages/lobe-label/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"build": "npm run type-check && pkgroll --minify -p tsconfig.prod.json --env.NODE_ENV=production && npm run shebang",
"dev": "pkgroll -p tsconfig.prod.json --env.NODE_ENV=development --watch",
"link": "npm run build && npm link -f",
"prepack": "clean-pkg-json",
"shebang": "lobe-shebang -t ./dist/cli.js",
"start": "node ./dist/cli.js",
"test": "vitest --passWithNoTests",
Expand All @@ -41,7 +40,7 @@
},
"dependencies": {
"@inkjs/ui": "^1",
"@lobehub/cli-ui": "1.9.1",
"@lobehub/cli-ui": "latest",
"@octokit/rest": "^20",
"commander": "^11",
"conf": "^12",
Expand All @@ -51,9 +50,6 @@
"react": "^18",
"update-notifier": "^7"
},
"devDependencies": {
"clean-pkg-json": "^1"
},
"peerDependencies": {
"ink": ">=4",
"react": ">=18"
Expand Down

0 comments on commit c234534

Please sign in to comment.