@bhsd/codemirror-mediawiki 2.23.0 → 2.23.2

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/mwConfig.mjs CHANGED
@@ -10,12 +10,64 @@ var compareVersion = (version, baseVersion) => {
10
10
  return major > baseMajor || major === baseMajor && minor >= baseMinor;
11
11
  };
12
12
 
13
+ // ../code-standard/dist/cm.mjs
14
+ var otherParserFunctions = /* @__PURE__ */ new Set(["msg", "raw", "subst", "safesubst"]);
15
+ var getConfig = (magicWords, rule, flip) => {
16
+ const words = magicWords.filter(rule);
17
+ return Object.fromEntries(
18
+ (flip === void 0 ? words : words.filter(({ "case-sensitive": i }) => i !== flip)).flatMap(({ aliases, name: n, "case-sensitive": i }) => aliases.map((alias) => ({
19
+ alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
20
+ name: n
21
+ }))).map(({ alias, name: n }) => [alias, n])
22
+ );
23
+ };
24
+ var getParserConfig = (minConfig, mwConfig) => {
25
+ const { tags, doubleUnderscore, urlProtocols, functionSynonyms, variableIDs } = mwConfig, [insensitive, sensitive] = functionSynonyms, behaviorSwitch = doubleUnderscore.map(
26
+ (obj, i) => Object.entries(obj).map(([k, v]) => [
27
+ k.slice(2, -2),
28
+ i && typeof v === "string" ? v.toUpperCase() : v
29
+ ])
30
+ );
31
+ for (const [k, v] of Object.entries(insensitive)) {
32
+ if (k in sensitive) {
33
+ delete insensitive[k];
34
+ } else {
35
+ insensitive[k] = v.toLowerCase();
36
+ }
37
+ }
38
+ return {
39
+ ...minConfig,
40
+ ext: Object.keys(tags),
41
+ parserFunction: [{ ...insensitive }, { ...sensitive, "=": "=" }, [], []],
42
+ doubleUnderscore: [
43
+ ...behaviorSwitch.map((entries) => entries.map(([k]) => k)),
44
+ ...behaviorSwitch.map(Object.fromEntries)
45
+ ],
46
+ protocol: urlProtocols.replace(/\|\\?\/\\?\/$|\\(?=[:/])/gu, ""),
47
+ ...variableIDs && { variable: [.../* @__PURE__ */ new Set([...variableIDs, "="])] }
48
+ };
49
+ };
50
+ var getVariants = (variants) => {
51
+ var _a2;
52
+ return (_a2 = variants == null ? void 0 : variants.map(({ code }) => code)) != null ? _a2 : [];
53
+ };
54
+ var getKeywords = (magicwords, web) => ({
55
+ img: Object.fromEntries(
56
+ magicwords.filter(({ name: n }) => n.startsWith("img_") && n !== "img_lossy").flatMap(({ name: n, aliases }) => {
57
+ const k = web ? n : n.slice(4).replace(/_/gu, "-");
58
+ return (n === "img_alt" ? aliases.filter((alias) => alias.includes("$1")) : aliases).map((alias) => [alias, k]);
59
+ })
60
+ ),
61
+ redirection: magicwords.find(({ name: n }) => n === "redirect").aliases.map((s) => s.toLowerCase())
62
+ });
63
+
13
64
  // src/static.ts
