@bhsd/codemirror-mediawiki 3.6.1 → 3.6.3

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.
@@ -10,14 +10,12 @@ export const findEnclosingBrackets = (node, pos, brackets) => {
10
10
  return { start: firstChild, end: lastChild, matched: true };
11
11
  }
12
12
  }
13
- ({ parent } = parent); // eslint-disable-line no-param-reassign
13
+ ({ parent } = parent);
14
14
  }
15
15
  return undefined;
16
16
  };
17
17
  export const findEnclosingPlainBrackets = (state, pos, config) => {
18
- const { brackets, maxScanDistance } = config, re = new RegExp(`[${
19
- // eslint-disable-next-line @typescript-eslint/no-misused-spread
20
- [...brackets].filter((_, i) => i % 2).map(c => c === ']' ? String.raw `\]` : c).join('')}]`, 'gu'), str = state.sliceDoc(pos, pos + maxScanDistance);
18
+ const { brackets, maxScanDistance } = config, re = new RegExp(`[${[...brackets].filter((_, i) => i % 2).map(c => c === ']' ? String.raw `\]` : c).join('')}]`, 'gu'), str = state.sliceDoc(pos, pos + maxScanDistance);
21
19
  let mt = re.exec(str);
22
20
  while (mt) {
23
21
  const result = matchBrackets(state, pos + mt.index + 1, -1, config), left = result?.end?.to;
@@ -8,6 +8,11 @@ import { MediaWiki } from './token';
8
8
  import type { StreamParser, TagStyle } from '@codemirror/language';
9
9
  import type { CompletionSource, Completion } from '@codemirror/autocomplete';
10
10
  import type { MwConfig } from './token';
11
+ /**
12
+ * 是否是普通维基链接
13
+ * @param name 节点名称
14
+ */
15
+ export declare const isWikiLink: (name: string) => boolean;
11
16
  export declare class FullMediaWiki extends MediaWiki {
12
17
  #private;
13
18
  readonly nsRegex: RegExp;
package/dist/mediawiki.js CHANGED
@@ -12,6 +12,11 @@ import { isolateSelector, ltrSelector, isWMF } from './constants';
12
12
  import { MediaWiki } from './token';
13
13
  import { braceStackUpdate, hasTag } from './util';
14
14
  import { EditorView } from '@codemirror/view';
15
+ /**
16
+ * 是否是普通维基链接
17
+ * @param name 节点名称
18
+ */
19
+ export const isWikiLink = (name) => /mw-[\w-]*link-ground/u.test(name);
15
20
  /**
16
21
  * 检查首字母大小写并插入正确的自动填充内容
17
22
  * @param view
@@ -98,13 +103,12 @@ export class FullMediaWiki extends MediaWiki {
98
103
  * @param str 搜索字符串,开头不包含` `,但可能包含`_`
99
104
  * @param ns 命名空间
100
105
  */
101
- async #linkSuggest(str, ns = 0) {
106
+ async #linkSuggest(str, ns) {
102
107
  const { config: { linkSuggest, nsid }, nsRegex } = this;
103
108
  if (typeof linkSuggest !== 'function' || /[|{}<>[\]#]/u.test(str)) {
104
109
  return undefined;
105
110
  }
106
111
  let subpage = false, search = str, offset = 0;
107
- /* eslint-disable no-param-reassign */
108
112
  if (search.startsWith('/')) {
109
113
  ns = 0;
110
114
  subpage = true;
@@ -131,7 +135,6 @@ export class FullMediaWiki extends MediaWiki {
131
135
  search = `${ns === -2 ? 'File' : prefix}:${search.slice(length)}`;
132
136
  }
133
137
  }
134
- /* eslint-enable no-param-reassign */
135
138
  const underscore = str.slice(offset).includes('_');
136
139
  return {
137
140
  offset,
@@ -159,8 +162,8 @@ export class FullMediaWiki extends MediaWiki {
159
162
  /** 自动补全魔术字和标签名 */
160
163
  get completionSource() {
161
164
  return async (context) => {
162
- const { state, pos, explicit } = context, node = syntaxTree(state).resolve(pos, -1), types = new Set(node.name.split('_')), isParserFunction = hasTag(types, 'parserFunctionName'),
163
- /** 开头不包含` `,但可能包含`_` */ search = state.sliceDoc(node.from, pos).trimStart(), start = pos - search.length;
165
+ const { state, pos, explicit } = context, node = syntaxTree(state).resolve(pos, -1), { name: n, from: f, to: t } = node, types = new Set(n.split('_')), isParserFunction = hasTag(types, 'parserFunctionName'),
166
+ /** 开头不包含` `,但可能包含`_` */ search = state.sliceDoc(f, pos).trimStart(), start = pos - search.length;
164
167
  let { prevSibling } = node;
165
168
  if (explicit || isParserFunction && search.includes('#') || isWMF) {
166
169
  const validFor = isWMF ? null : { validFor: /^[^|{}<>[\]#]*$/u };
@@ -184,16 +187,20 @@ export class FullMediaWiki extends MediaWiki {
184
187
  }
185
188
  const isPage = hasTag(types, 'pageName') && hasTag(types, 'parserFunction') || 0;
186
189
  if (isPage && search.trim() || hasTag(types, 'linkPageName')) {
187
- let prefix = '';
190
+ const isLink = isWikiLink(n);
191
+ let prefix = '', ns = 0;
188
192
  if (isPage) {
189
- prefix = this.autocompleteNamespaces[[...types].find(t => t.startsWith('mw-function-'))
193
+ prefix = this.autocompleteNamespaces[[...types].find(type => type.startsWith('mw-function-'))
190
194
  .slice(12)];
191
195
  }
192
- const suggestions = await this.#linkSuggest(prefix + search);
196
+ else if (hasTag(types, 'mw-tag-gallery') && !isLink) {
197
+ ns = 6;
198
+ }
199
+ const suggestions = await this.#linkSuggest(prefix + search, ns);
193
200
  if (!suggestions) {
194
201
  return null;
195
202
  }
196
- else if (!isPage) {
203
+ else if (!isPage && isLink) {
197
204
  suggestions.options = suggestions.options.map((option) => ({ ...option, apply }));
198
205
  }
199
206
  else if (prefix === 'Module:') {
@@ -235,7 +242,7 @@ export class FullMediaWiki extends MediaWiki {
235
242
  ({ prevSibling } = prevSibling);
236
243
  }
237
244
  if (prevSibling && page) {
238
- const equal = isArgument && state.sliceDoc(pos, node.to).trim() === '=' ? '' : '=', suggestions = await this.#paramSuggest(isDelimiter ? '' : search, page, equal);
245
+ const equal = isArgument && state.sliceDoc(pos, t).trim() === '=' ? '' : '=', suggestions = await this.#paramSuggest(isDelimiter ? '' : search, page, equal);
239
246
  if (suggestions && suggestions.options.length > 0) {
240
247
  return {
241
248
  from: isDelimiter ? pos : start + suggestions.offset,
@@ -249,7 +256,7 @@ export class FullMediaWiki extends MediaWiki {
249
256
  const isTagName = hasTag(types, ['htmlTagName', 'extTagName']), explicitMatch = explicit && context.matchBefore(/\s$/u), validForAttr = /^[a-z]*$/iu;
250
257
  if (isTagName && explicitMatch
251
258
  || hasTag(types, ['htmlTagAttribute', 'extTagAttribute', 'tableDefinition'])) {
252
- const tagName = isTagName ? search.trim() : /mw-(?:ext|html)-([a-z]+)/u.exec(node.name)[1], mt = explicitMatch || context.matchBefore(hasTag(types, 'tableDefinition') ? /[\s|-][a-z]+$/iu : /\s[a-z]+$/iu);
259
+ const tagName = isTagName ? search.trim() : /mw-(?:ext|html)-([a-z]+)/u.exec(n)[1], mt = explicitMatch || context.matchBefore(hasTag(types, 'tableDefinition') ? /[\s|-][a-z]+$/iu : /\s[a-z]+$/iu);
253
260
  return mt && (mt.from < start || /^\s/u.test(mt.text))
254
261
  ? {
255
262
  from: mt.from + 1,
@@ -266,7 +273,7 @@ export class FullMediaWiki extends MediaWiki {
266
273
  : null;
267
274
  }
268
275
  else if (explicit && hasTag(types, ['tableTd', 'tableTh', 'tableCaption'])) {
269
- const [, tagName] = /mw-table-([a-z]+)/u.exec(node.name), mt = context.matchBefore(/[\s|!+][a-z]*$/iu);
276
+ const [, tagName] = /mw-table-([a-z]+)/u.exec(n), mt = context.matchBefore(/[\s|!+][a-z]*$/iu);
270
277
  if (mt && (mt.from < start || /^\s/u.test(mt.text))) {
271
278
  return {
272
279
  from: mt.from + 1,
@@ -297,7 +304,7 @@ export class FullMediaWiki extends MediaWiki {
297
304
  };
298
305
  }
299
306
  mt = context.matchBefore(/<\/?[a-z\d]*$/iu);
300
- const extTags = [...types].filter(t => t.startsWith('mw-tag-'))
307
+ const extTags = [...types].filter(type => type.startsWith('mw-tag-'))
301
308
  .map(s => s.slice(7));
302
309
  if (mt && (explicit || mt.to - mt.from > 1)) {
303
310
  const validFor = /^[a-z\d]*$/iu;
@@ -493,6 +500,9 @@ const theme = /* @__PURE__ */ EditorView.theme({
493
500
  [getSelector(['pre', 'nowiki'], 'tag-')]: {
494
501
  backgroundColor: 'rgb(0,0,0,.04)',
495
502
  },
503
+ '.cm-mw-tag-ref': {
504
+ backgroundColor: 'var(--cm-ref)',
505
+ },
496
506
  [`${isolateSelector}, &[dir="rtl"] .cm-mw-template-name`]: {
497
507
  unicodeBidi: 'isolate',
498
508
  },