@bhsd/common 0.7.0 → 0.8.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/bin/dev.cjs CHANGED
@@ -4,9 +4,11 @@
4
4
  const fs = require('fs'),
5
5
  {devDependencies, version} = require('@bhsd/common/package.json'),
6
6
  json = require(`${process.cwd()}/package.json`);
7
- const {dependencies} = json,
7
+ const {name: n, dependencies, optionalDependencies} = json,
8
+ others = {[n]: '', ...dependencies, ...optionalDependencies},
8
9
  dev = Object.fromEntries(
9
- [...Object.entries({...json.devDependencies, ...devDependencies})].sort(([a], [b]) => a.localeCompare(b)),
10
+ Object.entries({...json.devDependencies, ...devDependencies}).filter(([k]) => !(k in others))
11
+ .sort(([a], [b]) => a.localeCompare(b)),
10
12
  );
11
13
  (dependencies && '@bhsd/common' in dependencies ? dependencies : dev)['@bhsd/common'] = `^${version}`;
12
14
  json.devDependencies = dev;
package/dist/cm.d.ts ADDED
@@ -0,0 +1,47 @@
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
+ export declare const otherParserFunctions: Set<string>;
21
+ /**
22
+ * 将魔术字信息转换为CodeMirror接受的设置
23
+ * @param magicWords 完整魔术字列表
24
+ * @param rule 过滤函数
25
+ * @param flip 是否反向筛选对大小写敏感的魔术字
26
+ */
27
+ export declare const getConfig: (magicWords: MagicWord[], rule: MagicRule, flip?: boolean) => Record<string, string>;
28
+ /**
29
+ * 将MwConfig转换为Config
30
+ * @param minConfig 基础Config
31
+ * @param mwConfig MwConfig
32
+ */
33
+ export declare const getParserConfig: (minConfig: Config, mwConfig: MwConfig) => Config;
34
+ /**
35
+ * 获取语言变体
36
+ * @param variants 语言变体列表
37
+ */
38
+ export declare const getVariants: (variants: {
39
+ code: string;
40
+ }[] | undefined) => string[];
41
+ /**
42
+ * 获取图片和重定向关键字
43
+ * @param magicwords 魔术字列表
44
+ * @param web 是否用于网页
45
+ */
46
+ export declare const getKeywords: (magicwords: MagicWord[], web?: boolean) => Keywords;
47
+ export {};
package/dist/cm.js ADDED
@@ -0,0 +1,73 @@
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
+ otherParserFunctions: () => otherParserFunctions
26
+ });
27
+ module.exports = __toCommonJS(cm_exports);
28
+ const otherParserFunctions = /* @__PURE__ */ new Set(["msg", "raw", "subst", "safesubst"]);
29
+ const getConfig = (magicWords, rule, flip) => {
30
+ const words = magicWords.filter(rule);
31
+ return Object.fromEntries(
32
+ (flip === void 0 ? words : words.filter(({ "case-sensitive": i }) => i !== flip)).flatMap(({ aliases, name: n, "case-sensitive": i }) => aliases.map((alias) => ({
33
+ alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
34
+ name: n
35
+ }))).map(({ alias, name: n }) => [alias, n])
36
+ );
37
+ };
38
+ const getParserConfig = (minConfig, mwConfig) => {
39
+ const { tags, doubleUnderscore, urlProtocols, functionSynonyms, variableIDs } = mwConfig, [insensitive, sensitive] = functionSynonyms, behaviorSwitch = doubleUnderscore.map(
40
+ (obj, i) => Object.entries(obj).map(([k, v]) => [
41
+ k.slice(2, -2),
42
+ i && typeof v === "string" ? v.toUpperCase() : v
43
+ ])
44
+ );
45
+ for (const [k, v] of Object.entries(insensitive)) {
46
+ if (k in sensitive) {
47
+ delete insensitive[k];
48
+ } else {
49
+ insensitive[k] = v.toLowerCase();
50
+ }
51
+ }
52
+ return {
53
+ ...minConfig,
54
+ ext: Object.keys(tags),
55
+ parserFunction: [{ ...insensitive }, { ...sensitive, "=": "=" }, [], []],
56
+ doubleUnderscore: [
57
+ ...behaviorSwitch.map((entries) => entries.map(([k]) => k)),
58
+ ...behaviorSwitch.map(Object.fromEntries)
59
+ ],
60
+ protocol: urlProtocols.replace(/\|\\?\/\\?\/$|\\(?=[:/])/gu, ""),
61
+ ...variableIDs && { variable: [.../* @__PURE__ */ new Set([...variableIDs, "="])] }
62
+ };
63
+ };
64
+ const getVariants = (variants) => variants?.map(({ code }) => code) ?? [];
65
+ const getKeywords = (magicwords, web) => ({
66
+ img: Object.fromEntries(
67
+ magicwords.filter(({ name: n }) => n.startsWith("img_") && n !== "img_lossy").flatMap(({ name: n, aliases }) => {
68
+ const k = web ? n : n.slice(4).replace(/_/gu, "-");
69
+ return (n === "img_alt" ? aliases.filter((alias) => alias.includes("$1")) : aliases).map((alias) => [alias, k]);
70
+ })
71
+ ),
72
+ redirection: magicwords.find(({ name: n }) => n === "redirect").aliases.map((s) => s.toLowerCase())
73
+ });
package/dist/cm.mjs ADDED
@@ -0,0 +1,53 @@
1
+ const otherParserFunctions = /* @__PURE__ */ new Set(["msg", "raw", "subst", "safesubst"]);
2
+ const getConfig = (magicWords, rule, flip) => {
3
+ const words = magicWords.filter(rule);
4
+ return Object.fromEntries(
5
+ (flip === void 0 ? words : words.filter(({ "case-sensitive": i }) => i !== flip)).flatMap(({ aliases, name: n, "case-sensitive": i }) => aliases.map((alias) => ({
6
+ alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
7
+ name: n
8
+ }))).map(({ alias, name: n }) => [alias, n])
9
+ );
10
+ };
11
+ const getParserConfig = (minConfig, mwConfig) => {
12
+ const { tags, doubleUnderscore, urlProtocols, functionSynonyms, variableIDs } = mwConfig, [insensitive, sensitive] = functionSynonyms, behaviorSwitch = doubleUnderscore.map(
13
+ (obj, i) => Object.entries(obj).map(([k, v]) => [
14
+ k.slice(2, -2),
15
+ i && typeof v === "string" ? v.toUpperCase() : v
16
+ ])
17
+ );
18
+ for (const [k, v] of Object.entries(insensitive)) {
19
+ if (k in sensitive) {
20
+ delete insensitive[k];
21
+ } else {
22
+ insensitive[k] = v.toLowerCase();
23
+ }
24
+ }
25
+ return {
26
+ ...minConfig,
27
+ ext: Object.keys(tags),
28
+ parserFunction: [{ ...insensitive }, { ...sensitive, "=": "=" }, [], []],
29
+ doubleUnderscore: [
30
+ ...behaviorSwitch.map((entries) => entries.map(([k]) => k)),
31
+ ...behaviorSwitch.map(Object.fromEntries)
32
+ ],
33
+ protocol: urlProtocols.replace(/\|\\?\/\\?\/$|\\(?=[:/])/gu, ""),
34
+ ...variableIDs && { variable: [.../* @__PURE__ */ new Set([...variableIDs, "="])] }
35
+ };
36
+ };
37
+ const getVariants = (variants) => variants?.map(({ code }) => code) ?? [];
38
+ const getKeywords = (magicwords, web) => ({
39
+ img: Object.fromEntries(
40
+ magicwords.filter(({ name: n }) => n.startsWith("img_") && n !== "img_lossy").flatMap(({ name: n, aliases }) => {
41
+ const k = web ? n : n.slice(4).replace(/_/gu, "-");
42
+ return (n === "img_alt" ? aliases.filter((alias) => alias.includes("$1")) : aliases).map((alias) => [alias, k]);
43
+ })
44
+ ),
45
+ redirection: magicwords.find(({ name: n }) => n === "redirect").aliases.map((s) => s.toLowerCase())
46
+ });
47
+ export {
48
+ getConfig,
49
+ getKeywords,
50
+ getParserConfig,
51
+ getVariants,
52
+ otherParserFunctions
53
+ };
package/dist/index.js CHANGED
@@ -61,7 +61,7 @@ const splitColors = (str, hsl = true) => {
61
61
  pieces.push([str.slice(lastIndex, index), lastIndex, index, false]);
62
62
  }
