1
1
let allFunctions = new Set ( ) ;
2
2
3
- function extractFunctions ( tmLanguage ) {
4
- const wantedScopes = new Set ( [
5
- "support.function.mta-shared" ,
6
- "support.function.mta-server" ,
7
- "support.function.mta-client" ,
8
- "keyword.mta"
9
- ] ) ;
10
-
11
- return tmLanguage . patterns ?. reduce ( ( funcs , { match, name } ) => {
12
- if ( match && wantedScopes . has ( name ) ) {
13
- funcs . push ( ...match . match ( / \\ b \( ( [ ^ ) ] + ) \) \\ b / ) ?. [ 1 ] ?. split ( "|" ) || [ ] ) ;
3
+ function extractFunctions ( tmLanguage , textContent ) {
4
+ const result = new Set ( ) ;
5
+
6
+ tmLanguage . patterns ?. forEach ( ( { match, name } ) => {
7
+ if ( ! match ) return ;
8
+
9
+ if ( name === "keyword.mta" ) {
10
+ const regex = new RegExp ( match . replace ( / \\ \\ / g, "\\" ) , "g" ) ;
11
+ let m ;
12
+ while ( ( m = regex . exec ( textContent ) ) !== null ) {
13
+ if ( m [ 0 ] ) result . add ( m [ 0 ] ) ;
14
+ }
15
+ } else {
16
+ const matches = match . match ( / \\ b \( ( [ ^ ) ] + ) \) \\ b / ) ?. [ 1 ] ?. split ( "|" ) || [ ] ;
17
+ matches . forEach ( w => result . add ( w ) ) ;
14
18
}
15
- return funcs ;
16
- } , [ ] ) || [ ] ;
19
+ } ) ;
20
+
21
+ return Array . from ( result ) ;
17
22
}
18
23
24
+
19
25
function initKeywordLinker ( ) {
20
26
function onDomReady ( ) {
21
27
const spans = [
@@ -34,7 +40,12 @@ function initKeywordLinker() {
34
40
fetch ( '/grammars/lua-mta.tmLanguage.json' )
35
41
. then ( res => res . json ( ) )
36
42
. then ( json => {
37
- allFunctions = new Set ( extractFunctions ( json ) ) ;
43
+ const allText = Array . from (
44
+ document . querySelectorAll ( ".examples-section .code-example pre code, .function-syntax, .expressive-code" )
45
+ ) . map ( el => el . textContent ) . join ( "\n" ) ;
46
+
47
+ allFunctions = new Set ( extractFunctions ( json , allText ) ) ;
48
+
38
49
document . readyState === "loading"
39
50
? window . addEventListener ( "DOMContentLoaded" , onDomReady )
40
51
: onDomReady ( ) ;
0 commit comments