generated from canisminor1990/canisminor-template
-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3827300
commit c234534
Showing
8 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ', '')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters