valaxy 0.26.8 → 0.26.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node/cli/index.mjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import 'node:process';
|
|
2
2
|
import 'yargs';
|
|
3
3
|
import 'yargs/helpers';
|
|
4
|
-
export { c as cli, d as registerDevCommand, r as run, a as startValaxyDev } from '../../shared/valaxy.
|
|
4
|
+
export { c as cli, d as registerDevCommand, r as run, a as startValaxyDev } from '../../shared/valaxy.B5JLGXpW.mjs';
|
|
5
|
+
import 'node:os';
|
|
5
6
|
import 'node:path';
|
|
6
7
|
import 'consola';
|
|
7
8
|
import 'consola/utils';
|
|
8
9
|
import 'fast-glob';
|
|
9
10
|
import 'fs-extra';
|
|
10
11
|
import 'gray-matter';
|
|
12
|
+
import 'js-yaml';
|
|
11
13
|
import '@antfu/utils';
|
|
12
14
|
import 'debug';
|
|
13
15
|
import 'pathe';
|
|
@@ -20,7 +22,6 @@ import 'ora';
|
|
|
20
22
|
import 'cross-spawn';
|
|
21
23
|
import 'mlly';
|
|
22
24
|
import 'resolve-global';
|
|
23
|
-
import 'js-yaml';
|
|
24
25
|
import 'node:fs/promises';
|
|
25
26
|
import 'dayjs';
|
|
26
27
|
import 'feed';
|
|
@@ -56,7 +57,6 @@ import 'pascalcase';
|
|
|
56
57
|
import 'lru-cache';
|
|
57
58
|
import 'html-to-text';
|
|
58
59
|
import 'unplugin-vue-router/vite';
|
|
59
|
-
import 'node:os';
|
|
60
60
|
import '@clack/prompts';
|
|
61
61
|
import 'node:net';
|
|
62
62
|
import 'node:child_process';
|
package/dist/node/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { C as ALL_ROUTE, E as EXCERPT_SEPARATOR, G as GLOBAL_STATE, P as PATHNAME_PROTOCOL_RE, V as ViteValaxyPlugins, b as build, c as cli, N as createServer, L as createValaxyPlugin, D as customElements, j as defaultSiteConfig, w as defaultValaxyConfig, F as defaultViteConfig, h as defineAddon, y as defineConfig, k as defineSiteConfig, u as defineTheme, f as defineValaxyAddon, x as defineValaxyConfig, t as defineValaxyTheme, O as encryptContent, g as generateClientRedirects, Q as getGitTimestamp, e as getIndexHtml, M as getServerInfoText, R as isExternal, U as isInstalledGlobally, S as isPath, v as loadConfigFromFile, A as mergeValaxyConfig, m as mergeViteConfigs, p as postProcessForSSG, I as processValaxyOptions, d as registerDevCommand, i as resolveAddonsConfig, Y as resolveImportPath, W as resolveImportUrl, J as resolveOptions, n as resolveSiteConfig, l as resolveSiteConfigFromRoot, o as resolveThemeConfigFromRoot, K as resolveThemeValaxyConfig, q as resolveUserThemeConfig, B as resolveValaxyConfig, z as resolveValaxyConfigFromRoot, r as run, s as ssgBuild, a as startValaxyDev, X as toAtFS, T as transformObject, H as version } from '../shared/valaxy.
|
|
1
|
+
export { C as ALL_ROUTE, E as EXCERPT_SEPARATOR, G as GLOBAL_STATE, P as PATHNAME_PROTOCOL_RE, V as ViteValaxyPlugins, b as build, c as cli, N as createServer, L as createValaxyPlugin, D as customElements, j as defaultSiteConfig, w as defaultValaxyConfig, F as defaultViteConfig, h as defineAddon, y as defineConfig, k as defineSiteConfig, u as defineTheme, f as defineValaxyAddon, x as defineValaxyConfig, t as defineValaxyTheme, O as encryptContent, g as generateClientRedirects, Q as getGitTimestamp, e as getIndexHtml, M as getServerInfoText, R as isExternal, U as isInstalledGlobally, S as isPath, v as loadConfigFromFile, A as mergeValaxyConfig, m as mergeViteConfigs, p as postProcessForSSG, I as processValaxyOptions, d as registerDevCommand, i as resolveAddonsConfig, Y as resolveImportPath, W as resolveImportUrl, J as resolveOptions, n as resolveSiteConfig, l as resolveSiteConfigFromRoot, o as resolveThemeConfigFromRoot, K as resolveThemeValaxyConfig, q as resolveUserThemeConfig, B as resolveValaxyConfig, z as resolveValaxyConfigFromRoot, r as run, s as ssgBuild, a as startValaxyDev, X as toAtFS, T as transformObject, H as version } from '../shared/valaxy.B5JLGXpW.mjs';
|
|
2
2
|
import 'node:path';
|
|
3
3
|
import 'fs-extra';
|
|
4
4
|
import 'consola/utils';
|
|
@@ -6,9 +6,11 @@ import 'node:process';
|
|
|
6
6
|
import 'define-config-ts';
|
|
7
7
|
import 'yargs';
|
|
8
8
|
import 'yargs/helpers';
|
|
9
|
+
import 'node:os';
|
|
9
10
|
import 'consola';
|
|
10
11
|
import 'fast-glob';
|
|
11
12
|
import 'gray-matter';
|
|
13
|
+
import 'js-yaml';
|
|
12
14
|
import '@antfu/utils';
|
|
13
15
|
import 'debug';
|
|
14
16
|
import 'pathe';
|
|
@@ -20,7 +22,6 @@ import 'ora';
|
|
|
20
22
|
import 'cross-spawn';
|
|
21
23
|
import 'mlly';
|
|
22
24
|
import 'resolve-global';
|
|
23
|
-
import 'js-yaml';
|
|
24
25
|
import 'node:fs/promises';
|
|
25
26
|
import 'dayjs';
|
|
26
27
|
import 'feed';
|
|
@@ -56,7 +57,6 @@ import 'pascalcase';
|
|
|
56
57
|
import 'lru-cache';
|
|
57
58
|
import 'html-to-text';
|
|
58
59
|
import 'unplugin-vue-router/vite';
|
|
59
|
-
import 'node:os';
|
|
60
60
|
import '@clack/prompts';
|
|
61
61
|
import 'node:net';
|
|
62
62
|
import 'node:child_process';
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
2
|
import yargs from 'yargs';
|
|
3
3
|
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import os from 'node:os';
|
|
4
5
|
import path$1, { join, dirname, resolve } from 'node:path';
|
|
5
6
|
import { consola } from 'consola';
|
|
6
7
|
import { colors } from 'consola/utils';
|
|
7
8
|
import fg from 'fast-glob';
|
|
8
9
|
import fs from 'fs-extra';
|
|
9
10
|
import matter from 'gray-matter';
|
|
11
|
+
import yaml, { CORE_SCHEMA } from 'js-yaml';
|
|
10
12
|
import { ensurePrefix, slash, uniq, objectEntries, isObject, isFunction, ensureSuffix } from '@antfu/utils';
|
|
11
13
|
import _debug from 'debug';
|
|
12
14
|
import path, { resolve as resolve$1, join as join$1, relative } from 'pathe';
|
|
@@ -19,7 +21,6 @@ import ora from 'ora';
|
|
|
19
21
|
import { spawn } from 'cross-spawn';
|
|
20
22
|
import { resolvePath } from 'mlly';
|
|
21
23
|
import { resolveGlobal } from 'resolve-global';
|
|
22
|
-
import yaml, { CORE_SCHEMA } from 'js-yaml';
|
|
23
24
|
import { readFile } from 'node:fs/promises';
|
|
24
25
|
import dayjs from 'dayjs';
|
|
25
26
|
import { Feed } from 'feed';
|
|
@@ -55,7 +56,6 @@ import pascalCase from 'pascalcase';
|
|
|
55
56
|
import { LRUCache } from 'lru-cache';
|
|
56
57
|
import { convert } from 'html-to-text';
|
|
57
58
|
import VueRouter from 'unplugin-vue-router/vite';
|
|
58
|
-
import os from 'node:os';
|
|
59
59
|
import { intro, confirm, select, outro } from '@clack/prompts';
|
|
60
60
|
import net from 'node:net';
|
|
61
61
|
import { exec } from 'node:child_process';
|
|
@@ -162,6 +162,7 @@ function getGitTimestamp(file, type = "updated") {
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;
|
|
165
|
+
const LOCALE_PREFIX = "$locale:";
|
|
165
166
|
|
|
166
167
|
function tObject(data, lang) {
|
|
167
168
|
if (data && typeof data === "object") {
|
|
@@ -1466,21 +1467,6 @@ function snippetPlugin(md, srcDir) {
|
|
|
1466
1467
|
md.block.ruler.before("fence", "snippet", parser);
|
|
1467
1468
|
}
|
|
1468
1469
|
|
|
1469
|
-
const globalTitleCollector = /* @__PURE__ */ new Set();
|
|
1470
|
-
function getGlobalTitleCollector() {
|
|
1471
|
-
return globalTitleCollector;
|
|
1472
|
-
}
|
|
1473
|
-
function titleCollectorPlugin(md) {
|
|
1474
|
-
const fence = md.renderer.rules.fence;
|
|
1475
|
-
md.renderer.rules.fence = (...args) => {
|
|
1476
|
-
const [tokens, idx] = args;
|
|
1477
|
-
const token = tokens[idx];
|
|
1478
|
-
const title = extractTitle(token.info);
|
|
1479
|
-
globalTitleCollector.add(title);
|
|
1480
|
-
return fence(...args);
|
|
1481
|
-
};
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
1470
|
const defaultCodeTheme = { light: "github-light", dark: "github-dark" };
|
|
1485
1471
|
async function setupMarkdownPlugins(md, options, base = "/") {
|
|
1486
1472
|
const mdOptions = options?.config.markdown || {};
|
|
@@ -1488,7 +1474,7 @@ async function setupMarkdownPlugins(md, options, base = "/") {
|
|
|
1488
1474
|
const siteConfig = options?.config.siteConfig || {};
|
|
1489
1475
|
if (mdOptions.preConfig)
|
|
1490
1476
|
mdOptions.preConfig(md);
|
|
1491
|
-
md.use(highlightLinePlugin).use(preWrapperPlugin, { theme, siteConfig }).use(snippetPlugin, options?.userRoot).use(
|
|
1477
|
+
md.use(highlightLinePlugin).use(preWrapperPlugin, { theme, siteConfig }).use(snippetPlugin, options?.userRoot).use(containerPlugin, {
|
|
1492
1478
|
languages: siteConfig.languages,
|
|
1493
1479
|
...mdOptions?.container,
|
|
1494
1480
|
blocks: {
|
|
@@ -1568,7 +1554,7 @@ async function setupMarkdownPlugins(md, options, base = "/") {
|
|
|
1568
1554
|
return md;
|
|
1569
1555
|
}
|
|
1570
1556
|
|
|
1571
|
-
const version = "0.26.
|
|
1557
|
+
const version = "0.26.9";
|
|
1572
1558
|
|
|
1573
1559
|
const GLOBAL_STATE = {
|
|
1574
1560
|
valaxyApp: void 0,
|
|
@@ -3513,65 +3499,18 @@ async function ViteValaxyPlugins(valaxyApp, serverOptions = {}) {
|
|
|
3513
3499
|
typedoc: "vscode-icons:file-type-typedoc",
|
|
3514
3500
|
eslint: "vscode-icons:file-type-eslint"
|
|
3515
3501
|
};
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
customIcon: {
|
|
3521
|
-
...builtinCustomIcon,
|
|
3522
|
-
...valaxyConfig.groupIcons?.customIcon
|
|
3523
|
-
},
|
|
3524
|
-
defaultLabels: [
|
|
3525
|
-
...valaxyConfig.groupIcons?.defaultLabels || [],
|
|
3526
|
-
...Object.keys(builtinCustomIcon),
|
|
3527
|
-
...Object.keys(valaxyConfig.groupIcons?.customIcon || {}),
|
|
3528
|
-
...Array.from(codeBlockTitles)
|
|
3529
|
-
]
|
|
3530
|
-
});
|
|
3531
|
-
if (originalPlugin && typeof originalPlugin === "object" && "load" in originalPlugin) {
|
|
3532
|
-
return originalPlugin.load(id);
|
|
3533
|
-
}
|
|
3534
|
-
return "";
|
|
3535
|
-
};
|
|
3536
|
-
const dynamicGroupIconPlugin = {
|
|
3537
|
-
name: "post-process-add-group-icons",
|
|
3538
|
-
enforce: "post",
|
|
3539
|
-
configureServer(server) {
|
|
3540
|
-
const markdownGlobs = roots.map((root) => `${root}/**/*.md`);
|
|
3541
|
-
server.watcher.add(markdownGlobs);
|
|
3542
|
-
server.watcher.on("change", (file) => {
|
|
3543
|
-
if (file.endsWith(".md")) {
|
|
3544
|
-
cachedGroupIconsCSS = null;
|
|
3545
|
-
const module = server.moduleGraph.getModuleById("\0virtual:group-icons.css");
|
|
3546
|
-
if (module) {
|
|
3547
|
-
server.reloadModule(module);
|
|
3548
|
-
}
|
|
3549
|
-
}
|
|
3550
|
-
});
|
|
3551
|
-
},
|
|
3552
|
-
resolveId(id) {
|
|
3553
|
-
if (id === "virtual:group-icons.css") {
|
|
3554
|
-
return "\0virtual:group-icons.css";
|
|
3555
|
-
}
|
|
3556
|
-
return void 0;
|
|
3557
|
-
},
|
|
3558
|
-
async load(id) {
|
|
3559
|
-
if (id === "\0virtual:group-icons.css") {
|
|
3560
|
-
if (cachedGroupIconsCSS !== null) {
|
|
3561
|
-
return cachedGroupIconsCSS;
|
|
3562
|
-
}
|
|
3563
|
-
return generateGroupIconsCSS(id);
|
|
3564
|
-
}
|
|
3565
|
-
return void 0;
|
|
3502
|
+
const groupIconPlugin = groupIconVitePlugin({
|
|
3503
|
+
customIcon: {
|
|
3504
|
+
...builtinCustomIcon,
|
|
3505
|
+
...valaxyConfig.groupIcons?.customIcon
|
|
3566
3506
|
},
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
plugins.push(dynamicGroupIconPlugin);
|
|
3507
|
+
defaultLabels: [
|
|
3508
|
+
...valaxyConfig.groupIcons?.defaultLabels || [],
|
|
3509
|
+
...Object.keys(builtinCustomIcon),
|
|
3510
|
+
...Object.keys(valaxyConfig.groupIcons?.customIcon || {})
|
|
3511
|
+
]
|
|
3512
|
+
});
|
|
3513
|
+
plugins.push(groupIconPlugin);
|
|
3575
3514
|
return plugins;
|
|
3576
3515
|
}
|
|
3577
3516
|
|
|
@@ -3667,6 +3606,48 @@ function setupModules(node, modules) {
|
|
|
3667
3606
|
});
|
|
3668
3607
|
}
|
|
3669
3608
|
|
|
3609
|
+
const NODE_I18N = {
|
|
3610
|
+
/**
|
|
3611
|
+
* node 读取的 locales 数据
|
|
3612
|
+
*/
|
|
3613
|
+
locales: {}
|
|
3614
|
+
};
|
|
3615
|
+
function loadLocalesYml(localesPath) {
|
|
3616
|
+
const locales = {};
|
|
3617
|
+
if (fs.existsSync(localesPath)) {
|
|
3618
|
+
const files = fs.readdirSync(localesPath);
|
|
3619
|
+
files.forEach((file) => {
|
|
3620
|
+
if (file.endsWith(".yml") || file.endsWith(".yaml")) {
|
|
3621
|
+
const lang = file.replace(/\.ya?ml$/, "");
|
|
3622
|
+
const filePath = `${localesPath}/${file}`;
|
|
3623
|
+
try {
|
|
3624
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
3625
|
+
const data = yaml.load(content);
|
|
3626
|
+
locales[lang] = data || {};
|
|
3627
|
+
} catch (e) {
|
|
3628
|
+
console.error(`Error loading locale file: ${filePath}`, e);
|
|
3629
|
+
}
|
|
3630
|
+
}
|
|
3631
|
+
});
|
|
3632
|
+
}
|
|
3633
|
+
NODE_I18N.locales = locales;
|
|
3634
|
+
return locales;
|
|
3635
|
+
}
|
|
3636
|
+
function nodeT(key, lang) {
|
|
3637
|
+
if (key.startsWith(LOCALE_PREFIX)) {
|
|
3638
|
+
key = key.slice(LOCALE_PREFIX.length);
|
|
3639
|
+
}
|
|
3640
|
+
const data = NODE_I18N.locales[lang] || {};
|
|
3641
|
+
const keys = key.split(".");
|
|
3642
|
+
let result = data;
|
|
3643
|
+
for (const k of keys) {
|
|
3644
|
+
result = result?.[k];
|
|
3645
|
+
if (result === void 0)
|
|
3646
|
+
return "";
|
|
3647
|
+
}
|
|
3648
|
+
return result || "";
|
|
3649
|
+
}
|
|
3650
|
+
|
|
3670
3651
|
function commonOptions(args) {
|
|
3671
3652
|
return args.positional("root", {
|
|
3672
3653
|
default: ".",
|
|
@@ -3675,15 +3656,19 @@ function commonOptions(args) {
|
|
|
3675
3656
|
});
|
|
3676
3657
|
}
|
|
3677
3658
|
|
|
3659
|
+
const isWindows = os.platform() === "win32";
|
|
3678
3660
|
async function generateFuseList(options) {
|
|
3679
3661
|
consola.start(`Generate List for Fuse Search by (${colors.cyan("fuse.js")}) ...`);
|
|
3680
3662
|
const pattern = path$1.resolve(options.userRoot, options.config.siteConfig.fuse.pattern || "pages/**/*.md");
|
|
3681
|
-
const
|
|
3663
|
+
const finalPattern = isWindows ? fg.convertPathToPattern(pattern) : pattern;
|
|
3664
|
+
const files = await fg(finalPattern);
|
|
3682
3665
|
if (files.length > 0) {
|
|
3683
3666
|
consola.success(`Found ${colors.dim(files.length.toString())} markdown files for fuse search.`);
|
|
3684
3667
|
} else {
|
|
3685
|
-
consola.warn(`No markdown files found for fuse search. Please check your fuse pattern: ${colors.dim(
|
|
3668
|
+
consola.warn(`No markdown files found for fuse search. Please check your fuse pattern: ${colors.dim(finalPattern)}`);
|
|
3686
3669
|
}
|
|
3670
|
+
loadLocalesYml(path$1.resolve(options.userRoot, "locales"));
|
|
3671
|
+
const globalAuthor = nodeT(options.config.siteConfig.author.name, options.config.siteConfig.lang || "en");
|
|
3687
3672
|
const posts = [];
|
|
3688
3673
|
for await (const i of files) {
|
|
3689
3674
|
const raw = fs.readFileSync(i, "utf-8");
|
|
@@ -3698,15 +3683,15 @@ async function generateFuseList(options) {
|
|
|
3698
3683
|
if (fmData.password)
|
|
3699
3684
|
continue;
|
|
3700
3685
|
const extendKeys = options.config.fuse?.extendKeys || [];
|
|
3701
|
-
const relativeLink = path$1.
|
|
3686
|
+
const relativeLink = path$1.join(options.config.vite?.base || "/", path$1.relative(path$1.resolve(options.userRoot, "pages"), i)).replace(/\\/g, "/");
|
|
3702
3687
|
const link = i.endsWith("index.md") ? relativeLink.replace(/\/index\.md$/, "") : relativeLink.replace(/\.md$/, "");
|
|
3703
3688
|
const fuseListItem = {
|
|
3704
3689
|
title: fmData.title || "",
|
|
3705
3690
|
tags: (typeof fmData.tags === "string" ? [fmData.tags] : fmData.tags) || [],
|
|
3706
3691
|
categories: (typeof fmData.categories === "string" ? [fmData.categories] : fmData.categories) || [],
|
|
3707
|
-
author:
|
|
3692
|
+
author: fmData.author || globalAuthor,
|
|
3708
3693
|
excerpt: excerpt || content.slice(0, 100),
|
|
3709
|
-
// encode for chinese url
|
|
3694
|
+
// encode for chinese url, replace for windows path
|
|
3710
3695
|
link: encodeURI(link)
|
|
3711
3696
|
};
|
|
3712
3697
|
if (options.config.siteConfig.fuse.options.keys?.includes("content")) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valaxy",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.26.
|
|
4
|
+
"version": "0.26.9",
|
|
5
5
|
"description": "📄 Vite & Vue powered static blog generator.",
|
|
6
6
|
"author": {
|
|
7
7
|
"email": "me@yunyoujun.cn",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@clack/prompts": "^0.11.0",
|
|
66
66
|
"@iconify-json/ri": "^1.2.6",
|
|
67
67
|
"@intlify/unplugin-vue-i18n": "^11.0.1",
|
|
68
|
-
"@shikijs/transformers": "^3.
|
|
68
|
+
"@shikijs/transformers": "^3.15.0",
|
|
69
69
|
"@types/katex": "^0.16.7",
|
|
70
70
|
"@unhead/addons": "^2.0.19",
|
|
71
71
|
"@unhead/schema-org": "^2.0.19",
|
|
@@ -115,11 +115,11 @@
|
|
|
115
115
|
"ora": "^9.0.0",
|
|
116
116
|
"pascalcase": "^2.0.0",
|
|
117
117
|
"pathe": "^2.0.3",
|
|
118
|
-
"pinia": "^3.0.
|
|
118
|
+
"pinia": "^3.0.4",
|
|
119
119
|
"qrcode": "^1.5.4",
|
|
120
120
|
"resolve-global": "^2.0.0",
|
|
121
121
|
"sass": "^1.93.3",
|
|
122
|
-
"shiki": "^3.
|
|
122
|
+
"shiki": "^3.15.0",
|
|
123
123
|
"star-markdown-css": "^0.5.3",
|
|
124
124
|
"table": "^6.9.0",
|
|
125
125
|
"unhead": "^2.0.19",
|
|
@@ -128,19 +128,19 @@
|
|
|
128
128
|
"unplugin-vue-markdown": "^29.2.0",
|
|
129
129
|
"unplugin-vue-router": "^0.16.1",
|
|
130
130
|
"vanilla-lazyload": "^19.1.3",
|
|
131
|
-
"vite": "^7.1
|
|
131
|
+
"vite": "^7.2.1",
|
|
132
132
|
"vite-dev-rpc": "^1.1.0",
|
|
133
133
|
"vite-plugin-vue-devtools": "^8.0.3",
|
|
134
134
|
"vite-plugin-vue-layouts": "^0.11.0",
|
|
135
135
|
"vite-ssg": "^28.2.2",
|
|
136
136
|
"vite-ssg-sitemap": "^0.10.0",
|
|
137
137
|
"vitepress-plugin-group-icons": "^1.6.5",
|
|
138
|
-
"vue": "^3.5.
|
|
138
|
+
"vue": "^3.5.23",
|
|
139
139
|
"vue-i18n": "^11.1.12",
|
|
140
140
|
"vue-router": "^4.6.3",
|
|
141
141
|
"yargs": "^18.0.0",
|
|
142
|
-
"@valaxyjs/
|
|
143
|
-
"@valaxyjs/
|
|
142
|
+
"@valaxyjs/devtools": "0.26.9",
|
|
143
|
+
"@valaxyjs/utils": "0.26.9"
|
|
144
144
|
},
|
|
145
145
|
"devDependencies": {
|
|
146
146
|
"@mdit-vue/plugin-component": "^3.0.2",
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import yaml from 'js-yaml'
|
|
3
|
+
import { LOCALE_PREFIX } from '../constants'
|
|
4
|
+
|
|
5
|
+
export const NODE_I18N: {
|
|
6
|
+
locales: Record<string, any>
|
|
7
|
+
} = {
|
|
8
|
+
/**
|
|
9
|
+
* node 读取的 locales 数据
|
|
10
|
+
*/
|
|
11
|
+
locales: {},
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 读取翻译 yml 文件
|
|
16
|
+
*/
|
|
17
|
+
export function loadLocalesYml(localesPath: string): Record<string, any> {
|
|
18
|
+
/**
|
|
19
|
+
* read locales dir *.yml
|
|
20
|
+
*/
|
|
21
|
+
const locales: Record<string, any> = {}
|
|
22
|
+
if (fs.existsSync(localesPath)) {
|
|
23
|
+
const files = fs.readdirSync(localesPath)
|
|
24
|
+
files.forEach((file) => {
|
|
25
|
+
if (file.endsWith('.yml') || file.endsWith('.yaml')) {
|
|
26
|
+
const lang = file.replace(/\.ya?ml$/, '')
|
|
27
|
+
const filePath = `${localesPath}/${file}`
|
|
28
|
+
try {
|
|
29
|
+
const content = fs.readFileSync(filePath, 'utf-8')
|
|
30
|
+
const data = yaml.load(content)
|
|
31
|
+
locales[lang] = data || {}
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.error(`Error loading locale file: ${filePath}`, e)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// cache
|
|
41
|
+
NODE_I18N.locales = locales
|
|
42
|
+
return locales
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* node translate function
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* nodeT('greeting.hello', 'en') // 'Hello'
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function nodeT(key: string, lang: string): string {
|
|
53
|
+
if (key.startsWith(LOCALE_PREFIX)) {
|
|
54
|
+
key = key.slice(LOCALE_PREFIX.length)
|
|
55
|
+
}
|
|
56
|
+
const data = NODE_I18N.locales[lang] || {}
|
|
57
|
+
const keys = key.split('.')
|
|
58
|
+
let result: any = data
|
|
59
|
+
for (const k of keys) {
|
|
60
|
+
result = result?.[k]
|
|
61
|
+
if (result === undefined)
|
|
62
|
+
return ''
|
|
63
|
+
}
|
|
64
|
+
return result || ''
|
|
65
|
+
}
|