Skip to content

Custom syntax highlighting & clickable keywords #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions web/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ export default defineConfig({
},
integrations: [
starlight({
expressiveCode: {
themes: [JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_dark.json', 'utf-8')), JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_light.json', 'utf-8'))],
shiki: {
langs: [
{
id: 'lua',
scopeName: 'source.lua.mta',
...JSON.parse(fs.readFileSync('./src/grammars/lua-mta.tmLanguage.json', 'utf-8')),
},
],
},
},

plugins: [
mtasaStarlightThemePlugin(),
],
Expand Down
17 changes: 17 additions & 0 deletions web/ec.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'fs';

export default {
themes: [
JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_dark.json', 'utf-8')),
JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_light.json', 'utf-8')),
],
shiki: {
langs: [
{
id: 'lua',
scopeName: 'source.lua.mta',
...JSON.parse(fs.readFileSync('./src/grammars/lua-mta.tmLanguage.json', 'utf-8')),
},
],
},
};
9 changes: 9 additions & 0 deletions web/mta_highlighting/generate-lua-tmlanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const functionsDir = path.resolve(__dirname, '../../functions');
const basePath = path.resolve(__dirname, './lua-base.tmLanguage.json');
const outputPath = path.resolve(__dirname, '../src/grammars/lua-mta.tmLanguage.json');

const mtaKeywords = ['string','bool','boolean','number','int','float','element','player','vehicle','ped','object','building'];

function extractFunctionsWithScope(yamlContent) {
if (yamlContent.shared?.name) {
return [{ name: yamlContent.shared.name, scope: 'support.function.mta-shared' }];
Expand Down Expand Up @@ -44,6 +46,13 @@ async function generateTmLanguage() {
name: scope,
}));

if (mtaKeywords.length > 0) {
patterns.push({
match: `\\b(${mtaKeywords.join('|')})\\b`,
name: 'keyword.mta',
});
}

const baseGrammar = JSON.parse(fs.readFileSync(basePath, 'utf-8'));
baseGrammar.patterns = [...patterns, ...(baseGrammar.patterns || [])];

Expand Down
41 changes: 41 additions & 0 deletions web/public/mta-keywords_linker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tmLanguage from "../src/grammars/lua-mta.tmLanguage.json";

function extractFunctions() {
const wantedScopes = new Set([
"support.function.mta-shared",
"support.function.mta-server",
"support.function.mta-client",
'keyword.mta'
]);

return tmLanguage.patterns?.reduce((funcs, { match, name }) => {
if (match && wantedScopes.has(name)) {
funcs.push(...match.match(/\\b\(([^)]+)\)\\b/)?.[1]?.split("|") || []);
}
return funcs;
}, []) || [];
}

const allFunctions = new Set(extractFunctions());

function initKeywordLinker() {
function onDomReady() {
const spans = [
...document.querySelectorAll(".examples-section .code-example pre code span"),
...document.querySelectorAll(".function-syntax span, .expressive-code span")
];

spans.forEach(span => {
const text = span.textContent;
if (allFunctions.has(text)) {
span.innerHTML = `<a href="/${text}" class="mta-keyword-link">${text}</a>`;
}
});
}

document.readyState === "loading"
? window.addEventListener("DOMContentLoaded", onDomReady)
: onDomReady();
}

initKeywordLinker();
2 changes: 2 additions & 0 deletions web/src/components/CodeExamplesSection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ const { codeExamples } = Astro.props;
))}
</div>
)}

<script type="module" src="/mta-keywords_linker.js"></script>
4 changes: 4 additions & 0 deletions web/src/grammars/lua-mta.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"match": "\\b(setCursorPosition|setCursorAlpha|getCursorPosition|getCursorAlpha)\\b",
"name": "support.function.mta-client"
},
{
"match": "\\b(string|bool|boolean|number|int|float|element|player|vehicle|ped|object|building)\\b",
"name": "keyword.mta"
},
{
"begin": "\\b(?:(local)\\s+)?(function)\\b(?![,:])",
"beginCaptures": {
Expand Down
9 changes: 9 additions & 0 deletions web/src/styles/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@
.side-server {
color: var(--color-type-server);
}

.mta-keyword-link {
color: inherit;
text-decoration: none;
}

.mta-keyword-link:hover {
font-weight: 100;
}
8 changes: 8 additions & 0 deletions web/src/themes/mtasa_lua-theme_dark.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
}
},

{
"scope": "keyword.mta",
"settings": {
"foreground": "#6edbdb",
"fontStyle": "bold"
}
},

{
"scope": "meta.function.lua",
"settings": {
Expand Down
9 changes: 9 additions & 0 deletions web/src/themes/mtasa_lua-theme_light.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
"fontStyle": "bold"
}
},

{
"scope": "keyword.mta",
"settings": {
"foreground": "#6edb9a",
"fontStyle": "bold"
}
},

{
"scope": "meta.function.lua",
"settings": {
Expand Down