diff --git a/packages/lobe-i18n/README.md b/packages/lobe-i18n/README.md index a6b6ccf..a198793 100644 --- a/packages/lobe-i18n/README.md +++ b/packages/lobe-i18n/README.md @@ -301,6 +301,7 @@ $ lobe-i18n | outputExtensions | | `function` | `(locale) => '.{locale}.md'` | Output file extension generation | | mode | | `string`,`mdast`,`function` | `string` | Translation mode selection, explained below | | translateCode | | `boolean` | `false` | Whether to translate code blocks under `mdast`, other modes are invalid | +| includeMatter | | `boolean` | `false` | Whether to include front matter in the translation | #### `outputExtensions` diff --git a/packages/lobe-i18n/README.zh-CN.md b/packages/lobe-i18n/README.zh-CN.md index d22e0af..fb036ac 100644 --- a/packages/lobe-i18n/README.zh-CN.md +++ b/packages/lobe-i18n/README.zh-CN.md @@ -302,6 +302,7 @@ $ lobe-i18n | outputExtensions | | `function` | `(locale) => '.{locale}.md'` | 输出文件的扩展名生成 | | mode | | `string`,`mdast`,`function` | `string` | 翻译的模式选择,解释见下文 | | translateCode | | `boolean` | `false` | 在 `mdast` 下是否翻译代码块,其他模式无效 | +| includeMatter | | `boolean` | `false` | 是否翻译 `front matter` | #### `outputExtensions` diff --git a/packages/lobe-i18n/package.json b/packages/lobe-i18n/package.json index dd00dc9..9422ef7 100644 --- a/packages/lobe-i18n/package.json +++ b/packages/lobe-i18n/package.json @@ -67,6 +67,7 @@ "fast-deep-equal": "^3", "glob": "^10", "gpt-tokenizer": "^2", + "gray-matter": "^4", "ink": "^4.2", "json-stable-stringify": "^1", "just-diff": "^6", diff --git a/packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx b/packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx index 631eda1..3e593c4 100644 --- a/packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx +++ b/packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx @@ -2,6 +2,7 @@ import { alert, render } from '@lobehub/cli-ui'; import chalk from 'chalk'; import { consola } from 'consola'; import { globSync } from 'glob'; +import matter from 'gray-matter'; import { isString } from 'lodash-es'; import { existsSync } from 'node:fs'; import { relative, resolve } from 'node:path'; @@ -94,7 +95,12 @@ class TranslateMarkdown { clear(); const outputPath = relative('.', item.filename); if (data?.result && Object.keys(data.result).length > 0) { - writeMarkdown(item.filename, data.result); + let mdResut = data.result; + if (!this.markdownConfig.includeMatter) { + mdResut = matter.stringify(data.result, item.matter); + } + + writeMarkdown(item.filename, mdResut); totalTokenUsage += data.tokenUsage; consola.success(chalk.yellow(outputPath), chalk.gray(`[Token usage: ${data.tokenUsage}]`)); } else { @@ -120,11 +126,13 @@ class TranslateMarkdown { const targetFilename = this.getTargetFilename(file, targetExtension); if (existsSync(targetFilename)) continue; const mode = this.getMode(file, md); + const { data, content } = matter(md); consola.info(`📄 To ${locale}: ${chalk.yellow(file)}`); this.query.push({ filename: targetFilename, from: config.entryLocale, - md, + matter: data, + md: this.markdownConfig.includeMatter ? md : content, mode, to: locale, }); diff --git a/packages/lobe-i18n/src/core/I18n.ts b/packages/lobe-i18n/src/core/I18n.ts index 62c352f..a050b1e 100644 --- a/packages/lobe-i18n/src/core/I18n.ts +++ b/packages/lobe-i18n/src/core/I18n.ts @@ -33,6 +33,7 @@ export interface I18nTranslateOptions { export interface I18nMarkdownTranslateOptions extends Pick { + matter?: any; md: string; mode: MarkdownModeType; } diff --git a/packages/lobe-i18n/src/types/config.ts b/packages/lobe-i18n/src/types/config.ts index 79e09e7..d60b0d0 100644 --- a/packages/lobe-i18n/src/types/config.ts +++ b/packages/lobe-i18n/src/types/config.ts @@ -67,6 +67,10 @@ export interface MarkdownConfig { * @description The markdown that will ignore, support glob */ exclude?: string[]; + /** + * @description Whether to include matter in the translation + */ + includeMatter?: boolean; /** * @description Markdown translate mode */