14
65
  var getStaticMwConfig = ({
15
66
  variable,
16
67
  parserFunction: [p0, p1, ...p2],
17
68
  protocol,
18
69
  nsid,
70
+ functionHook,
19
71
  variants,
20
72
  redirection,
21
73
  ext,
@@ -28,6 +80,7 @@ var getStaticMwConfig = ({
28
80
  Object.fromEntries((d2 && d0.length === 0 ? Object.keys(d2) : d0).map((s) => [`__${s}__`, true])),
29
81
  Object.fromEntries((d3 && d1.length === 0 ? Object.keys(d3) : d1).map((s) => [`__${s}__`, true]))
30
82
  ],
83
+ functionHooks: functionHook,
31
84
  variableIDs: variable,
32
85
  functionSynonyms: [
33
86
  {
@@ -49,18 +102,13 @@ var ALL_SETTINGS_CACHE = (_a = getObject("InPageEditMwConfig")) != null ? _a : {
49
102
  var SITE_ID = typeof mw === "object" ? mw.config.get("wgServerName") + mw.config.get("wgScriptPath") : location.origin;
50
103
  var SITE_SETTINGS = ALL_SETTINGS_CACHE[SITE_ID];
51
104
  var VALID = Number(SITE_SETTINGS == null ? void 0 : SITE_SETTINGS.time) > Date.now() - 86400 * 1e3 * 30;
52
- var getConfig = (magicWords, rule, flip) => Object.fromEntries(
53
- magicWords.filter(rule).filter(({ "case-sensitive": i }) => i !== flip).flatMap(({ aliases, name, "case-sensitive": i }) => aliases.map((alias) => ({
54
- alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
55
- name
56
- }))).map(({ alias, name }) => [alias, name])
57
- );
105
+ var others = /* @__PURE__ */ new Set([...otherParserFunctions, "msgnw"]);
58
106
  var getConfigPair = (magicWords, rule) => [true, false].map((bool) => getConfig(magicWords, rule, bool));
59
107
  var setConfig = (config) => {
60
108
  mw.config.set("extCodeMirrorConfig", config);
61
109
  };
62
110
  var getMwConfig = async (modes) => {
63
- var _a2;
111
+ var _a2, _b;
64
112
  if (mw.loader.getState("ext.CodeMirror") !== null && !VALID) {
65
113
  await mw.loader.using(
66
114
  mw.loader.getState("ext.CodeMirror.data") ? "ext.CodeMirror.data" : "ext.CodeMirror"
@@ -72,7 +120,7 @@ var getMwConfig = async (modes) => {
72
120
  setConfig(config);
73
121
  }
74
122
  const isIPE = config && Object.values(config.functionSynonyms[0]).includes(true), nsid = mw.config.get("wgNamespaceIds");
75
- if ((config == null ? void 0 : config.img) && config.redirection && config.variants && config.variableIDs && !isIPE) {
123
+ if ((config == null ? void 0 : config.img) && config.redirection && config.variants && config.variableIDs && config.functionHooks && !isIPE) {
76
124
  config.urlProtocols = config.urlProtocols.replace(/\\:/gu, ":");
77
125
  config.tagModes = modes;
78
126
  return { ...config, nsid };
@@ -90,11 +138,11 @@ var getMwConfig = async (modes) => {
90
138
  "general",
91
139
  "magicwords",
92
140
  ...config && !isIPE ? [] : ["extensiontags", "functionhooks"],
93
- ...(config == null ? void 0 : config.variableIDs) && !isIPE ? [] : ["variables"]
141
+ ...(config == null ? void 0 : config.variableIDs) && !isIPE ? [] : ["variables"],
142
+ ...config && !isIPE && !config.functionHooks ? ["functionhooks"] : []
94
143
  ],
95
144
  formatversion: "2"
96
145
  });
97
- const others = /* @__PURE__ */ new Set(["msg", "raw", "msgnw", "subst", "safesubst"]);
98
146
  if (config && !isIPE) {
99
147
  const { functionSynonyms: [insensitive] } = config;
100
148
  if (!("subst" in insensitive)) {
@@ -115,65 +163,55 @@ var getMwConfig = async (modes) => {
115
163
  )
116
164
  };
117
165
  }
118
- config.tagModes = modes;
119
- config.img = getConfig(magicwords, ({ name }) => name.startsWith("img_"));
120
- config.variants = variants ? variants.map(({ code }) => code) : [];
121
- config.redirection = magicwords.find(({ name }) => name === "redirect").aliases;
122
- config.urlProtocols = mw.config.get("wgUrlProtocols").replace(/\\:/gu, ":");
166
+ Object.assign(config, {
167
+ ...getKeywords(magicwords, true),
168
+ tagModes: modes,
169
+ variants: getVariants(variants),
170
+ urlProtocols: mw.config.get("wgUrlProtocols").replace(/\\:/gu, ":")
171
+ });
123
172
  (_a2 = config.variableIDs) != null ? _a2 : config.variableIDs = variables;
173
+ (_b = config.functionHooks) != null ? _b : config.functionHooks = [...functionhooks, "msgnw"];
124
174
  }
125
175
  setConfig(config);
126
176
  ALL_SETTINGS_CACHE[SITE_ID] = { config, time: Date.now() };
127
177
  setObject("InPageEditMwConfig", ALL_SETTINGS_CACHE);
128
178
  return { ...config, nsid };
129
179
  };
130
- var getParserConfig = (minConfig, mwConfig) => {
180
+ var getParserConfig2 = (minConfig, mwConfig) => {
131
181
  let config = getObject("wikilintConfig");
132
182
  if (config) {
133
183
  return config;
134
184
  }
135
- const {
136
- tags,
137
- nsid,
138
- doubleUnderscore,
139
- variants,
140
- urlProtocols,
141
- redirection,
142
- functionSynonyms,
143
- variableIDs,
144
- img
145
- } = mwConfig, [insensitive, sensitive] = functionSynonyms;
185
+ const { nsid, variants, redirection, functionSynonyms, functionHooks, img } = mwConfig, [insensitive, sensitive] = functionSynonyms;
146
186
  config = {
147
- ...minConfig,
148
- ext: Object.keys(tags),
187
+ ...getParserConfig(minConfig, mwConfig),
149
188
  namespaces: mw.config.get("wgFormattedNamespaces"),
150
189
  nsid,
151
- doubleUnderscore: doubleUnderscore.map(
152
- (obj) => Object.keys(obj).map((s) => s.slice(2, -2))
153
- ),
154
190
  variants,
155
- protocol: urlProtocols.replace(/\|\\?\/\\?\//u, ""),
156
191
  redirection: redirection != null ? redirection : minConfig.redirection,
157
- ...variableIDs && { variable: variableIDs }
192
+ ...functionHooks && { functionHook: functionHooks }
158
193
  };
159
194
  if (location.hostname.endsWith(".moegirl.org.cn")) {
160
195
  config.html[2].push("img");
161
196
  }
162
- config.parserFunction[0] = insensitive;
163
- if (mw.loader.getState("ext.CodeMirror") === null) {
164
- for (const [key, val] of Object.entries(insensitive)) {
165
- if (!key.startsWith("#")) {
166
- config.parserFunction[0][`#${key}`] = val;
167
- }
197
+ const noCM = mw.loader.getState("ext.CodeMirror") === null;
198
+ for (const [key, val] of Object.entries(insensitive)) {
199
+ if (others.has(val) && val !== "msgnw") {
200
+ delete config.parserFunction[0][key];
201
+ config.parserFunction[val === "msg" || val === "raw" ? 2 : 3].push(key);
202
+ } else if (noCM && !key.startsWith("#")) {
203
+ config.parserFunction[0][`#${key}`] = val;
168
204
  }
169
205
  }
170
- config.parserFunction[1] = typeof wikiparse === "object" && compareVersion(wikiparse.version, "1.15") && !Object.values(sensitive).includes(true) ? { ...sensitive, "=": "=" } : [...Object.keys(sensitive), "="];
206
+ if (typeof wikiparse !== "object" || !compareVersion(wikiparse.version, "1.15") || Object.values(sensitive).includes(true)) {
207
+ config.parserFunction[1] = Object.keys(config.parserFunction[1]);
208
+ }
171
209
  for (const [key, val] of Object.entries(img)) {
172
- config.img[key] = val.slice(4);
210
+ config.img[key] = val.slice(4).replace(/_/gu, "-");
173
211
  }
174
212
  return config;
175
213
  };
176
214
  export {
177
215
  getMwConfig,
178
- getParserConfig
216
+ getParserConfig2 as getParserConfig
179
217
  };
package/dist/static.d.ts CHANGED
@@ -24,4 +24,4 @@ export declare const tagModes: {
24
24
  maplink: string;
25
25
  graph: string;
26
26
  };
27
- export declare const getStaticMwConfig: ({ variable, parserFunction: [p0, p1, ...p2], protocol, nsid, variants, redirection, ext, doubleUnderscore: [d0, d1, d2, d3], img, }: Config, modes: Record<string, string>) => MwConfig;
27
+ export declare const getStaticMwConfig: ({ variable, parserFunction: [p0, p1, ...p2], protocol, nsid, functionHook, variants, redirection, ext, doubleUnderscore: [d0, d1, d2, d3], img, }: Config, modes: Record<string, string>) => MwConfig;
package/dist/token.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import { Tag } from '@lezer/highlight';
7
7
  import { tokens } from './config';
8
+ import type { MwConfig as MwConfigBase } from '@bhsd/common/dist/cm';
8
9
  import type { EditorState } from '@codemirror/state';
9
10
  import type { StreamParser, StringStream as StringStreamBase } from '@codemirror/language';
10
11
  import type { SyntaxNode } from '@lezer/common';
@@ -29,7 +30,10 @@ declare interface State extends Nesting {
29
30
  n: number;
30
31
  html: number;
31
32
  };
32
- redirect: boolean;
33
+ sof: boolean;
34
+ redirect: {
35
+ colon: boolean;
36
+ } | false;
33
37
  imgLink: boolean;
34
38
  data: MediaWikiData;
35
39
  }
@@ -52,14 +56,9 @@ export type ApiSuggestions = [string, string?][];
52
56
  * @param subpage 是否为子页面
53
57
  */
54
58
  export type ApiSuggest = (search: string, namespace?: number, subpage?: boolean) => ApiSuggestions | Promise<ApiSuggestions>;
55
- export interface MwConfig {
56
- readonly tags: Record<string, true>;
57
- tagModes: Record<string, string>;
58
- urlProtocols: string;
59
- functionSynonyms: [Record<string, string>, Record<string, string>];
60
- doubleUnderscore: [Record<string, unknown>, Record<string, unknown>];
59
+ export interface MwConfig extends MwConfigBase {
61
60
  nsid: Record<string, number>;
62
- variableIDs?: string[];
61
+ functionHooks?: string[];
63
62
  variants?: string[];
64
63
  img?: Record<string, string>;
65
64
  redirection?: string[];
@@ -67,8 +66,8 @@ export interface MwConfig {
67
66
  implicitlyClosedHtmlTags?: string[];
68
67
  linkSuggest?: ApiSuggest;
69
68
  paramSuggest?: ApiSuggest;
70
- titleParser?: (state: EditorState, node: SyntaxNode, urlProtocols: string) => true | undefined;
71
- isbnParser?: (link: string) => true;
69
+ titleParser?: (state: EditorState, node: SyntaxNode) => string | undefined;
70
+ isbnParser?: (link: string) => string;
72
71
  }
73
72
  declare class MediaWikiData {
74
73
  /** 已解析的节点 */
@@ -90,7 +89,7 @@ export declare class MediaWiki {
90
89
  [x: string]: Tag;
91
90
  };
92
91
  readonly hiddenTable: Record<string, Tag>;
93
- readonly permittedHtmlTags: Set<string>;
92
+ readonly permittedHtmlTags: Set<string | undefined>;
94
93
  readonly implicitlyClosedHtmlTags: Set<string>;
95
94
  readonly urlProtocols: RegExp;
96
95
  readonly linkRegex: RegExp;
@@ -98,16 +97,11 @@ export declare class MediaWiki {
98
97
  readonly redirectRegex: RegExp;
99
98
  readonly img: string[];
100
99
  readonly imgRegex: RegExp;
101
- readonly headerRegex: RegExp;
102
- readonly templateRegex: RegExp;
103
- readonly argumentRegex: RegExp;
104
- readonly styleRegex: RegExp;
105
100
  readonly convertSemicolon: RegExp | undefined;
106
101
  readonly convertLang: RegExp | undefined;
107
- readonly convertRegex: RegExp;
108
- readonly wikiRegex: RegExp;
109
- readonly tableDefinitionRegex: RegExp;
110
102
  readonly tags: string[];
103
+ readonly hasVariants: boolean;
104
+ readonly preRegex: [RegExp, RegExp];
111
105
  constructor(config: MwConfig);
112
106
  /**
113
107
  * Dynamically register a token in CodeMirror.
@@ -149,9 +143,9 @@ export declare class MediaWiki {
149
143
  inExtTokens(origString: string): Tokenizer;
150
144
  inVariable(pos?: number): Tokenizer;
151
145
  eatTransclusion(stream: StringStream, state: State, { length }: string): string | undefined;
152
- inParserFunctionName(invoke?: number, n?: number): Tokenizer;
146
+ inParserFunctionName(invoke?: number, n?: number, ns?: number): Tokenizer;
153
147
  inTemplatePageName(haveEaten?: boolean, anchor?: boolean): Tokenizer;
154
- inParserFunctionArgument(module?: number, n?: number): Tokenizer;
148
+ inParserFunctionArgument(module?: number, n?: number, ns?: number): Tokenizer;
155
149
  inTemplateArgument(expectName?: boolean, parserFunction?: boolean): Tokenizer;
156
150
  inConvert(style: string, needFlag?: boolean, needLang?: boolean, plain?: boolean): Tokenizer;
157
151
  eatEntity(stream: StringStream, style: string): string;