Skip to content

Commit ab11682

Browse files
committed
Fixed linking of words that aren’t highlighted
local a = nil
1 parent c7bd3e3 commit ab11682

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

web/public/mta-keyword_linker.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
let allFunctions = new Set();
22

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));
1418
}
15-
return funcs;
16-
}, []) || [];
19+
});
20+
21+
return Array.from(result);
1722
}
1823

24+
1925
function initKeywordLinker() {
2026
function onDomReady() {
2127
const spans = [
@@ -34,7 +40,12 @@ function initKeywordLinker() {
3440
fetch('/grammars/lua-mta.tmLanguage.json')
3541
.then(res => res.json())
3642
.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+
3849
document.readyState === "loading"
3950
? window.addEventListener("DOMContentLoaded", onDomReady)
4051
: onDomReady();

0 commit comments

Comments
 (0)