@bhsd/common 0.6.7 → 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 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/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { LanguageServiceBase } from 'wikiparser-node/extensions/typings';
1
2
  export declare const CDN = "https://testingcf.jsdelivr.net";
2
3
  declare global {
3
4
  const define: unknown;
@@ -43,3 +44,14 @@ export declare const setObject: (key: string, value: unknown) => void;
43
44
  * @param baseVersion 基础版本号
44
45
  */
45
46
  export declare const compareVersion: (version: string, baseVersion: string) => boolean;
47
+ /**
48
+ * 获取LSP
49
+ * @param obj 关联对象
50
+ * @param include 是否嵌入
51
+ */
52
+ export declare const getLSP: (obj: object, include?: boolean) => LanguageServiceBase | undefined;
53
+ /**
54
+ * 清理内联样式中的`{`和`}`
55
+ * @param style 内联样式
56
+ */
57
+ export declare const sanitizeInlineStyle: (style: string) => string;
package/dist/index.js CHANGED
@@ -21,11 +21,13 @@ __export(index_exports, {
21
21
  CDN: () => CDN,
22
22
  compareVersion: () => compareVersion,
23
23
  decodeHTML: () => decodeHTML,
24
+ getLSP: () => getLSP,
24
25
  getObject: () => getObject,
25
26
  loadScript: () => loadScript,
26
27
  normalizeTitle: () => normalizeTitle,
27
28
  numToHex: () => numToHex,
28
29
  rawurldecode: () => rawurldecode,
30
+ sanitizeInlineStyle: () => sanitizeInlineStyle,
29
31
  setObject: () => setObject,
30
32
  splitColors: () => splitColors
31
33
  });
@@ -108,3 +110,16 @@ const compareVersion = (version, baseVersion) => {
108
110
  const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
109
111
  return major > baseMajor || major === baseMajor && minor >= baseMinor;
110
112
  };
113
+ const lsps = /* @__PURE__ */ new WeakMap();
114
+ const getLSP = (obj, include) => {
115
+ const path = "npm/wikiparser-node/extensions/dist";
116
+ void loadScript(`${path}/base.min.js`, "wikiparse");
117
+ void loadScript(`${path}/lsp.min.js`, "wikiparse.LanguageService");
118
+ if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
119
+ return lsps.get(obj);
120
+ }
121
+ const lsp = new wikiparse.LanguageService(include);
122
+ lsps.set(obj, lsp);
123
+ return lsp;
124
+ };
125
+ const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, (p) => p === "{" ? "{" : "}");
package/dist/index.mjs CHANGED
@@ -76,15 +76,30 @@ 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
+ const lsps = /* @__PURE__ */ new WeakMap();
80
+ const getLSP = (obj, include) => {
81
+ const path = "npm/wikiparser-node/extensions/dist";
82
+ void loadScript(`${path}/base.min.js`, "wikiparse");
83
+ void loadScript(`${path}/lsp.min.js`, "wikiparse.LanguageService");
84
+ if (typeof wikiparse !== "object" || !wikiparse.LanguageService || lsps.has(obj)) {
85
+ return lsps.get(obj);
86
+ }
87
+ const lsp = new wikiparse.LanguageService(include);
88
+ lsps.set(obj, lsp);
89
+ return lsp;
90
+ };
91
+ const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, (p) => p === "{" ? "{" : "}");
79
92
  export {
80
93
  CDN,
81
94
  compareVersion,
82
95
  decodeHTML,
96
+ getLSP,
83
97
  getObject,
84
98
  loadScript,
85
99
  normalizeTitle,
86
100
  numToHex,
87
101
  rawurldecode,
102
+ sanitizeInlineStyle,
88
103
  setObject,
89
104
  splitColors
90
105
  };
@@ -0,0 +1,8 @@
1
+ import type { PublicApi, Warning } from 'stylelint';
2
+ /**
3
+ * 使用Stylelint检查CSS代码
4
+ * @param stylelint Stylelint实例
5
+ * @param code CSS代码
6
+ * @param additionalRules 额外的规则
7
+ */
8
+ export declare const styleLint: (stylelint: PublicApi, code: string, additionalRules?: Record<string, unknown>) => Promise<Warning[]>;
@@ -0,0 +1,30 @@
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 stylelint_exports = {};
20
+ __export(stylelint_exports, {
21
+ styleLint: () => styleLint
22
+ });
23
+ module.exports = __toCommonJS(stylelint_exports);
24
+ var import_stylelint_config_recommended = require("stylelint-config-recommended");
25
+ const styleLint = async (stylelint, code, additionalRules) => {
26
+ const config = {
27
+ rules: { ...import_stylelint_config_recommended.rules, ...additionalRules }
28
+ };
29
+ return (await stylelint.lint({ code, config })).results.flatMap(({ warnings }) => warnings).filter(({ text }) => !text.startsWith("Unknown rule "));
30
+ };
@@ -0,0 +1,10 @@
1
+ import { rules } from "stylelint-config-recommended";
2
+ const styleLint = async (stylelint, code, additionalRules) => {
3
+ const config = {
4
+ rules: { ...rules, ...additionalRules }
5
+ };
6
+ return (await stylelint.lint({ code, config })).results.flatMap(({ warnings }) => warnings).filter(({ text }) => !text.startsWith("Unknown rule "));
7
+ };
8
+ export {
9
+ styleLint
10
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "0.6.7",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "author": "Bhsd",
6
6
  "files": [
@@ -14,6 +14,16 @@
14
14
  "import": "./dist/index.mjs",
15
15
  "require": "./dist/index.js"
16
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
+ },
17
27
  "./*.cjs": "./*.cjs",
18
28
  "./package.json": "./package.json"
19
29
  },
@@ -24,10 +34,13 @@
24
34
  },
25
35
  "scripts": {
26
36
  "prepublishOnly": "npm run build",
27
- "build": "tsc --emitDeclarationOnly && esbuild src/index.ts --charset=utf8 --target=es2024 --format=cjs --outfile=dist/index.js && esbuild src/index.ts --charset=utf8 --target=es2024 --format=esm --outfile=dist/index.mjs",
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.*",
28
38
  "lint:ts": "tsc --noEmit && eslint --cache .",
29
39
  "lint": "npm run lint:ts"
30
40
  },
41
+ "dependencies": {
42
+ "stylelint-config-recommended": "^15.0.0"
43
+ },
31
44
  "devDependencies": {
32
45
  "@stylistic/eslint-plugin": "^3.1.0",
33
46
  "@stylistic/stylelint-plugin": "^3.1.2",
@@ -49,7 +62,7 @@
49
62
  "http-server": "^14.1.1",
50
63
  "mocha": "^11.1.0",
51
64
  "stylelint": "^16.14.1",
52
- "stylelint-config-recommended": "^15.0.0",
53
- "typescript": "^5.7.3"
65
+ "typescript": "^5.7.3",
66
+ "wikiparser-node": "^1.17.1"
54
67
  }
55
68
  }