-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-highlight.js
More file actions
134 lines (124 loc) · 4.89 KB
/
docs-highlight.js
File metadata and controls
134 lines (124 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// docs-highlight.js - Shiki с собственной грамматикой для Ely
import { getHighlighter } from 'shiki';
// Минимальная, но настроенная грамматика для Ely, основанная на вашем файле
const elyGrammar = {
"scopeName": "source.ely",
"name": "Ely",
"patterns": [
{ "include": "#keywords" },
{ "include": "#strings" },
{ "include": "#comments" },
{ "include": "#numbers" },
{ "include": "#functions" },
{ "include": "#builtins" },
{ "include": "#punctuation" }
],
"repository": {
"keywords": {
"patterns": [
{ "match": "\\b(public|private|static|override|async|abstract|sealed|wait|func|class|interface|impl|extends|if|else|elif|for|while|return|break|continue|switch|case|default|foreach|await|new|using|asafe|except|throw|int|flt|str|bool|void|arr|any|true|false|NULL)\\b", "name": "keyword.control.ely" },
{ "match": "\\b(this|super)\\b", "name": "variable.language.ely" }
]
},
"strings": {
"name": "string.quoted.double.ely",
"begin": "(f?)(\"\"\"|''')",
"end": "\\3",
"beginCaptures": { "1": { "name": "storage.type.fstring.ely" }, "2": { "name": "punctuation.definition.string.begin.ely" } }
},
"comments": {
"name": "comment.line.double-slash.ely",
"begin": "//",
"end": "$"
},
"numbers": {
"match": "\\b\\d+(\\.\\d+)?\\b",
"name": "constant.numeric.ely"
},
"functions": {
"match": "\\b([a-zA-Z_][a-zA-Z0-9_]*)\\s*(?=\\()",
"captures": { "1": { "name": "entity.name.function.call.ely" } }
},
"builtins": {
"match": "\\b(println|print|typeof|fields|methods|len|fileExists|fileRemove)\\b",
"name": "support.function.ely"
},
"punctuation": {
"patterns": [
{ "match": "\\(", "name": "punctuation.section.paren.begin.ely" },
{ "match": "\\)", "name": "punctuation.section.paren.end.ely" },
{ "match": "\\{", "name": "punctuation.section.brace.begin.ely" },
{ "match": "\\}", "name": "punctuation.section.brace.end.ely" },
{ "match": "\\[", "name": "punctuation.section.bracket.begin.ely" },
{ "match": "\\]", "name": "punctuation.section.bracket.end.ely" }
]
}
}
};
let highlighterInstance = null;
export async function initShiki() {
if (highlighterInstance) return highlighterInstance;
highlighterInstance = await getHighlighter({
themes: ['dark-plus'],
langs: [
'c',
'go',
'cpp',
'json',
'javascript',
'bash',
'markdown',
'python',
{
id: 'ely',
scopeName: 'source.ely',
grammar: elyGrammar,
name: 'Ely',
aliases: ['elylang']
}
]
});
console.log('Shiki initialized with Ely language');
return highlighterInstance;
}
export async function highlightCodeBlocks(container) {
const highlighter = await initShiki();
const blocks = container.querySelectorAll('pre code');
for (const block of blocks) {
let lang = 'plaintext';
const className = block.className || '';
const match = className.match(/language-(\w+)/);
if (match) {
let shortLang = match[1];
if (shortLang === 'cpp') lang = 'cpp';
else if (shortLang === 'js') lang = 'javascript';
else if (shortLang === 'ely' || shortLang === 'elylang') lang = 'ely';
else if (shortLang === 'bash') lang = 'bash';
else if (shortLang === 'py') lang = 'python';
else if (shortLang === 'go') lang = 'go';
else lang = shortLang;
}
const code = block.textContent;
let highlightedHtml;
try {
highlightedHtml = highlighter.codeToHtml(code, { lang, theme: 'dark-plus' });
} catch (err) {
console.warn(`Failed to highlight ${lang}, using plain text`, err);
highlightedHtml = `<pre class="shiki" style="background-color:#1e1e1e"><code>${escapeHtml(code)}</code></pre>`;
}
const oldPre = block.parentNode;
const tempDiv = document.createElement('div');
tempDiv.innerHTML = highlightedHtml;
const newPre = tempDiv.firstElementChild;
oldPre.parentNode.replaceChild(newPre, oldPre);
}
}
function escapeHtml(str) {
if (!str) return '';
return str.replace(/[&<>]/g, function(m) {
if (m === '&') return '&';
if (m === '<') return '<';
if (m === '>') return '>';
return m;
});
}