@bhsd/common 0.6.6 → 0.7.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 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
  });
@@ -67,30 +69,38 @@ const splitColors = (str, hsl = true) => {
67
69
  }
68
70
  return pieces;
69
71
  };
70
- const loadScript = (src, globalConst, amd) => new Promise((resolve) => {
71
- const path = `${CDN}/${src}`;
72
- let obj = globalThis;
73
- for (const prop of globalConst.split(".")) {
74
- obj = obj?.[prop];
72
+ const loading = /* @__PURE__ */ new Map();
73
+ const loadScript = (src, globalConst, amd) => {
74
+ if (loading.has(src)) {
75
+ return loading.get(src);
75
76
  }
76
- if (obj) {
77
- resolve();
78
- } else if (amd && typeof define === "function" && "amd" in define) {
79
- const requirejs = globalThis.require;
80
- requirejs.config({ paths: { [globalConst]: path } });
81
- requirejs([globalConst], (exports) => {
82
- Object.assign(globalThis, { [globalConst]: exports });
83
- resolve();
84
- });
85
- } else {
86
- const script = document.createElement("script");
87
- script.src = path;
88
- script.onload = () => {
77
+ const promise = new Promise((resolve) => {
78
+ const path = `${CDN}/${src}`;
79
+ let obj = globalThis;
80
+ for (const prop of globalConst.split(".")) {
81
+ obj = obj?.[prop];
82
+ }
83
+ if (obj) {
89
84
  resolve();
90
- };
91
- document.head.append(script);
92
- }
93
- });
85
+ } else if (amd && typeof define === "function" && "amd" in define) {
86
+ const requirejs = globalThis.require;
87
+ requirejs.config({ paths: { [globalConst]: path } });
88
+ requirejs([globalConst], (exports) => {
89
+ Object.assign(globalThis, { [globalConst]: exports });
90
+ resolve();
91
+ });
92
+ } else {
93
+ const script = document.createElement("script");
94
+ script.src = path;
95
+ script.onload = () => {
96
+ resolve();
97
+ };
98
+ document.head.append(script);
99
+ }
100
+ });
101
+ loading.set(src, promise);
102
+ return promise;
103
+ };
94
104
  const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
95
105
  const setObject = (key, value) => {
96
106
  localStorage.setItem(key, JSON.stringify(value));
@@ -100,3 +110,16 @@ const compareVersion = (version, baseVersion) => {
100
110
  const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
101
111
  return major > baseMajor || major === baseMajor && minor >= baseMinor;
102
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
@@ -35,30 +35,38 @@ const splitColors = (str, hsl = true) => {
35
35
  }
36
36
  return pieces;
37
37
  };
38
- const loadScript = (src, globalConst, amd) => new Promise((resolve) => {
39
- const path = `${CDN}/${src}`;
40
- let obj = globalThis;
41
- for (const prop of globalConst.split(".")) {
42
- obj = obj?.[prop];
38
+ const loading = /* @__PURE__ */ new Map();
39
+ const loadScript = (src, globalConst, amd) => {
40
+ if (loading.has(src)) {
41
+ return loading.get(src);
43
42
  }
44
- if (obj) {
45
- resolve();
46
- } else if (amd && typeof define === "function" && "amd" in define) {
47
- const requirejs = globalThis.require;
48
- requirejs.config({ paths: { [globalConst]: path } });
49
- requirejs([globalConst], (exports) => {
50
- Object.assign(globalThis, { [globalConst]: exports });
51
- resolve();
52
- });
53
- } else {
54
- const script = document.createElement("script");
55
- script.src = path;
56
- script.onload = () => {
43
+ const promise = new Promise((resolve) => {
44
+ const path = `${CDN}/${src}`;
45
+ let obj = globalThis;
46
+ for (const prop of globalConst.split(".")) {
47
+ obj = obj?.[prop];
48
+ }
49
+ if (obj) {
57
50
  resolve();
58
- };
59
- document.head.append(script);
60
- }
61
- });
51
+ } else if (amd && typeof define === "function" && "amd" in define) {
52
+ const requirejs = globalThis.require;
53
+ requirejs.config({ paths: { [globalConst]: path } });
54
+ requirejs([globalConst], (exports) => {
55
+ Object.assign(globalThis, { [globalConst]: exports });
56
+ resolve();
57
+ });
58
+ } else {
59
+ const script = document.createElement("script");
60
+ script.src = path;
61
+ script.onload = () => {
62
+ resolve();
63
+ };
64
+ document.head.append(script);
65
+ }
66
+ });
67
+ loading.set(src, promise);
68
+ return promise;
69
+ };
62
70
  const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
63
71
  const setObject = (key, value) => {
64
72
  localStorage.setItem(key, JSON.stringify(value));
@@ -68,15 +76,30 @@ const compareVersion = (version, baseVersion) => {
68
76
  const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
69
77
  return major > baseMajor || major === baseMajor && minor >= baseMinor;
70
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 === "{" ? "{" : "}");
71
92
  export {
72
93
  CDN,
73
94
  compareVersion,
74
95
  decodeHTML,
96
+ getLSP,
75
97
  getObject,
76
98
  loadScript,
77
99
  normalizeTitle,
78
100
  numToHex,
79
101
  rawurldecode,
102
+ sanitizeInlineStyle,
80
103
  setObject,
81
104
  splitColors
82
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/eslintrc.node.cjs CHANGED
@@ -28,6 +28,12 @@ module.exports = {
28
28
  'n/no-mixed-requires': 2,
29
29
  'n/no-new-require': 2,
30
30
  'n/no-path-concat': 2,
31
+ 'n/no-unsupported-features/node-builtins': [
32
+ 2,
33
+ {
34
+ ignores: ['fetch'],
35
+ },
36
+ ],
31
37
  },
32
38
  settings: {
33
39
  ...config.settings,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "0.6.6",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "author": "Bhsd",
6
6
  "files": [
@@ -14,6 +14,11 @@
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
+ },
17
22
  "./*.cjs": "./*.cjs",
18
23
  "./package.json": "./package.json"
19
24
  },
@@ -24,10 +29,15 @@
24
29
  },
25
30
  "scripts": {
26
31
  "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",
32
+ "build": "tsc --emitDeclarationOnly && esbuild src/index.ts src/stylelint.ts --charset=utf8 --target=es2024 --format=cjs --outdir=dist && esbuild src/index.ts src/stylelint.ts --charset=utf8 --target=es2024 --format=esm --outdir=dist --out-extension:.js=.mjs",
28
33
  "lint:ts": "tsc --noEmit && eslint --cache .",
29
34
  "lint": "npm run lint:ts"
30
35
  },
36
+ "dependencies": {
37
+ "stylelint": "^16.14.1",
38
+ "stylelint-config-recommended": "^15.0.0",
39
+ "wikiparser-node": "^1.17.0"
40
+ },
31
41
  "devDependencies": {
32
42
  "@stylistic/eslint-plugin": "^3.1.0",
33
43
  "@stylistic/stylelint-plugin": "^3.1.2",
@@ -48,8 +58,6 @@
48
58
  "eslint-plugin-unicorn": "^56.0.1",
49
59
  "http-server": "^14.1.1",
50
60
  "mocha": "^11.1.0",
51
- "stylelint": "^16.14.1",
52
- "stylelint-config-recommended": "^15.0.0",
53
61
  "typescript": "^5.7.3"
54
62
  }
55
63
  }