@bhsd/common 0.15.0 → 1.0.1
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 +1 -64
- package/dist/index.js +66 -186
- package/dist/index.mjs +1 -122
- package/package.json +14 -33
- package/dist/cm.d.ts +0 -49
- package/dist/cm.js +0 -83
- package/dist/cm.mjs +0 -63
- package/dist/stylelint.d.ts +0 -10
- package/dist/stylelint.js +0 -39
- package/dist/stylelint.mjs +0 -19
- package/dist/test.d.ts +0 -38
- package/dist/test.js +0 -172
- package/eslintrc.browser.cjs +0 -54
- package/eslintrc.cjs +0 -950
- package/eslintrc.dist.cjs +0 -23
- package/eslintrc.node.cjs +0 -48
- package/stylelintrc.cjs +0 -72
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
import type { ConfigData } from 'wikiparser-node';
|
|
2
|
-
import type { LanguageServiceBase } from 'wikiparser-node/extensions/typings';
|
|
3
1
|
export type RegexGetter<T = string> = (s: T) => RegExp;
|
|
4
|
-
declare
|
|
5
|
-
declare global {
|
|
6
|
-
const define: unknown;
|
|
7
|
-
}
|
|
8
|
-
export declare const CDN = "https://testingcf.jsdelivr.net", wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
|
|
9
|
-
/**
|
|
10
|
-
* 解码HTML实体
|
|
11
|
-
* @param str 要解码的字符串
|
|
12
|
-
*/
|
|
13
|
-
export declare const decodeHTML: (str: string) => string;
|
|
2
|
+
export declare const wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
|
|
14
3
|
/**
|
|
15
4
|
* PHP的`rawurldecode`函数的JavaScript实现
|
|
16
5
|
* @param str 要解码的字符串
|
|
17
6
|
*/
|
|
18
7
|
export declare const rawurldecode: (str: string) => string;
|
|
19
|
-
/**
|
|
20
|
-
* 解码标题中的HTML实体和URL编码
|
|
21
|
-
* @param title 标题
|
|
22
|
-
*/
|
|
23
|
-
export declare const normalizeTitle: (title: string) => string;
|
|
24
8
|
/**
|
|
25
9
|
* 将0~1之间的数字转换为十六进制
|
|
26
10
|
* @param d 0~1之间的数字
|
|
@@ -32,57 +16,11 @@ export declare const numToHex: (d: number) => string;
|
|
|
32
16
|
* @param hsl 是否包含 HSL
|
|
33
17
|
*/
|
|
34
18
|
export declare const splitColors: (str: string, hsl?: boolean) => [string, number, number, boolean][];
|
|
35
|
-
/**
|
|
36
|
-
* 使用传统方法加载脚本
|
|
37
|
-
* @param src 脚本地址
|
|
38
|
-
* @param globalConst 脚本全局变量名
|
|
39
|
-
* @param amd 是否兼容 AMD
|
|
40
|
-
*/
|
|
41
|
-
export declare const loadScript: (src: string, globalConst: string, amd?: boolean) => Promise<void>;
|
|
42
|
-
export declare const getObject: (key: string) => any;
|
|
43
|
-
export declare const setObject: (key: string, value: unknown) => void;
|
|
44
|
-
/**
|
|
45
|
-
* 比较版本号
|
|
46
|
-
* @param version 版本号
|
|
47
|
-
* @param baseVersion 基础版本号
|
|
48
|
-
*/
|
|
49
|
-
export declare const compareVersion: (version: string, baseVersion: string) => boolean;
|
|
50
|
-
/**
|
|
51
|
-
* 加载 I18N
|
|
52
|
-
* @param url 下载地址
|
|
53
|
-
* @param cur 当前版本号
|
|
54
|
-
* @param languages 语言代码列表
|
|
55
|
-
* @param acceptableLangs 可接受的语言代码列表
|
|
56
|
-
* @param key 存储的键名
|
|
57
|
-
* @param i18n 已存储的I18N对象
|
|
58
|
-
* @throws `Error` 无法获取语言包
|
|
59
|
-
*/
|
|
60
|
-
export declare const setI18N: (url: string, cur: string, languages: string[] | string, acceptableLangs: string[], key: string, i18n?: Record<string, string>) => Promise<Record<string, string>>;
|
|
61
|
-
/**
|
|
62
|
-
* 加载 wikiparse
|
|
63
|
-
* @param getConfig 获取解析配置的函数
|
|
64
|
-
* @param langs 语言代码
|
|
65
|
-
* @param cdn CDN 地址
|
|
66
|
-
*/
|
|
67
|
-
export declare const getWikiparse: (getConfig?: ConfigGetter, langs?: string | string[], cdn?: string) => Promise<void>;
|
|
68
|
-
/**
|
|
69
|
-
* 获取LSP
|
|
70
|
-
* @param obj 关联对象
|
|
71
|
-
* @param include 是否嵌入
|
|
72
|
-
* @param getConfig 获取解析配置的函数
|
|
73
|
-
* @param lang 语言代码
|
|
74
|
-
*/
|
|
75
|
-
export declare const getLSP: (obj: object, include?: boolean, getConfig?: ConfigGetter, lang?: string) => LanguageServiceBase | undefined;
|
|
76
19
|
/**
|
|
77
20
|
* 清理内联样式中的`{`和`}`
|
|
78
21
|
* @param style 内联样式
|
|
79
22
|
*/
|
|
80
23
|
export declare const sanitizeInlineStyle: (style: string) => string;
|
|
81
|
-
/**
|
|
82
|
-
* 刷新屏幕输出
|
|
83
|
-
* @param str 要输出的字符串
|
|
84
|
-
*/
|
|
85
|
-
export declare const refreshStdout: (str: string) => void;
|
|
86
24
|
/**
|
|
87
25
|
* 缓存生成的正则表达式
|
|
88
26
|
* @param f 生成正则表达式的函数
|
|
@@ -94,4 +32,3 @@ export declare function getRegex<T extends object>(f: RegexGetter<T>): RegexGett
|
|
|
94
32
|
* @param f 生成正则表达式的函数
|
|
95
33
|
*/
|
|
96
34
|
export declare const getObjRegex: typeof getRegex;
|
|
97
|
-
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,194 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
getLSP: () => getLSP,
|
|
25
|
-
getObjRegex: () => getObjRegex,
|
|
26
|
-
getObject: () => getObject,
|
|
27
|
-
getRegex: () => getRegex,
|
|
28
|
-
getWikiparse: () => getWikiparse,
|
|
29
|
-
loadScript: () => loadScript,
|
|
30
|
-
normalizeTitle: () => normalizeTitle,
|
|
31
|
-
numToHex: () => numToHex,
|
|
32
|
-
rawurldecode: () => rawurldecode,
|
|
33
|
-
refreshStdout: () => refreshStdout,
|
|
34
|
-
sanitizeInlineStyle: () => sanitizeInlineStyle,
|
|
35
|
-
setI18N: () => setI18N,
|
|
36
|
-
setObject: () => setObject,
|
|
37
|
-
splitColors: () => splitColors,
|
|
38
|
-
wmf: () => wmf
|
|
39
|
-
});
|
|
40
|
-
module.exports = __toCommonJS(index_exports);
|
|
41
|
-
var define_LANGS_default = ["zh-hans", "zh-hant"];
|
|
42
|
-
const CDN = "https://testingcf.jsdelivr.net", wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
|
|
43
|
-
const textarea = /* @__PURE__ */ (() => typeof document === "object" ? document.createElement("textarea") : void 0)();
|
|
44
|
-
const decodeHTML = (str) => {
|
|
45
|
-
textarea.innerHTML = str;
|
|
46
|
-
return textarea.value;
|
|
47
|
-
};
|
|
48
|
-
const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, "%25"));
|
|
49
|
-
const normalizeTitle = (title) => {
|
|
50
|
-
const decoded = rawurldecode(title);
|
|
51
|
-
return /[<>[\]|{}]/u.test(decoded) ? decoded : decodeHTML(decoded);
|
|
52
|
-
};
|
|
53
|
-
const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
|
|
54
|
-
const regex = /* @__PURE__ */ (() => {
|
|
55
|
-
const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\p{N}_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`;
|
|
56
|
-
return {
|
|
57
|
-
full: new RegExp(String.raw`(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"),
|
|
58
|
-
rgb: new RegExp(String.raw`(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor})`, "giu")
|
|
59
|
-
};
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getObjRegex = exports.sanitizeInlineStyle = exports.splitColors = exports.numToHex = exports.rawurldecode = exports.wmf = void 0;
|
|
4
|
+
exports.getRegex = getRegex;
|
|
5
|
+
exports.wmf = 'wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)';
|
|
6
|
+
/**
|
|
7
|
+
* PHP的`rawurldecode`函数的JavaScript实现
|
|
8
|
+
* @param str 要解码的字符串
|
|
9
|
+
*/
|
|
10
|
+
const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, '%25'));
|
|
11
|
+
exports.rawurldecode = rawurldecode;
|
|
12
|
+
/**
|
|
13
|
+
* 将0~1之间的数字转换为十六进制
|
|
14
|
+
* @param d 0~1之间的数字
|
|
15
|
+
*/
|
|
16
|
+
const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, '0');
|
|
17
|
+
exports.numToHex = numToHex;
|
|
18
|
+
const regex = /* #__PURE__ */ (() => {
|
|
19
|
+
const hexColor = String.raw `#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\p{N}_])`, rgbValue = String.raw `(?:\d*\.)?\d+%?`, hue = String.raw `(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw `rgba?\(\s*(?:${String.raw `${new Array(3).fill(rgbValue).join(String.raw `\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw `${new Array(3).fill(rgbValue).join(String.raw `\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw `hsla?\(\s*(?:${String.raw `${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw `${hue}${String.raw `\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`;
|
|
20
|
+
return {
|
|
21
|
+
full: new RegExp(String.raw `(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor}|${hslColor})`, 'giu'),
|
|
22
|
+
rgb: new RegExp(String.raw `(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor})`, 'giu'),
|
|
23
|
+
};
|
|
60
24
|
})();
|
|
25
|
+
/**
|
|
26
|
+
* 包含颜色时断开字符串
|
|
27
|
+
* @param str 字符串
|
|
28
|
+
* @param hsl 是否包含 HSL
|
|
29
|
+
*/
|
|
61
30
|
const splitColors = (str, hsl = true) => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
31
|
+
const pieces = [], re = regex[hsl ? 'full' : 'rgb'];
|
|
32
|
+
re.lastIndex = 0;
|
|
33
|
+
let mt = re.exec(str), lastIndex = 0;
|
|
34
|
+
while (mt) {
|
|
35
|
+
const index = mt.index + mt[1].length;
|
|
36
|
+
if (index > lastIndex) {
|
|
37
|
+
pieces.push([str.slice(lastIndex, index), lastIndex, index, false]);
|
|
38
|
+
}
|
|
39
|
+
({ lastIndex } = re);
|
|
40
|
+
pieces.push([mt[2], index, lastIndex, str[index - 1] !== '&' || !/^#\d+$/u.test(mt[2])]);
|
|
41
|
+
mt = re.exec(str);
|
|
69
42
|
}
|
|
70
|
-
(
|
|
71
|
-
|
|
72
|
-
mt = re.exec(str);
|
|
73
|
-
}
|
|
74
|
-
if (str.length > lastIndex) {
|
|
75
|
-
pieces.push([str.slice(lastIndex), lastIndex, str.length, false]);
|
|
76
|
-
}
|
|
77
|
-
return pieces;
|
|
78
|
-
};
|
|
79
|
-
const loading = /* @__PURE__ */ new Map();
|
|
80
|
-
const loadScript = (src, globalConst, amd) => {
|
|
81
|
-
if (loading.has(src)) {
|
|
82
|
-
return loading.get(src);
|
|
83
|
-
}
|
|
84
|
-
const promise = new Promise((resolve) => {
|
|
85
|
-
const path = /^https?:\/\//iu.test(src) ? src : `${CDN}/${src}`;
|
|
86
|
-
let obj = globalThis;
|
|
87
|
-
for (const prop of globalConst.split(".")) {
|
|
88
|
-
obj = obj?.[prop];
|
|
43
|
+
if (str.length > lastIndex) {
|
|
44
|
+
pieces.push([str.slice(lastIndex), lastIndex, str.length, false]);
|
|
89
45
|
}
|
|
90
|
-
|
|
91
|
-
resolve();
|
|
92
|
-
} else if (amd && typeof define === "function" && "amd" in define) {
|
|
93
|
-
const requirejs = globalThis.require;
|
|
94
|
-
requirejs.config({ paths: { [globalConst]: path } });
|
|
95
|
-
requirejs([globalConst], (exports) => {
|
|
96
|
-
Object.assign(globalThis, { [globalConst]: exports });
|
|
97
|
-
resolve();
|
|
98
|
-
});
|
|
99
|
-
} else {
|
|
100
|
-
const script = document.createElement("script");
|
|
101
|
-
script.src = path;
|
|
102
|
-
script.onload = () => {
|
|
103
|
-
resolve();
|
|
104
|
-
};
|
|
105
|
-
document.head.append(script);
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
loading.set(src, promise);
|
|
109
|
-
return promise;
|
|
110
|
-
};
|
|
111
|
-
const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
|
|
112
|
-
const setObject = (key, value) => {
|
|
113
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
114
|
-
};
|
|
115
|
-
const parseVersion = (version) => version.split(".", 3).map(Number);
|
|
116
|
-
const compareVersion = (version, baseVersion) => {
|
|
117
|
-
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
118
|
-
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
119
|
-
};
|
|
120
|
-
const setI18N = async (url, cur, languages, acceptableLangs, key, i18n = getObject(key) ?? {}) => {
|
|
121
|
-
const { version, lang } = i18n, langs = Array.isArray(languages) ? languages : [languages];
|
|
122
|
-
if (version === cur && langs.includes(lang)) {
|
|
123
|
-
return i18n;
|
|
124
|
-
}
|
|
125
|
-
for (const language of langs) {
|
|
126
|
-
const l = language.toLowerCase();
|
|
127
|
-
if (!acceptableLangs.includes(l)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
const res = await fetch(`${url}/${l}.json`);
|
|
132
|
-
Object.assign(i18n, await res.json(), { version: cur, lang: language });
|
|
133
|
-
setObject(key, i18n);
|
|
134
|
-
return i18n;
|
|
135
|
-
} catch {
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
throw new Error(`Failed to fetch the localization for ${langs[0]}.`);
|
|
139
|
-
};
|
|
140
|
-
let configLoaded = false, i18nLoaded = false;
|
|
141
|
-
const getWikiparse = async (getConfig, langs, cdn) => {
|
|
142
|
-
const dir = "extensions/dist";
|
|
143
|
-
let src = cdn || `npm/wikiparser-node/${dir}/base.min.js`;
|
|
144
|
-
if (!src.endsWith(".js")) {
|
|
145
|
-
src = `${src}${src.endsWith("/") ? "" : "/"}${dir}/base.js`;
|
|
146
|
-
}
|
|
147
|
-
await loadScript(src, "wikiparse");
|
|
148
|
-
await loadScript(`${wikiparse.CDN}/${dir}/lsp.js`, "wikiparse.LanguageService");
|
|
149
|
-
if (!configLoaded && typeof getConfig === "function") {
|
|
150
|
-
configLoaded = true;
|
|
151
|
-
try {
|
|
152
|
-
wikiparse.setConfig(await getConfig());
|
|
153
|
-
} catch {
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (!i18nLoaded && langs) {
|
|
157
|
-
i18nLoaded = true;
|
|
158
|
-
const key = "wikiparse-i18n", { version } = wikiparse;
|
|
159
|
-
try {
|
|
160
|
-
wikiparse.setI18N(await setI18N(`${wikiparse.CDN}/i18n`, version, langs, define_LANGS_default, key));
|
|
161
|
-
} catch {
|
|
162
|
-
setObject(key, { version, lang: "en" });
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
const lsps = /* @__PURE__ */ new WeakMap();
|
|
167
|
-
const getLSP = (obj, include, getConfig, lang) => {
|
|
168
|
-
void getWikiparse(getConfig, lang);
|
|
169
|
-
if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
|
|
170
|
-
return lsps.get(obj);
|
|
171
|
-
}
|
|
172
|
-
const lsp = new wikiparse.LanguageService(include);
|
|
173
|
-
lsps.set(obj, lsp);
|
|
174
|
-
return lsp;
|
|
175
|
-
};
|
|
176
|
-
const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, (p) => p === "{" ? "{" : "}");
|
|
177
|
-
const refreshStdout = (str) => {
|
|
178
|
-
process.stdout.write(`\x1B[K\x1B[?7l${str}\x1B[?7h\r`);
|
|
46
|
+
return pieces;
|
|
179
47
|
};
|
|
48
|
+
exports.splitColors = splitColors;
|
|
49
|
+
/**
|
|
50
|
+
* 清理内联样式中的`{`和`}`
|
|
51
|
+
* @param style 内联样式
|
|
52
|
+
*/
|
|
53
|
+
const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, p => p === '{' ? '{' : '}');
|
|
54
|
+
exports.sanitizeInlineStyle = sanitizeInlineStyle;
|
|
180
55
|
function getRegex(f) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
56
|
+
/* eslint-enable jsdoc/require-jsdoc */
|
|
57
|
+
const map = new Map(), weakMap = new WeakMap();
|
|
58
|
+
return s => {
|
|
59
|
+
const regexp = typeof s === 'string' ? map : weakMap;
|
|
60
|
+
if (regexp.has(s)) {
|
|
61
|
+
const re = regexp.get(s);
|
|
62
|
+
re.lastIndex = 0;
|
|
63
|
+
return re;
|
|
64
|
+
}
|
|
65
|
+
const re = f(s);
|
|
66
|
+
regexp.set(s, re);
|
|
67
|
+
return re;
|
|
68
|
+
};
|
|
193
69
|
}
|
|
194
|
-
|
|
70
|
+
/**
|
|
71
|
+
* 缓存生成的正则表达式
|
|
72
|
+
* @param f 生成正则表达式的函数
|
|
73
|
+
*/
|
|
74
|
+
exports.getObjRegex = getRegex;
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
const CDN = "https://testingcf.jsdelivr.net", wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
|
|
3
|
-
const textarea = /* @__PURE__ */ (() => typeof document === "object" ? document.createElement("textarea") : void 0)();
|
|
4
|
-
const decodeHTML = (str) => {
|
|
5
|
-
textarea.innerHTML = str;
|
|
6
|
-
return textarea.value;
|
|
7
|
-
};
|
|
1
|
+
const wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
|
|
8
2
|
const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, "%25"));
|
|
9
|
-
const normalizeTitle = (title) => {
|
|
10
|
-
const decoded = rawurldecode(title);
|
|
11
|
-
return /[<>[\]|{}]/u.test(decoded) ? decoded : decodeHTML(decoded);
|
|
12
|
-
};
|
|
13
3
|
const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
|
|
14
4
|
const regex = /* @__PURE__ */ (() => {
|
|
15
5
|
const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\p{N}_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`;
|
|
@@ -36,107 +26,7 @@ const splitColors = (str, hsl = true) => {
|
|
|
36
26
|
}
|
|
37
27
|
return pieces;
|
|
38
28
|
};
|
|
39
|
-
const loading = /* @__PURE__ */ new Map();
|
|
40
|
-
const loadScript = (src, globalConst, amd) => {
|
|
41
|
-
if (loading.has(src)) {
|
|
42
|
-
return loading.get(src);
|
|
43
|
-
}
|
|
44
|
-
const promise = new Promise((resolve) => {
|
|
45
|
-
const path = /^https?:\/\//iu.test(src) ? src : `${CDN}/${src}`;
|
|
46
|
-
let obj = globalThis;
|
|
47
|
-
for (const prop of globalConst.split(".")) {
|
|
48
|
-
obj = obj?.[prop];
|
|
49
|
-
}
|
|
50
|
-
if (obj) {
|
|
51
|
-
resolve();
|
|
52
|
-
} else if (amd && typeof define === "function" && "amd" in define) {
|
|
53
|
-
const requirejs = globalThis.require;
|
|
54
|
-
requirejs.config({ paths: { [globalConst]: path } });
|
|
55
|
-
requirejs([globalConst], (exports) => {
|
|
56
|
-
Object.assign(globalThis, { [globalConst]: exports });
|
|
57
|
-
resolve();
|
|
58
|
-
});
|
|
59
|
-
} else {
|
|
60
|
-
const script = document.createElement("script");
|
|
61
|
-
script.src = path;
|
|
62
|
-
script.onload = () => {
|
|
63
|
-
resolve();
|
|
64
|
-
};
|
|
65
|
-
document.head.append(script);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
loading.set(src, promise);
|
|
69
|
-
return promise;
|
|
70
|
-
};
|
|
71
|
-
const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
|
|
72
|
-
const setObject = (key, value) => {
|
|
73
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
74
|
-
};
|
|
75
|
-
const parseVersion = (version) => version.split(".", 3).map(Number);
|
|
76
|
-
const compareVersion = (version, baseVersion) => {
|
|
77
|
-
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
78
|
-
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
79
|
-
};
|
|
80
|
-
const setI18N = async (url, cur, languages, acceptableLangs, key, i18n = getObject(key) ?? {}) => {
|
|
81
|
-
const { version, lang } = i18n, langs = Array.isArray(languages) ? languages : [languages];
|
|
82
|
-
if (version === cur && langs.includes(lang)) {
|
|
83
|
-
return i18n;
|
|
84
|
-
}
|
|
85
|
-
for (const language of langs) {
|
|
86
|
-
const l = language.toLowerCase();
|
|
87
|
-
if (!acceptableLangs.includes(l)) {
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
const res = await fetch(`${url}/${l}.json`);
|
|
92
|
-
Object.assign(i18n, await res.json(), { version: cur, lang: language });
|
|
93
|
-
setObject(key, i18n);
|
|
94
|
-
return i18n;
|
|
95
|
-
} catch {
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
throw new Error(`Failed to fetch the localization for ${langs[0]}.`);
|
|
99
|
-
};
|
|
100
|
-
let configLoaded = false, i18nLoaded = false;
|
|
101
|
-
const getWikiparse = async (getConfig, langs, cdn) => {
|
|
102
|
-
const dir = "extensions/dist";
|
|
103
|
-
let src = cdn || `npm/wikiparser-node/${dir}/base.min.js`;
|
|
104
|
-
if (!src.endsWith(".js")) {
|
|
105
|
-
src = `${src}${src.endsWith("/") ? "" : "/"}${dir}/base.js`;
|
|
106
|
-
}
|
|
107
|
-
await loadScript(src, "wikiparse");
|
|
108
|
-
await loadScript(`${wikiparse.CDN}/${dir}/lsp.js`, "wikiparse.LanguageService");
|
|
109
|
-
if (!configLoaded && typeof getConfig === "function") {
|
|
110
|
-
configLoaded = true;
|
|
111
|
-
try {
|
|
112
|
-
wikiparse.setConfig(await getConfig());
|
|
113
|
-
} catch {
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (!i18nLoaded && langs) {
|
|
117
|
-
i18nLoaded = true;
|
|
118
|
-
const key = "wikiparse-i18n", { version } = wikiparse;
|
|
119
|
-
try {
|
|
120
|
-
wikiparse.setI18N(await setI18N(`${wikiparse.CDN}/i18n`, version, langs, define_LANGS_default, key));
|
|
121
|
-
} catch {
|
|
122
|
-
setObject(key, { version, lang: "en" });
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
const lsps = /* @__PURE__ */ new WeakMap();
|
|
127
|
-
const getLSP = (obj, include, getConfig, lang) => {
|
|
128
|
-
void getWikiparse(getConfig, lang);
|
|
129
|
-
if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
|
|
130
|
-
return lsps.get(obj);
|
|
131
|
-
}
|
|
132
|
-
const lsp = new wikiparse.LanguageService(include);
|
|
133
|
-
lsps.set(obj, lsp);
|
|
134
|
-
return lsp;
|
|
135
|
-
};
|
|
136
29
|
const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, (p) => p === "{" ? "{" : "}");
|
|
137
|
-
const refreshStdout = (str) => {
|
|
138
|
-
process.stdout.write(`\x1B[K\x1B[?7l${str}\x1B[?7h\r`);
|
|
139
|
-
};
|
|
140
30
|
function getRegex(f) {
|
|
141
31
|
const map = /* @__PURE__ */ new Map(), weakMap = /* @__PURE__ */ new WeakMap();
|
|
142
32
|
return (s) => {
|
|
@@ -153,22 +43,11 @@ function getRegex(f) {
|
|
|
153
43
|
}
|
|
154
44
|
const getObjRegex = getRegex;
|
|
155
45
|
export {
|
|
156
|
-
CDN,
|
|
157
|
-
compareVersion,
|
|
158
|
-
decodeHTML,
|
|
159
|
-
getLSP,
|
|
160
46
|
getObjRegex,
|
|
161
|
-
getObject,
|
|
162
47
|
getRegex,
|
|
163
|
-
getWikiparse,
|
|
164
|
-
loadScript,
|
|
165
|
-
normalizeTitle,
|
|
166
48
|
numToHex,
|
|
167
49
|
rawurldecode,
|
|
168
|
-
refreshStdout,
|
|
169
50
|
sanitizeInlineStyle,
|
|
170
|
-
setI18N,
|
|
171
|
-
setObject,
|
|
172
51
|
splitColors,
|
|
173
52
|
wmf
|
|
174
53
|
};
|
package/package.json
CHANGED
|
@@ -1,52 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bhsd/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"homepage": "https://github.com/bhsd-harry/common#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/bhsd-harry/common/issues"
|
|
7
|
+
},
|
|
4
8
|
"license": "MIT",
|
|
5
9
|
"author": "Bhsd",
|
|
6
10
|
"files": [
|
|
7
|
-
"/dist/"
|
|
8
|
-
"*.cjs",
|
|
9
|
-
"!.eslintrc.cjs"
|
|
11
|
+
"/dist/"
|
|
10
12
|
],
|
|
11
13
|
"exports": {
|
|
12
14
|
".": {
|
|
13
15
|
"types": "./dist/index.d.ts",
|
|
14
16
|
"import": "./dist/index.mjs",
|
|
15
17
|
"require": "./dist/index.js"
|
|
16
|
-
}
|
|
17
|
-
"./dist/stylelint": {
|
|
18
|
-
"types": "./dist/stylelint.d.ts",
|
|
19
|
-
"import": "./dist/stylelint.mjs",
|
|
20
|
-
"require": "./dist/stylelint.js"
|
|
21
|
-
},
|
|
22
|
-
"./dist/cm": {
|
|
23
|
-
"types": "./dist/cm.d.ts",
|
|
24
|
-
"import": "./dist/cm.mjs",
|
|
25
|
-
"require": "./dist/cm.js"
|
|
26
|
-
},
|
|
27
|
-
"./dist/test": {
|
|
28
|
-
"types": "./dist/test.d.ts",
|
|
29
|
-
"import": "./dist/test.js",
|
|
30
|
-
"require": "./dist/test.js"
|
|
31
|
-
},
|
|
32
|
-
"./*.cjs": "./*.cjs",
|
|
33
|
-
"./package.json": "./package.json"
|
|
18
|
+
}
|
|
34
19
|
},
|
|
35
20
|
"types": "./dist/index.d.ts",
|
|
36
21
|
"sideEffects": false,
|
|
37
22
|
"scripts": {
|
|
38
23
|
"prepublishOnly": "npm run build",
|
|
39
|
-
"build": "tsc &&
|
|
24
|
+
"build": "tsc && esbuild src/index.ts --charset=utf8 --target=es2024 --format=esm --outfile=dist/index.mjs",
|
|
40
25
|
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
41
26
|
"lint": "npm run lint:ts"
|
|
42
27
|
},
|
|
43
|
-
"dependencies": {
|
|
44
|
-
"stylelint-config-recommended": "^16.0.0"
|
|
45
|
-
},
|
|
46
28
|
"devDependencies": {
|
|
29
|
+
"@bhsd/code-standard": "^1.0.0",
|
|
47
30
|
"@stylistic/eslint-plugin": "^3.1.0",
|
|
48
|
-
"@stylistic/stylelint-plugin": "^3.1.2",
|
|
49
|
-
"@types/mocha": "^10.0.10",
|
|
50
31
|
"@types/node": "^24.0.10",
|
|
51
32
|
"@typescript-eslint/eslint-plugin": "^8.35.1",
|
|
52
33
|
"@typescript-eslint/parser": "^8.35.1",
|
|
@@ -54,14 +35,14 @@
|
|
|
54
35
|
"eslint": "^8.57.1",
|
|
55
36
|
"eslint-plugin-es-x": "^8.7.0",
|
|
56
37
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
57
|
-
"eslint-plugin-jsdoc": "^
|
|
38
|
+
"eslint-plugin-jsdoc": "^52.0.2",
|
|
58
39
|
"eslint-plugin-json-es": "^1.6.0",
|
|
59
|
-
"eslint-plugin-n": "^17.20.0",
|
|
60
40
|
"eslint-plugin-promise": "^7.2.1",
|
|
61
41
|
"eslint-plugin-regexp": "^2.9.0",
|
|
62
42
|
"eslint-plugin-unicorn": "^56.0.1",
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
43
|
+
"typescript": "^5.8.2"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18.17.0"
|
|
66
47
|
}
|
|
67
48
|
}
|
package/dist/cm.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { ConfigData } from 'wikiparser-node/dist/base';
|
|
2
|
-
export interface MwConfig {
|
|
3
|
-
readonly tags: Record<string, true>;
|
|
4
|
-
tagModes: Record<string, string>;
|
|
5
|
-
urlProtocols: string;
|
|
6
|
-
functionSynonyms: [Record<string, string>, Record<string, string>];
|
|
7
|
-
doubleUnderscore: [Record<string, unknown>, Record<string, unknown>];
|
|
8
|
-
variableIDs?: string[];
|
|
9
|
-
functionHooks?: string[];
|
|
10
|
-
redirection?: string[];
|
|
11
|
-
}
|
|
12
|
-
export interface MagicWord {
|
|
13
|
-
name: string;
|
|
14
|
-
aliases: string[];
|
|
15
|
-
'case-sensitive': boolean;
|
|
16
|
-
}
|
|
17
|
-
export type MagicRule = (word: MagicWord) => boolean;
|
|
18
|
-
declare interface Keywords {
|
|
19
|
-
img: Record<string, string>;
|
|
20
|
-
redirection: string[];
|
|
21
|
-
}
|
|
22
|
-
export declare const otherParserFunctions: Set<string>;
|
|
23
|
-
/**
|
|
24
|
-
* 将魔术字信息转换为CodeMirror接受的设置
|
|
25
|
-
* @param magicWords 完整魔术字列表
|
|
26
|
-
* @param rule 过滤函数
|
|
27
|
-
* @param flip 是否反向筛选对大小写敏感的魔术字
|
|
28
|
-
*/
|
|
29
|
-
export declare const getConfig: (magicWords: MagicWord[], rule: MagicRule, flip?: boolean) => Record<string, string>;
|
|
30
|
-
/**
|
|
31
|
-
* 将MwConfig转换为Config
|
|
32
|
-
* @param minConfig 基础Config
|
|
33
|
-
* @param mwConfig MwConfig
|
|
34
|
-
*/
|
|
35
|
-
export declare const getParserConfig: (minConfig: ConfigData, mwConfig: MwConfig) => ConfigData;
|
|
36
|
-
/**
|
|
37
|
-
* 获取语言变体
|
|
38
|
-
* @param variants 语言变体列表
|
|
39
|
-
*/
|
|
40
|
-
export declare const getVariants: (variants: {
|
|
41
|
-
code: string;
|
|
42
|
-
}[] | undefined) => string[];
|
|
43
|
-
/**
|
|
44
|
-
* 获取图片和重定向关键字
|
|
45
|
-
* @param magicwords 魔术字列表
|
|
46
|
-
* @param web 是否用于网页
|
|
47
|
-
*/
|
|
48
|
-
export declare const getKeywords: (magicwords: MagicWord[], web?: boolean) => Keywords;
|
|
49
|
-
export {};
|