1
1
import * as regex from './regex.js' ;
2
2
import { inherit } from './utils.js' ;
3
3
import * as EXT from "./compiler_extensions.js" ;
4
-
5
- // keywords that should have no default relevance value
6
- const COMMON_KEYWORDS = 'of and for in not or if then' . split ( ' ' ) ;
4
+ import { compileKeywords } from "./compile_keywords.js" ;
7
5
8
6
// compilation
9
7
@@ -13,7 +11,7 @@ const COMMON_KEYWORDS = 'of and for in not or if then'.split(' ');
13
11
* Given the raw result of a language definition (Language), compiles this so
14
12
* that it is ready for highlighting code.
15
13
* @param {Language } language
16
- * @param {{plugins: [ HLJSPlugin] } } opts
14
+ * @param {{plugins: HLJSPlugin[] ] } } opts
17
15
* @returns {CompiledLanguage }
18
16
*/
19
17
export function compileLanguage ( language , { plugins} ) {
@@ -423,73 +421,3 @@ function expandOrCloneMode(mode) {
423
421
// no special dependency issues, just return ourselves
424
422
return mode ;
425
423
}
426
-
427
- /***********************************************
428
- Keywords
429
- ***********************************************/
430
-
431
- /**
432
- * Given raw keywords from a language definition, compile them.
433
- *
434
- * @param {string | Record<string,string> } rawKeywords
435
- * @param {boolean } caseInsensitive
436
- */
437
- function compileKeywords ( rawKeywords , caseInsensitive ) {
438
- /** @type KeywordDict */
439
- const compiledKeywords = { } ;
440
-
441
- if ( typeof rawKeywords === 'string' ) { // string
442
- splitAndCompile ( 'keyword' , rawKeywords ) ;
443
- } else {
444
- Object . keys ( rawKeywords ) . forEach ( function ( className ) {
445
- splitAndCompile ( className , rawKeywords [ className ] ) ;
446
- } ) ;
447
- }
448
- return compiledKeywords ;
449
-
450
- // ---
451
-
452
- /**
453
- * Compiles an individual list of keywords
454
- *
455
- * Ex: "for if when while|5"
456
- *
457
- * @param {string } className
458
- * @param {string } keywordList
459
- */
460
- function splitAndCompile ( className , keywordList ) {
461
- if ( caseInsensitive ) {
462
- keywordList = keywordList . toLowerCase ( ) ;
463
- }
464
- keywordList . split ( ' ' ) . forEach ( function ( keyword ) {
465
- const pair = keyword . split ( '|' ) ;
466
- compiledKeywords [ pair [ 0 ] ] = [ className , scoreForKeyword ( pair [ 0 ] , pair [ 1 ] ) ] ;
467
- } ) ;
468
- }
469
- }
470
-
471
- /**
472
- * Returns the proper score for a given keyword
473
- *
474
- * Also takes into account comment keywords, which will be scored 0 UNLESS
475
- * another score has been manually assigned.
476
- * @param {string } keyword
477
- * @param {string } [providedScore]
478
- */
479
- function scoreForKeyword ( keyword , providedScore ) {
480
- // manual scores always win over common keywords
481
- // so you can force a score of 1 if you really insist
482
- if ( providedScore ) {
483
- return Number ( providedScore ) ;
484
- }
485
-
486
- return commonKeyword ( keyword ) ? 0 : 1 ;
487
- }
488
-
489
- /**
490
- * Determines if a given keyword is common or not
491
- *
492
- * @param {string } keyword */
493
- function commonKeyword ( keyword ) {
494
- return COMMON_KEYWORDS . includes ( keyword . toLowerCase ( ) ) ;
495
- }
0 commit comments