@bhsd/common 0.7.0 → 0.8.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/cm.d.ts +46 -0
- package/dist/cm.js +71 -0
- package/dist/cm.mjs +51 -0
- package/package.json +11 -6
package/dist/cm.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Config } from 'wikiparser-node/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
|
+
}
|
|
10
|
+
export interface MagicWord {
|
|
11
|
+
name: string;
|
|
12
|
+
aliases: string[];
|
|
13
|
+
'case-sensitive': boolean;
|
|
14
|
+
}
|
|
15
|
+
export type MagicRule = (word: MagicWord) => boolean;
|
|
16
|
+
declare interface Keywords {
|
|
17
|
+
img: Record<string, string>;
|
|
18
|
+
redirection: string[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 将魔术字信息转换为CodeMirror接受的设置
|
|
22
|
+
* @param magicWords 完整魔术字列表
|
|
23
|
+
* @param rule 过滤函数
|
|
24
|
+
* @param flip 是否反向筛选对大小写敏感的魔术字
|
|
25
|
+
*/
|
|
26
|
+
export declare const getConfig: (magicWords: MagicWord[], rule: MagicRule, flip?: boolean) => Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* 将MwConfig转换为Config
|
|
29
|
+
* @param minConfig 基础Config
|
|
30
|
+
* @param mwConfig MwConfig
|
|
31
|
+
*/
|
|
32
|
+
export declare const getParserConfig: (minConfig: Config, mwConfig: MwConfig) => Config;
|
|
33
|
+
/**
|
|
34
|
+
* 获取语言变体
|
|
35
|
+
* @param variants 语言变体列表
|
|
36
|
+
*/
|
|
37
|
+
export declare const getVariants: (variants: {
|
|
38
|
+
code: string;
|
|
39
|
+
}[] | undefined) => string[];
|
|
40
|
+
/**
|
|
41
|
+
* 获取图片和重定向关键字
|
|
42
|
+
* @param magicwords 魔术字列表
|
|
43
|
+
* @param web 是否用于网页
|
|
44
|
+
*/
|
|
45
|
+
export declare const getKeywords: (magicwords: MagicWord[], web?: boolean) => Keywords;
|
|
46
|
+
export {};
|
package/dist/cm.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var cm_exports = {};
|
|
20
|
+
__export(cm_exports, {
|
|
21
|
+
getConfig: () => getConfig,
|
|
22
|
+
getKeywords: () => getKeywords,
|
|
23
|
+
getParserConfig: () => getParserConfig,
|
|
24
|
+
getVariants: () => getVariants
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(cm_exports);
|
|
27
|
+
const getConfig = (magicWords, rule, flip) => {
|
|
28
|
+
const words = magicWords.filter(rule);
|
|
29
|
+
return Object.fromEntries(
|
|
30
|
+
(flip === void 0 ? words : words.filter(({ "case-sensitive": i }) => i !== flip)).flatMap(({ aliases, name: n, "case-sensitive": i }) => aliases.map((alias) => ({
|
|
31
|
+
alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
|
|
32
|
+
name: n
|
|
33
|
+
}))).map(({ alias, name: n }) => [alias, n])
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
const getParserConfig = (minConfig, mwConfig) => {
|
|
37
|
+
const { tags, doubleUnderscore, urlProtocols, functionSynonyms, variableIDs } = mwConfig, [insensitive, sensitive] = functionSynonyms, behaviorSwitch = doubleUnderscore.map(
|
|
38
|
+
(obj, i) => Object.entries(obj).map(([k, v]) => [
|
|
39
|
+
k.slice(2, -2),
|
|
40
|
+
i && typeof v === "string" ? v.toUpperCase() : v
|
|
41
|
+
])
|
|
42
|
+
);
|
|
43
|
+
for (const [k, v] of Object.entries(insensitive)) {
|
|
44
|
+
if (k in sensitive) {
|
|
45
|
+
delete insensitive[k];
|
|
46
|
+
} else {
|
|
47
|
+
insensitive[k] = v.toLowerCase();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
...minConfig,
|
|
52
|
+
ext: Object.keys(tags),
|
|
53
|
+
parserFunction: [insensitive, { ...sensitive, "=": "=" }, [], []],
|
|
54
|
+
doubleUnderscore: [
|
|
55
|
+
...behaviorSwitch.map((entries) => entries.map(([k]) => k)),
|
|
56
|
+
...behaviorSwitch.map(Object.fromEntries)
|
|
57
|
+
],
|
|
58
|
+
protocol: urlProtocols.replace(/\|\\?\/\\?\/$|\\(?=[:/])/gu, ""),
|
|
59
|
+
...variableIDs && { variable: variableIDs }
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const getVariants = (variants) => variants?.map(({ code }) => code) ?? [];
|
|
63
|
+
const getKeywords = (magicwords, web) => ({
|
|
64
|
+
img: Object.fromEntries(
|
|
65
|
+
magicwords.filter(({ name: n }) => n.startsWith("img_") && n !== "img_lossy").flatMap(({ name: n, aliases }) => {
|
|
66
|
+
const k = web ? n : n.slice(4).replace(/_/gu, "-");
|
|
67
|
+
return (n === "img_alt" ? aliases.filter((alias) => alias.includes("$1")) : aliases).map((alias) => [alias, k]);
|
|
68
|
+
})
|
|
69
|
+
),
|
|
70
|
+
redirection: magicwords.find(({ name: n }) => n === "redirect").aliases.map((s) => s.toLowerCase())
|
|
71
|
+
});
|
package/dist/cm.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const getConfig = (magicWords, rule, flip) => {
|
|
2
|
+
const words = magicWords.filter(rule);
|
|
3
|
+
return Object.fromEntries(
|
|
4
|
+
(flip === void 0 ? words : words.filter(({ "case-sensitive": i }) => i !== flip)).flatMap(({ aliases, name: n, "case-sensitive": i }) => aliases.map((alias) => ({
|
|
5
|
+
alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
|
|
6
|
+
name: n
|
|
7
|
+
}))).map(({ alias, name: n }) => [alias, n])
|
|
8
|
+
);
|
|
9
|
+
};
|
|
10
|
+
const getParserConfig = (minConfig, mwConfig) => {
|
|
11
|
+
const { tags, doubleUnderscore, urlProtocols, functionSynonyms, variableIDs } = mwConfig, [insensitive, sensitive] = functionSynonyms, behaviorSwitch = doubleUnderscore.map(
|
|
12
|
+
(obj, i) => Object.entries(obj).map(([k, v]) => [
|
|
13
|
+
k.slice(2, -2),
|
|
14
|
+
i && typeof v === "string" ? v.toUpperCase() : v
|
|
15
|
+
])
|
|
16
|
+
);
|
|
17
|
+
for (const [k, v] of Object.entries(insensitive)) {
|
|
18
|
+
if (k in sensitive) {
|
|
19
|
+
delete insensitive[k];
|
|
20
|
+
} else {
|
|
21
|
+
insensitive[k] = v.toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
...minConfig,
|
|
26
|
+
ext: Object.keys(tags),
|
|
27
|
+
parserFunction: [insensitive, { ...sensitive, "=": "=" }, [], []],
|
|
28
|
+
doubleUnderscore: [
|
|
29
|
+
...behaviorSwitch.map((entries) => entries.map(([k]) => k)),
|
|
30
|
+
...behaviorSwitch.map(Object.fromEntries)
|
|
31
|
+
],
|
|
32
|
+
protocol: urlProtocols.replace(/\|\\?\/\\?\/$|\\(?=[:/])/gu, ""),
|
|
33
|
+
...variableIDs && { variable: variableIDs }
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const getVariants = (variants) => variants?.map(({ code }) => code) ?? [];
|
|
37
|
+
const getKeywords = (magicwords, web) => ({
|
|
38
|
+
img: Object.fromEntries(
|
|
39
|
+
magicwords.filter(({ name: n }) => n.startsWith("img_") && n !== "img_lossy").flatMap(({ name: n, aliases }) => {
|
|
40
|
+
const k = web ? n : n.slice(4).replace(/_/gu, "-");
|
|
41
|
+
return (n === "img_alt" ? aliases.filter((alias) => alias.includes("$1")) : aliases).map((alias) => [alias, k]);
|
|
42
|
+
})
|
|
43
|
+
),
|
|
44
|
+
redirection: magicwords.find(({ name: n }) => n === "redirect").aliases.map((s) => s.toLowerCase())
|
|
45
|
+
});
|
|
46
|
+
export {
|
|
47
|
+
getConfig,
|
|
48
|
+
getKeywords,
|
|
49
|
+
getParserConfig,
|
|
50
|
+
getVariants
|
|
51
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bhsd/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Bhsd",
|
|
6
6
|
"files": [
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"import": "./dist/stylelint.mjs",
|
|
20
20
|
"require": "./dist/stylelint.js"
|
|
21
21
|
},
|
|
22
|
+
"./dist/cm": {
|
|
23
|
+
"types": "./dist/cm.d.ts",
|
|
24
|
+
"import": "./dist/cm.mjs",
|
|
25
|
+
"require": "./dist/cm.js"
|
|
26
|
+
},
|
|
22
27
|
"./*.cjs": "./*.cjs",
|
|
23
28
|
"./package.json": "./package.json"
|
|
24
29
|
},
|
|
@@ -29,14 +34,12 @@
|
|
|
29
34
|
},
|
|
30
35
|
"scripts": {
|
|
31
36
|
"prepublishOnly": "npm run build",
|
|
32
|
-
"build": "tsc --emitDeclarationOnly && esbuild src
|
|
37
|
+
"build": "tsc --emitDeclarationOnly && esbuild src/* --charset=utf8 --target=es2024 --format=cjs --outdir=dist && esbuild src/* --charset=utf8 --target=es2024 --format=esm --outdir=dist --out-extension:.js=.mjs && rm dist/global.*",
|
|
33
38
|
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
34
39
|
"lint": "npm run lint:ts"
|
|
35
40
|
},
|
|
36
41
|
"dependencies": {
|
|
37
|
-
"stylelint": "^
|
|
38
|
-
"stylelint-config-recommended": "^15.0.0",
|
|
39
|
-
"wikiparser-node": "^1.17.0"
|
|
42
|
+
"stylelint-config-recommended": "^15.0.0"
|
|
40
43
|
},
|
|
41
44
|
"devDependencies": {
|
|
42
45
|
"@stylistic/eslint-plugin": "^3.1.0",
|
|
@@ -58,6 +61,8 @@
|
|
|
58
61
|
"eslint-plugin-unicorn": "^56.0.1",
|
|
59
62
|
"http-server": "^14.1.1",
|
|
60
63
|
"mocha": "^11.1.0",
|
|
61
|
-
"
|
|
64
|
+
"stylelint": "^16.14.1",
|
|
65
|
+
"typescript": "^5.7.3",
|
|
66
|
+
"wikiparser-node": "^1.17.1"
|
|
62
67
|
}
|
|
63
68
|
}
|