@bhsd/common 0.10.2 → 0.11.0
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/index.d.ts +13 -2
- package/dist/index.js +27 -4
- package/dist/index.mjs +27 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { ConfigData } from 'wikiparser-node';
|
|
1
2
|
import type { LanguageServiceBase } from 'wikiparser-node/extensions/typings';
|
|
2
|
-
export declare const CDN = "https://testingcf.jsdelivr.net";
|
|
3
3
|
export type RegexGetter<T = string> = (s: T) => RegExp;
|
|
4
|
+
declare type ConfigGetter = () => Promise<ConfigData>;
|
|
4
5
|
declare global {
|
|
5
6
|
const define: unknown;
|
|
6
7
|
}
|
|
8
|
+
export declare const CDN = "https://testingcf.jsdelivr.net";
|
|
7
9
|
/**
|
|
8
10
|
* 解码HTML实体
|
|
9
11
|
* @param str 要解码的字符串
|
|
@@ -45,12 +47,20 @@ export declare const setObject: (key: string, value: unknown) => void;
|
|
|
45
47
|
* @param baseVersion 基础版本号
|
|
46
48
|
*/
|
|
47
49
|
export declare const compareVersion: (version: string, baseVersion: string) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 加载 wikiparse
|
|
52
|
+
* @param getConfig 获取解析配置的函数
|
|
53
|
+
* @param langs 语言代码
|
|
54
|
+
*/
|
|
55
|
+
export declare const getWikiparse: (getConfig?: ConfigGetter, langs?: string | string[]) => Promise<void>;
|
|
48
56
|
/**
|
|
49
57
|
* 获取LSP
|
|
50
58
|
* @param obj 关联对象
|
|
51
59
|
* @param include 是否嵌入
|
|
60
|
+
* @param getConfig 获取解析配置的函数
|
|
61
|
+
* @param lang 语言代码
|
|
52
62
|
*/
|
|
53
|
-
export declare const getLSP: (obj: object, include?: boolean) => LanguageServiceBase | undefined;
|
|
63
|
+
export declare const getLSP: (obj: object, include?: boolean, getConfig?: ConfigGetter, lang?: string) => LanguageServiceBase | undefined;
|
|
54
64
|
/**
|
|
55
65
|
* 清理内联样式中的`{`和`}`
|
|
56
66
|
* @param style 内联样式
|
|
@@ -72,3 +82,4 @@ export declare function getRegex<T extends object>(f: RegexGetter<T>): RegexGett
|
|
|
72
82
|
* @param f 生成正则表达式的函数
|
|
73
83
|
*/
|
|
74
84
|
export declare const getObjRegex: typeof getRegex;
|
|
85
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __export(index_exports, {
|
|
|
25
25
|
getObjRegex: () => getObjRegex,
|
|
26
26
|
getObject: () => getObject,
|
|
27
27
|
getRegex: () => getRegex,
|
|
28
|
+
getWikiparse: () => getWikiparse,
|
|
28
29
|
loadScript: () => loadScript,
|
|
29
30
|
normalizeTitle: () => normalizeTitle,
|
|
30
31
|
numToHex: () => numToHex,
|
|
@@ -113,11 +114,33 @@ const compareVersion = (version, baseVersion) => {
|
|
|
113
114
|
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
114
115
|
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
115
116
|
};
|
|
117
|
+
let configLoaded = false, i18nLoaded = false;
|
|
118
|
+
const getWikiparse = async (getConfig, langs) => {
|
|
119
|
+
const dir = "extensions/dist";
|
|
120
|
+
await loadScript(`npm/wikiparser-node/${dir}/base.min.js`, "wikiparse");
|
|
121
|
+
await loadScript(`${wikiparse.CDN}/${dir}/lsp.min.js`, "wikiparse.LanguageService");
|
|
122
|
+
if (!configLoaded && typeof getConfig === "function") {
|
|
123
|
+
configLoaded = true;
|
|
124
|
+
try {
|
|
125
|
+
wikiparse.setConfig(await getConfig());
|
|
126
|
+
} catch {
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!i18nLoaded && langs) {
|
|
130
|
+
i18nLoaded = true;
|
|
131
|
+
for (const lang of Array.isArray(langs) ? langs : [langs]) {
|
|
132
|
+
try {
|
|
133
|
+
const i18n = await (await fetch(`${wikiparse.CDN}/i18n/${lang.toLowerCase()}.json`)).json();
|
|
134
|
+
wikiparse.setI18N(i18n);
|
|
135
|
+
break;
|
|
136
|
+
} catch {
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
116
141
|
const lsps = /* @__PURE__ */ new WeakMap();
|
|
117
|
-
const getLSP = (obj, include) => {
|
|
118
|
-
|
|
119
|
-
void loadScript(`${path}/base.min.js`, "wikiparse");
|
|
120
|
-
void loadScript(`${path}/lsp.min.js`, "wikiparse.LanguageService");
|
|
142
|
+
const getLSP = (obj, include, getConfig, lang) => {
|
|
143
|
+
void getWikiparse(getConfig, lang);
|
|
121
144
|
if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
|
|
122
145
|
return lsps.get(obj);
|
|
123
146
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -76,11 +76,33 @@ const compareVersion = (version, baseVersion) => {
|
|
|
76
76
|
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
77
77
|
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
78
78
|
};
|
|
79
|
+
let configLoaded = false, i18nLoaded = false;
|
|
80
|
+
const getWikiparse = async (getConfig, langs) => {
|
|
81
|
+
const dir = "extensions/dist";
|
|
82
|
+
await loadScript(`npm/wikiparser-node/${dir}/base.min.js`, "wikiparse");
|
|
83
|
+
await loadScript(`${wikiparse.CDN}/${dir}/lsp.min.js`, "wikiparse.LanguageService");
|
|
84
|
+
if (!configLoaded && typeof getConfig === "function") {
|
|
85
|
+
configLoaded = true;
|
|
86
|
+
try {
|
|
87
|
+
wikiparse.setConfig(await getConfig());
|
|
88
|
+
} catch {
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!i18nLoaded && langs) {
|
|
92
|
+
i18nLoaded = true;
|
|
93
|
+
for (const lang of Array.isArray(langs) ? langs : [langs]) {
|
|
94
|
+
try {
|
|
95
|
+
const i18n = await (await fetch(`${wikiparse.CDN}/i18n/${lang.toLowerCase()}.json`)).json();
|
|
96
|
+
wikiparse.setI18N(i18n);
|
|
97
|
+
break;
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
79
103
|
const lsps = /* @__PURE__ */ new WeakMap();
|
|
80
|
-
const getLSP = (obj, include) => {
|
|
81
|
-
|
|
82
|
-
void loadScript(`${path}/base.min.js`, "wikiparse");
|
|
83
|
-
void loadScript(`${path}/lsp.min.js`, "wikiparse.LanguageService");
|
|
104
|
+
const getLSP = (obj, include, getConfig, lang) => {
|
|
105
|
+
void getWikiparse(getConfig, lang);
|
|
84
106
|
if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
|
|
85
107
|
return lsps.get(obj);
|
|
86
108
|
}
|
|
@@ -114,6 +136,7 @@ export {
|
|
|
114
136
|
getObjRegex,
|
|
115
137
|
getObject,
|
|
116
138
|
getRegex,
|
|
139
|
+
getWikiparse,
|
|
117
140
|
loadScript,
|
|
118
141
|
normalizeTitle,
|
|
119
142
|
numToHex,
|