Skip to content

Commit 7e7ab39

Browse files
theletterfclaudecursoragent
authored
Add KQL syntax highlighting for kuery code blocks (#2677)
Replace the JSON fallback for `kuery` code blocks with a dedicated highlight.js language definition based on the KQL grammar from Kibana. Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 4bceccf commit 7e7ab39

4 files changed

Lines changed: 85 additions & 7 deletions

File tree

docs/testing/custom-highlighters.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ modulo(10, 0.5)
7373

7474

7575

76+
## KQL
77+
78+
Simple field-value query:
79+
80+
```kuery
81+
status: active
82+
```
83+
84+
Boolean operators and quoted strings:
85+
86+
```kuery
87+
status: active and extension: "php"
88+
```
89+
90+
Wildcards and negation:
91+
92+
```kql
93+
machine.os: win* and not status: 503
94+
```
95+
96+
Nested queries:
97+
98+
```kql
99+
items: { name: "laptop" and price > 500 }
100+
```
101+
102+
Grouped values with range operators:
103+
104+
```kuery
105+
response: (200 or 404) and bytes >= 1000
106+
```
107+
76108
## ESQL
77109

78110

src/Elastic.Documentation.Site/Assets/hljs.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,52 @@ hljs.registerLanguage('painless', function () {
165165
}
166166
})
167167

168+
hljs.registerLanguage('kuery', function () {
169+
return {
170+
case_insensitive: true,
171+
keywords: {
172+
keyword: 'and or not',
173+
literal: ['true', 'false', 'null'],
174+
},
175+
contains: [
176+
// Field names followed by : or range operators
177+
{
178+
scope: 'attribute',
179+
match: /[a-zA-Z_][a-zA-Z0-9._]*(?=\s*(?::|<=|>=|<|>))/,
180+
},
181+
// Quoted strings
182+
{
183+
scope: 'string',
184+
begin: /"/,
185+
end: /"/,
186+
contains: [
187+
{
188+
scope: 'char.escape',
189+
match: /\\[\\"\t\r\n]|\\u[0-9a-fA-F]{4}/,
190+
},
191+
],
192+
},
193+
// Range and match operators
194+
{
195+
scope: 'operator',
196+
match: /<=|>=|<|>|:/,
197+
},
198+
// Wildcards
199+
{
200+
scope: 'operator',
201+
match: /\*/,
202+
},
203+
// Parentheses and braces (grouping / nested queries)
204+
{
205+
scope: 'punctuation',
206+
match: /[(){}]/,
207+
},
208+
NUMBER,
209+
],
210+
}
211+
})
212+
hljs.registerAliases(['kql'], { languageName: 'kuery' })
213+
168214
hljs.addPlugin(mergeHTMLPlugin)
169215

170216
// The unescaped HTML warning is caused by the mergeHTMLPlugin which we are using

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public override bool Close(BlockProcessor processor, Block block)
9898
"terminal" => "bash",
9999
"painless" => "java",
100100
//TODO support these natively
101-
"kuery" => "json",
102101
"lucene" => "json",
103102
_ => codeBlock.Language
104103
};

src/Elastic.Markdown/Myst/CodeBlocks/SupportedLanguages.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ public static class CodeBlock
4444
{ "xml", "html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg" }, // HTML, XML
4545
{ "yml", "yaml" }, // YAML
4646

47-
//CUSTOM, Elastic language we wrote highlighters for
48-
{ "apiheader", "" },
49-
{ "eql", "" },
50-
{ "esql", "" },
51-
{ "mermaid", "" },
52-
{ "painless", "" }
47+
//CUSTOM, Elastic language we wrote highlighters for
48+
{ "apiheader", "" },
49+
{ "eql", "" },
50+
{ "esql", "" },
51+
{ "kuery", "kql" },
52+
{ "mermaid", "" },
53+
{ "painless", "" }
5354
};
5455

5556
public static HashSet<string> Languages { get; } = new(

0 commit comments

Comments
 (0)