63
63
  ({ lastIndex } = re);
64
- pieces.push([mt[2], index, lastIndex, str.charAt(index - 1) !== "&" || !/^#\d+$/u.test(mt[2])]);
64
+ pieces.push([mt[2], index, lastIndex, str[index - 1] !== "&" || !/^#\d+$/u.test(mt[2])]);
65
65
  mt = re.exec(str);
66
66
  }
67
67
  if (str.length > lastIndex) {
package/dist/index.mjs CHANGED
@@ -27,7 +27,7 @@ const splitColors = (str, hsl = true) => {
27
27
  pieces.push([str.slice(lastIndex, index), lastIndex, index, false]);
28
28
  }
29
29
  ({ lastIndex } = re);
30
- pieces.push([mt[2], index, lastIndex, str.charAt(index - 1) !== "&" || !/^#\d+$/u.test(mt[2])]);
30
+ pieces.push([mt[2], index, lastIndex, str[index - 1] !== "&" || !/^#\d+$/u.test(mt[2])]);
31
31
  mt = re.exec(str);
32
32
  }
33
33
  if (str.length > lastIndex) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
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/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",
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": "^16.14.1",
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
- "typescript": "^5.7.3"
64
+ "stylelint": "^16.14.1",
65
+ "typescript": "^5.7.3",
66
+ "wikiparser-node": "^1.18.1"
62
67
  }
63
68
  }