@bhsd/codemirror-mediawiki 2.19.4 → 2.19.6

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.
@@ -0,0 +1,161 @@
1
+ // ../code-standard/dist/index.mjs
2
+ var CDN = "https://testingcf.jsdelivr.net";
3
+ var span;
4
+ if (typeof document === "object") {
5
+ span = document.createElement("span");
6
+ }
7
+ var getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
8
+ var setObject = (key, value) => {
9
+ localStorage.setItem(key, JSON.stringify(value));
10
+ };
11
+
12
+ // src/static.ts
13
+ var fromEntries = (entries, obj, string) => {
14
+ for (const entry of entries) {
15
+ obj[entry] = string ? entry : true;
16
+ }
17
+ };
18
+ var getStaticMwConfig = ({ parserFunction, protocol, nsid, variants, redirection, ext, doubleUnderscore, img }, modes) => {
19
+ const mwConfig = {
20
+ tags: {},
21
+ tagModes: modes,
22
+ doubleUnderscore: [{}, {}],
23
+ functionSynonyms: [parserFunction[0], {}],
24
+ urlProtocols: `${protocol}|//`,
25
+ nsid,
26
+ img: Object.fromEntries(Object.entries(img).map(([key, val]) => [key, `img_${val}`])),
27
+ variants,
28
+ redirection
29
+ }, [insensitive, , obj] = doubleUnderscore;
30
+ fromEntries(ext, mwConfig.tags);
31
+ fromEntries(
32
+ (obj && insensitive.length === 0 ? Object.keys(obj) : insensitive).map((s) => `__${s}__`),
33
+ mwConfig.doubleUnderscore[0]
34
+ );
35
+ fromEntries(doubleUnderscore[1].map((s) => `__${s}__`), mwConfig.doubleUnderscore[1]);
36
+ fromEntries(parserFunction.slice(2).flat(), mwConfig.functionSynonyms[0], true);
37
+ fromEntries(parserFunction[1], mwConfig.functionSynonyms[1]);
38
+ return mwConfig;
39
+ };
40
+
41
+ // mw/config.ts
42
+ var _a;
43
+ var ALL_SETTINGS_CACHE = (_a = getObject("InPageEditMwConfig")) != null ? _a : {};
44
+ var SITE_ID = typeof mw === "object" ? mw.config.get("wgServerName") + mw.config.get("wgScriptPath") : location.origin;
45
+ var SITE_SETTINGS = ALL_SETTINGS_CACHE[SITE_ID];
46
+ var VALID = Number(SITE_SETTINGS == null ? void 0 : SITE_SETTINGS.time) > Date.now() - 86400 * 1e3 * 30;
47
+ var getConfig = (magicWords, rule, flip) => Object.fromEntries(
48
+ magicWords.filter(rule).filter(({ "case-sensitive": i }) => i !== flip).flatMap(({ aliases, name, "case-sensitive": i }) => aliases.map((alias) => ({
49
+ alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ""),
50
+ name
51
+ }))).map(({ alias, name }) => [alias, name])
52
+ );
53
+ var getConfigPair = (magicWords, rule) => [true, false].map((bool) => getConfig(magicWords, rule, bool));
54
+ var setConfig = (config) => {
55
+ mw.config.set("extCodeMirrorConfig", config);
56
+ };
57
+ var getMwConfig = async (modes) => {
58
+ if (mw.loader.getState("ext.CodeMirror") !== null && !VALID) {
59
+ await mw.loader.using(mw.loader.getState("ext.CodeMirror.data") ? "ext.CodeMirror.data" : "ext.CodeMirror");
60
+ }
61
+ let config = mw.config.get("extCodeMirrorConfig");
62
+ if (!config && VALID) {
63
+ ({ config } = SITE_SETTINGS);
64
+ setConfig(config);
65
+ }
66
+ const isIPE = config && Object.values(config.functionSynonyms[0]).includes(true), nsid = mw.config.get("wgNamespaceIds");
67
+ if ((config == null ? void 0 : config.img) && config.redirection && config.variants && !isIPE) {
68
+ config.urlProtocols = config.urlProtocols.replace(/\\:/gu, ":");
69
+ config.tagModes = modes;
70
+ return { ...config, nsid };
71
+ } else if (location.hostname.endsWith(".moegirl.org.cn")) {
72
+ const parserConfig = await (await fetch(
73
+ `${CDN}/npm/wikiparser-node@browser/config/moegirl.json`
74
+ )).json();
75
+ setObject("wikilintConfig", parserConfig);
76
+ config = getStaticMwConfig(parserConfig, modes);
77
+ } else {
78
+ await mw.loader.using("mediawiki.api");
79
+ const { query: { general: { variants }, magicwords, extensiontags, functionhooks, variables } } = await new mw.Api().get({
80
+ meta: "siteinfo",
81
+ siprop: [
82
+ "general",
83
+ "magicwords",
84
+ ...config && !isIPE ? [] : ["extensiontags", "functionhooks", "variables"]
85
+ ],
86
+ formatversion: "2"
87
+ });
88
+ const others = /* @__PURE__ */ new Set(["msg", "raw", "msgnw", "subst", "safesubst"]);
89
+ if (config && !isIPE) {
90
+ const { functionSynonyms: [insensitive] } = config;
91
+ if (!("subst" in insensitive)) {
92
+ Object.assign(insensitive, getConfig(magicwords, ({ name }) => others.has(name)));
93
+ }
94
+ } else {
95
+ const functions = /* @__PURE__ */ new Set([
96
+ ...functionhooks,
97
+ ...variables,
98
+ ...others
99
+ ]);
100
+ config = {
101
+ tags: Object.fromEntries(extensiontags.map((tag) => [tag.slice(1, -1), true])),
102
+ functionSynonyms: getConfigPair(magicwords, ({ name }) => functions.has(name)),
103
+ doubleUnderscore: getConfigPair(
104
+ magicwords,
105
+ ({ aliases }) => aliases.some((alias) => /^__.+__$/u.test(alias))
106
+ )
107
+ };
108
+ }
109
+ config.tagModes = modes;
110
+ config.img = getConfig(magicwords, ({ name }) => name.startsWith("img_"));
111
+ config.variants = variants ? variants.map(({ code }) => code) : [];
112
+ config.redirection = magicwords.find(({ name }) => name === "redirect").aliases;
113
+ config.urlProtocols = mw.config.get("wgUrlProtocols").replace(/\\:/gu, ":");
114
+ }
115
+ setConfig(config);
116
+ ALL_SETTINGS_CACHE[SITE_ID] = { config, time: Date.now() };
117
+ setObject("InPageEditMwConfig", ALL_SETTINGS_CACHE);
118
+ return { ...config, nsid };
119
+ };
120
+ var getParserConfig = (minConfig, mwConfig) => {
121
+ var _a2;
122
+ let config = getObject("wikilintConfig");
123
+ if (config) {
124
+ return config;
125
+ }
126
+ config = {
127
+ ...minConfig,
128
+ ext: Object.keys(mwConfig.tags),
129
+ namespaces: mw.config.get("wgFormattedNamespaces"),
130
+ nsid: mwConfig.nsid,
131
+ doubleUnderscore: mwConfig.doubleUnderscore.map(
132
+ (obj) => Object.keys(obj).map((s) => s.slice(2, -2))
133
+ ),
134
+ variants: mwConfig.variants,
135
+ protocol: mwConfig.urlProtocols.replace(/\|\\?\/\\?\//u, ""),
136
+ redirection: (_a2 = mwConfig.redirection) != null ? _a2 : minConfig.redirection
137
+ };
138
+ if (location.hostname.endsWith(".moegirl.org.cn")) {
139
+ config.html[2].push("img");
140
+ }
141
+ [config.parserFunction[0]] = mwConfig.functionSynonyms;
142
+ if (mw.loader.getState("ext.CodeMirror") === null) {
143
+ for (const [key, val] of Object.entries(mwConfig.functionSynonyms[0])) {
144
+ if (!key.startsWith("#")) {
145
+ config.parserFunction[0][`#${key}`] = val;
146
+ }
147
+ }
148
+ }
149
+ config.parserFunction[1] = [
150
+ ...Object.keys(mwConfig.functionSynonyms[1]),
151
+ "="
152
+ ];
153
+ for (const [key, val] of Object.entries(mwConfig.img)) {
154
+ config.img[key] = val.slice(4);
155
+ }
156
+ return config;
157
+ };
158
+ export {
159
+ getMwConfig,
160
+ getParserConfig
161
+ };
@@ -1,2 +1,2 @@
1
- export declare const isMac: boolean;
1
+ export declare const isMac: boolean, modKey: string, key: string;
2
2
  export declare const openExtLinks: import("@codemirror/state").Extension[];
@@ -0,0 +1,4 @@
1
+ import type { Extension } from '@codemirror/state';
2
+ import type { CodeMirror6 } from './codemirror';
3
+ export declare const isMac: boolean;
4
+ export declare const openLinks: ({ langConfig }: CodeMirror6) => Extension;
package/dist/ref.d.ts CHANGED
@@ -1,11 +1,6 @@
1
1
  import { EditorView } from '@codemirror/view';
2
- import type { AST } from 'wikiparser-node/base';
3
2
  import type { editor } from 'monaco-editor';
4
3
  declare type Ranges = [number, number][];
5
- declare type Tree = Promise<AST> & {
6
- docChanged?: boolean;
7
- };
8
- export declare const trees: WeakMap<EditorView | editor.ITextModel, Tree>;
9
4
  /**
10
5
  * 异步查找注释的内容
11
6
  * @param view
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: ({ parserFunction, protocol, nsid, variants, redirection, ext, doubleUnderscore, img }: Config) => MwConfig;
27
+ export declare const getStaticMwConfig: ({ parserFunction, protocol, nsid, variants, redirection, ext, doubleUnderscore, img }: Config, modes: Record<string, string>) => MwConfig;
package/dist/token.d.ts CHANGED
@@ -5,7 +5,9 @@
5
5
  */
6
6
  import { Tag } from '@lezer/highlight';
7
7
  import { tokens } from './config';
8
- import type { StreamParser, StringStream } from '@codemirror/language';
8
+ import type { EditorState } from '@codemirror/state';
9
+ import type { StreamParser, StringStream as StringStreamBase } from '@codemirror/language';
10
+ import type { SyntaxNode } from '@lezer/common';
9
11
  declare type Style = string | [string];
10
12
  declare type Tokenizer<T = Style> = ((stream: StringStream, state: State) => T) & {
11
13
  args?: unknown[];
@@ -28,6 +30,7 @@ declare interface State extends Nesting {
28
30
  html: number;
29
31
  };
30
32
  redirect: boolean;
33
+ imgLink: boolean;
31
34
  data: MediaWikiData;
32
35
  }
33
36
  declare interface Token {
@@ -36,6 +39,10 @@ declare interface Token {
36
39
  style: Style;
37
40
  readonly state: State;
38
41
  }
42
+ declare interface StringStream extends StringStreamBase {
43
+ match(pattern: string, consume?: boolean, caseInsensitive?: boolean): true | null;
44
+ match(pattern: RegExp, consume?: boolean): RegExpMatchArray | null;
45
+ }
39
46
  export type TagName = keyof typeof tokens;
40
47
  export type ApiSuggestions = [string, string?][];
41
48
  /**
@@ -47,7 +54,7 @@ export type ApiSuggestions = [string, string?][];
47
54
  export type ApiSuggest = (search: string, namespace?: number, subpage?: boolean) => ApiSuggestions | Promise<ApiSuggestions>;
48
55
  export interface MwConfig {
49
56
  readonly tags: Record<string, true>;
50
- readonly tagModes: Record<string, string>;
57
+ tagModes: Record<string, string>;
51
58
  urlProtocols: string;
52
59
  functionSynonyms: [Record<string, string>, Record<string, unknown>];
53
60
  doubleUnderscore: [Record<string, unknown>, Record<string, unknown>];
@@ -59,6 +66,8 @@ export interface MwConfig {
59
66
  implicitlyClosedHtmlTags?: string[];
60
67
  linkSuggest?: ApiSuggest;
61
68
  paramSuggest?: ApiSuggest;
69
+ titleParser?: (state: EditorState, node: SyntaxNode, urlProtocols: string) => true | undefined;
70
+ isbnParser?: (link: string) => true;
62
71
  }
63
72
  declare class MediaWikiData {
64
73
  /** 已解析的节点 */
@@ -92,8 +101,8 @@ export declare class MediaWiki {
92
101
  readonly templateRegex: RegExp;
93
102
  readonly argumentRegex: RegExp;
94
103
  readonly styleRegex: RegExp;
95
- readonly convertSemicolon: RegExp;
96
- readonly convertLang: RegExp;
104
+ readonly convertSemicolon: RegExp | undefined;
105
+ readonly convertLang: RegExp | undefined;
97
106
  readonly convertRegex: RegExp;
98
107
  readonly wikiRegex: RegExp;
99
108
  readonly tableDefinitionRegex: RegExp;
@@ -114,10 +123,11 @@ export declare class MediaWiki {
114
123
  * See makeLocalStyle() for how these tokens are used.
115
124
  */
116
125
  registerGroundTokens(): void;
126
+ inChars({ length }: string, tag: TagName): Tokenizer;
117
127
  inStr(str: string, tag: TagName | false, errorTag?: TagName): Tokenizer;
118
128
  eatWikiText(style: string): Tokenizer;
119
129
  eatApostrophes(obj: Pick<State, 'bold' | 'italic'>): Tokenizer<string | false>;
120
- eatExternalLinkProtocol(chars: string, free?: boolean): Tokenizer;
130
+ eatExternalLinkProtocol({ length }: string, free?: boolean): Tokenizer;
121
131
  inExternalLink(text?: boolean): Tokenizer;
122
132
  get eatFreeExternalLink(): Tokenizer;
123
133
  inLink(file: boolean, section?: boolean): Tokenizer;
@@ -137,7 +147,7 @@ export declare class MediaWiki {
137
147
  eatExtTagArea(name: string): Tokenizer;
138
148
  inExtTokens(origString: string): Tokenizer;
139
149
  inVariable(pos?: number): Tokenizer;
140
- eatTransclusion(stream: StringStream, state: State): string;
150
+ eatTransclusion(stream: StringStream, state: State, { length }: string): string | undefined;
141
151
  inParserFunctionName(invoke?: number, n?: number): Tokenizer;
142
152
  inTemplatePageName(haveEaten?: boolean, anchor?: boolean): Tokenizer;
143
153
  inParserFunctionArgument(module?: number, n?: number): Tokenizer;
@@ -152,6 +162,7 @@ export declare class MediaWiki {
152
162
  * @param tags
153
163
  */
154
164
  mediawiki(tags?: string[]): StreamParser<State>;
165
+ 'text/mediawiki'(): StreamParser<State>;
155
166
  'text/nowiki'(): StreamParser<Record<string, never>>;
156
167
  inPre(begin?: boolean): Tokenizer<string>;
157
168
  'text/pre'(): StreamParser<State>;