Debian Codegraph @

resources/codemirror.mediawiki.config.js

raw
1 const {
2 StreamParser,
3 Tag,
4 TagStyle,
5 tags
6 } = require( 'ext.CodeMirror.v6.lib' );
7
8 /**
9 * Configuration for the MediaWiki highlighting mode for CodeMirror.
10 * This is a separate class mainly to keep static configuration out of
11 * the logic in {@link CodeMirrorModeMediaWiki}.
12 *
13 * @module CodeMirrorModeMediaWikiConfig
14 *
15 * @example
16 * // within MediaWiki:
17 * const { mwModeConfig } = require( 'ext.CodeMirror.v6.mode.mediawiki' );
18 * // Reference tags by their constants in the tags property.
19 * if ( tag === mwModeConfig.tags.htmlTagBracket ) {
20 * // …
21 * }
22 */
23 class CodeMirrorModeMediaWikiConfig {
24
25 /**
26 * @internal
27 */
28 constructor() {
29 this.extHighlightStyles = [];
30 this.tokenTable = this.defaultTokenTable;
31 }
32
33 /**
34 * Register a token for the given tag in CodeMirror. The generated CSS class will be of
35 * the form 'cm-mw-ext-tagname'. This is for internal use to dynamically register tags
36 * from other MediaWiki extensions.
37 *
38 * @see https://www.mediawiki.org/wiki/Extension:CodeMirror#Extension_integration
39 * @param {string} tag
40 * @param {Tag} [parent]
41 * @private
42 * @internal
43 */
44 addTag( tag, parent = null ) {
45 if ( this.tokenTable[ `mw-tag-${ tag }` ] ) {
46 return;
47 }
48 this.addToken( `mw-tag-${ tag }`, parent );
49 this.addToken( `mw-ext-${ tag }`, parent );
50 }
51
52 /**
53 * Dynamically register a token in CodeMirror.
54 * This is solely for use by this.addTag() and CodeMirrorModeMediaWiki.makeLocalStyle().
55 *
56 * @param {string} token
57 * @param {Tag} [parent]
58 * @private
59 * @internal
60 */
61 addToken( token, parent = null ) {
62 if ( this.tokenTable[ token ] ) {
63 return;
64 }
65 this.tokenTable[ token ] = Tag.define( parent );
66 this.extHighlightStyles.push( {
67 tag: this.tokenTable[ token ],
68 class: `cm-${ token }`
69 } );
70 }
71
72 /**
73 * All HTML/XML tags permitted in MediaWiki Core.
74 *
75 * Extensions should use the CodeMirrorTagModes extension attribute to register tags
76 * instead of adding them here.
77 *
78 * @see https://www.mediawiki.org/wiki/Extension:CodeMirror#Extension_integration
79 * @return {Object}
80 */
81 get permittedHtmlTags() {
82 return {
83 b: true, bdi: true, del: true, i: true, ins: true,
84 u: true, font: true, big: true, small: true, sub: true, sup: true,
85 h1: true, h2: true, h3: true, h4: true, h5: true, h6: true, cite: true,
86 code: true, em: true, s: true, strike: true, strong: true, tt: true,
87 var: true, div: true, center: true, blockquote: true, q: true, ol: true, ul: true,
88 dl: true, table: true, caption: true, pre: true, ruby: true, rb: true,
89 rp: true, rt: true, rtc: true, p: true, span: true, abbr: true, dfn: true,
90 kbd: true, samp: true, data: true, time: true, mark: true, br: true,
91 wbr: true, hr: true, li: true, dt: true, dd: true, td: true, th: true,
92 tr: true, noinclude: true, includeonly: true, onlyinclude: true
93 };
94 }
95
96 /**
97 * HTML tags that are only self-closing.
98 *
99 * @return {Object}
100 */
101 get implicitlyClosedHtmlTags() {
102 return {
103 br: true, hr: true, wbr: true
104 };
105 }
106
107 /**
108 * Mapping of MediaWiki-esque token identifiers to
109 * [standardized lezer highlighting tags]{@link https://lezer.codemirror.net/docs/ref/#highlight.tags}.
110 * Values are one of the default highlighting tags. The idea is to use as many default tags as
111 * possible so that theming (such as dark mode) can be applied with minimal effort. The
112 * semantic meaning of the tag may not really match how it is used, but as per CodeMirror docs,
113 * this is fine. It's still better to make use of the standard tags in some way.
114 *
115 * Once we allow use of other themes, we may want to tweak these values for aesthetic reasons.
116 * The values here can freely be changed. The actual CSS class used is defined further down
117 * in highlightStyle().
118 *
119 * @see https://lezer.codemirror.net/docs/ref/#highlight.tags
120 * @member CodeMirrorModeMediaWikiConfig
121 * @type {Object<string>}
122 * @return {Object<string>}
123 */
124 get tags() {
125 return Object.assign( {
126 apostrophes: 'character',
127 apostrophesBold: 'strong',
128 apostrophesItalic: 'emphasis',
129 comment: 'comment',
130 doubleUnderscore: 'controlKeyword',
131 extLink: 'url',
132 extLinkBracket: 'modifier',
133 extLinkProtocol: 'namespace',
134 extLinkText: 'labelName',
135 hr: 'contentSeparator',
136 htmlTagAttribute: 'attributeName',
137 htmlTagBracket: 'angleBracket',
138 htmlTagName: 'tagName',
139 indenting: 'operatorKeyword',
140 linkBracket: 'squareBracket',
141 linkDelimiter: 'operator',
142 linkText: 'string',
143 linkToSection: 'className',
144 list: 'list',
145 parserFunction: 'unit',
146 parserFunctionBracket: 'paren',
147 parserFunctionDelimiter: 'punctuation',
148 parserFunctionName: 'keyword',
149 sectionHeader: 'heading',
150 sectionHeader1: 'heading1',
151 sectionHeader2: 'heading2',
152 sectionHeader3: 'heading3',
153 sectionHeader4: 'heading4',
154 sectionHeader5: 'heading5',
155 sectionHeader6: 'heading6',
156 signature: 'quote',
157 tableBracket: 'null',
158 tableDefinition: 'definitionOperator',
159 tableDelimiter: 'typeOperator',
160 template: 'attributeValue',
161 templateArgumentName: 'definitionKeyword',
162 templateBracket: 'bracket',
163 templateDelimiter: 'separator',
164 templateName: 'moduleKeyword',
165 templateVariable: 'atom',
166 templateVariableBracket: 'brace',
167 templateVariableName: 'variableName'
168 }, this.customTags );
169 }
170
171 /**
172 * Custom tags. These are not mapped to any standard highlighting tag.
173 * These are here so that they, like tags(), serve as constants that we can
174 * reference in CodeMirrorModeMediaWiki.
175 *
176 * IMPORTANT: There should be a row in defaultTokenTable() for each of these.
177 *
178 * @return {Object<string>}
179 * @private
180 */
181 get customTags() {
182 return {
183 em: 'mw-em',
184 error: 'mw-error',
185 extNowiki: 'mw-ext-nowiki',
186 extPre: 'mw-ext-pre',
187 extTag: 'mw-exttag',
188 extTagAttribute: 'mw-exttag-attribute',
189 extTagBracket: 'mw-exttag-bracket',
190 extTagName: 'mw-exttag-name',
191 freeExtLink: 'mw-free-extlink',
192 freeExtLinkProtocol: 'mw-free-extlink-protocol',
193 htmlEntity: 'mw-html-entity',
194 link: 'mw-link',
195 linkPageName: 'mw-link-pagename',
196 nowiki: 'mw-tag-nowiki',
197 pageName: 'mw-pagename',
198 pre: 'mw-tag-pre',
199 section: 'mw-section',
200 skipFormatting: 'mw-skipformatting',
201 strong: 'mw-strong',
202 tableCaption: 'mw-table-caption',
203 templateVariableDelimiter: 'mw-templatevariable-delimiter'
204 };
205 }
206
207 /**
208 * These are custom tokens (a.k.a. tags) that aren't mapped to any of the standardized tags.
209 * Make sure these are also defined in customTags() above.
210 *
211 * TODO: pass parent Tags in Tag.define() where appropriate for better theming.
212 *
213 * @see https://codemirror.net/docs/ref/#language.StreamParser.tokenTable
214 * @see https://lezer.codemirror.net/docs/ref/#highlight.Tag%5Edefine
215 * @return {Object<Tag>}
216 * @internal
217 */
218 get defaultTokenTable() {
219 return {
220 [ this.tags.em ]: Tag.define(),
221 [ this.tags.error ]: Tag.define(),
222 [ this.tags.extNowiki ]: Tag.define(),
223 [ this.tags.extPre ]: Tag.define(),
224 [ this.tags.extTag ]: Tag.define(),
225 [ this.tags.extTagAttribute ]: Tag.define(),
226 [ this.tags.extTagBracket ]: Tag.define(),
227 [ this.tags.extTagName ]: Tag.define(),
228 [ this.tags.freeExtLink ]: Tag.define(),
229 [ this.tags.freeExtLinkProtocol ]: Tag.define(),
230 [ this.tags.htmlEntity ]: Tag.define(),
231 [ this.tags.link ]: Tag.define(),
232 [ this.tags.linkPageName ]: Tag.define(),
233 [ this.tags.nowiki ]: Tag.define(),
234 [ this.tags.pageName ]: Tag.define(),
235 [ this.tags.pre ]: Tag.define(),
236 [ this.tags.section ]: Tag.define(),
237 [ this.tags.skipFormatting ]: Tag.define(),
238 [ this.tags.strong ]: Tag.define(),
239 [ this.tags.tableCaption ]: Tag.define(),
240 [ this.tags.templateVariableDelimiter ]: Tag.define()
241 };
242 }
243
244 /**
245 * This defines the actual CSS class assigned to each tag/token.
246 * Keep this in sync and in the same order as tags().
247 *
248 * @see https://codemirror.net/docs/ref/#language.TagStyle
249 * @param {StreamParser} context
250 * @return {TagStyle[]}
251 * @internal
252 */
253 getTagStyles( context ) {
254 return [
255 {
256 tag: tags[ this.tags.apostrophes ],
257 class: 'cm-mw-apostrophes'
258 },
259 {
260 tag: tags[ this.tags.apostrophesBold ],
261 class: 'cm-mw-apostrophes-bold'
262 },
263 {
264 tag: tags[ this.tags.apostrophesItalic ],
265 class: 'cm-mw-apostrophes-italic'
266 },
267 {
268 tag: tags[ this.tags.comment ],
269 class: 'cm-mw-comment'
270 },
271 {
272 tag: tags[ this.tags.doubleUnderscore ],
273 class: 'cm-mw-double-underscore'
274 },
275 {
276 tag: tags[ this.tags.extLink ],
277 class: 'cm-mw-extlink'
278 },
279 {
280 tag: tags[ this.tags.extLinkBracket ],
281 class: 'cm-mw-extlink-bracket'
282 },
283 {
284 tag: tags[ this.tags.extLinkProtocol ],
285 class: 'cm-mw-extlink-protocol'
286 },
287 {
288 tag: tags[ this.tags.extLinkText ],
289 class: 'cm-mw-extlink-text'
290 },
291 {
292 tag: tags[ this.tags.hr ],
293 class: 'cm-mw-hr'
294 },
295 {
296 tag: tags[ this.tags.htmlTagAttribute ],
297 class: 'cm-mw-htmltag-attribute'
298 },
299 {
300 tag: tags[ this.tags.htmlTagBracket ],
301 class: 'cm-mw-htmltag-bracket'
302 },
303 {
304 tag: tags[ this.tags.htmlTagName ],
305 class: 'cm-mw-htmltag-name'
306 },
307 {
308 tag: tags[ this.tags.indenting ],
309 class: 'cm-mw-indenting'
310 },
311 {
312 tag: tags[ this.tags.linkBracket ],
313 class: 'cm-mw-link-bracket'
314 },
315 {
316 tag: tags[ this.tags.linkDelimiter ],
317 class: 'cm-mw-link-delimiter'
318 },
319 {
320 tag: tags[ this.tags.linkText ],
321 class: 'cm-mw-link-text'
322 },
323 {
324 tag: tags[ this.tags.linkToSection ],
325 class: 'cm-mw-link-tosection'
326 },
327 {
328 tag: tags[ this.tags.list ],
329 class: 'cm-mw-list'
330 },
331 {
332 tag: tags[ this.tags.parserFunction ],
333 class: 'cm-mw-parserfunction'
334 },
335 {
336 tag: tags[ this.tags.parserFunctionBracket ],
337 class: 'cm-mw-parserfunction-bracket'
338 },
339 {
340 tag: tags[ this.tags.parserFunctionDelimiter ],
341 class: 'cm-mw-parserfunction-delimiter'
342 },
343 {
344 tag: tags[ this.tags.parserFunctionName ],
345 class: 'cm-mw-parserfunction-name'
346 },
347 {
348 tag: tags[ this.tags.sectionHeader ],
349 class: 'cm-mw-section-header'
350 },
351 {
352 tag: tags[ this.tags.sectionHeader1 ],
353 class: 'cm-mw-section-1'
354 },
355 {
356 tag: tags[ this.tags.sectionHeader2 ],
357 class: 'cm-mw-section-2'
358 },
359 {
360 tag: tags[ this.tags.sectionHeader3 ],
361 class: 'cm-mw-section-3'
362 },
363 {
364 tag: tags[ this.tags.sectionHeader4 ],
365 class: 'cm-mw-section-4'
366 },
367 {
368 tag: tags[ this.tags.sectionHeader5 ],
369 class: 'cm-mw-section-5'
370 },
371 {
372 tag: tags[ this.tags.sectionHeader6 ],
373 class: 'cm-mw-section-6'
374 },
375 {
376 tag: tags[ this.tags.signature ],
377 class: 'cm-mw-signature'
378 },
379 {
380 tag: tags[ this.tags.tableBracket ],
381 class: 'cm-mw-table-bracket'
382 },
383 {
384 tag: tags[ this.tags.tableDefinition ],
385 class: 'cm-mw-table-definition'
386 },
387 {
388 tag: tags[ this.tags.tableDelimiter ],
389 class: 'cm-mw-table-delimiter'
390 },
391 {
392 tag: tags[ this.tags.template ],
393 class: 'cm-mw-template'
394 },
395 {
396 tag: tags[ this.tags.templateArgumentName ],
397 class: 'cm-mw-template-argument-name'
398 },
399 {
400 tag: tags[ this.tags.templateBracket ],
401 class: 'cm-mw-template-bracket'
402 },
403 {
404 tag: tags[ this.tags.templateDelimiter ],
405 class: 'cm-mw-template-delimiter'
406 },
407 {
408 tag: tags[ this.tags.templateName ],
409 class: 'cm-mw-pagename cm-mw-template-name'
410 },
411 {
412 tag: tags[ this.tags.templateVariable ],
413 class: 'cm-mw-templatevariable'
414 },
415 {
416 tag: tags[ this.tags.templateVariableBracket ],
417 class: 'cm-mw-templatevariable-bracket'
418 },
419 {
420 tag: tags[ this.tags.templateVariableName ],
421 class: 'cm-mw-templatevariable-name'
422 },
423
424 /**
425 * Custom tags.
426 * IMPORTANT: These need to reference the CodeMirrorModeMediaWiki context.
427 */
428 {
429 tag: context.tokenTable[ this.tags.em ],
430 class: 'cm-mw-em'
431 },
432 {
433 tag: context.tokenTable[ this.tags.error ],
434 class: 'cm-mw-error'
435 },
436 {
437 tag: context.tokenTable[ this.tags.extNowiki ],
438 class: 'cm-mw-ext-nowiki'
439 },
440 {
441 tag: context.tokenTable[ this.tags.extPre ],
442 class: 'cm-mw-ext-pre'
443 },
444 {
445 tag: context.tokenTable[ this.tags.extTagBracket ],
446 class: 'cm-mw-exttag-bracket'
447 },
448 {
449 tag: context.tokenTable[ this.tags.extTag ],
450 class: 'cm-mw-exttag'
451 },
452 {
453 tag: context.tokenTable[ this.tags.extTagAttribute ],
454 class: 'cm-mw-exttag-attribute'
455 },
456 {
457 tag: context.tokenTable[ this.tags.extTagName ],
458 class: 'cm-mw-exttag-name'
459 },
460 {
461 tag: context.tokenTable[ this.tags.freeExtLink ],
462 class: 'cm-mw-free-extlink'
463 },
464 {
465 tag: context.tokenTable[ this.tags.freeExtLinkProtocol ],
466 class: 'cm-mw-free-extlink-protocol'
467 },
468 {
469 tag: context.tokenTable[ this.tags.htmlEntity ],
470 class: 'cm-mw-html-entity'
471 },
472 {
473 tag: context.tokenTable[ this.tags.link ],
474 class: 'cm-mw-link'
475 },
476 {
477 tag: context.tokenTable[ this.tags.linkPageName ],
478 class: 'cm-mw-link-pagename'
479 },
480 {
481 tag: context.tokenTable[ this.tags.nowiki ],
482 class: 'cm-mw-tag-nowiki'
483 },
484 {
485 tag: context.tokenTable[ this.tags.pageName ],
486 class: 'cm-mw-pagename'
487 },
488 {
489 tag: context.tokenTable[ this.tags.pre ],
490 class: 'cm-mw-tag-pre'
491 },
492 {
493 tag: context.tokenTable[ this.tags.section ],
494 class: 'cm-mw-section'
495 },
496 {
497 tag: context.tokenTable[ this.tags.skipFormatting ],
498 class: 'cm-mw-skipformatting'
499 },
500 {
501 tag: context.tokenTable[ this.tags.strong ],
502 class: 'cm-mw-strong'
503 },
504 {
505 tag: context.tokenTable[ this.tags.tableCaption ],
506 class: 'cm-mw-table-caption'
507 },
508 {
509 tag: context.tokenTable[ this.tags.templateVariableDelimiter ],
510 class: 'cm-mw-templatevariable-delimiter'
511 },
512
513 ...this.extHighlightStyles
514 ];
515 }
516 }
517
518 /**
519 * @member CodeMirrorModeMediaWikiConfig
520 * @type {CodeMirrorModeMediaWikiConfig}
521 */
522 const mwModeConfig = new CodeMirrorModeMediaWikiConfig();
523
524 module.exports = mwModeConfig;