Skip to content

Commit 1c46e8f

Browse files
authored
add syntax highlighting for markdown/html elements in prompt editors (#1373)
* add syntax highlighting for markdown/html elements in prompt editors * add syntax highlighting for markdown/html elements in prompt editors
1 parent de60268 commit 1c46e8f

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

.changeset/some-jeans-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@inkeep/agents-manage-ui": minor
3+
---
4+
5+
add syntax highlighting for markdown/html elements in prompt editors

agents-manage-ui/src/features/agent/state/use-monaco-store.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
3737
{ default: monacoCompatibleSchema },
3838
{ default: githubLightTheme },
3939
{ default: githubDarkTheme },
40+
{ default: markdownShikiLangs },
4041
] = await Promise.all([
4142
import('monaco-editor'),
4243
import('shiki'),
@@ -48,9 +49,9 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
4849
}),
4950
import('shiki/themes/github-light-default.mjs'),
5051
import('shiki/themes/github-dark-default.mjs'),
52+
import('shiki/langs/markdown.mjs'),
5153
import('@/lib/monaco-editor/setup-monaco-workers'),
5254
]);
53-
monaco.languages.register({ id: TEMPLATE_LANGUAGE });
5455
monaco.json.jsonDefaults.setDiagnosticsOptions({
5556
// Fixes when `$schema` is `https://json-schema.org/draft/2020-12/schema`
5657
// The schema uses meta-schema features ($dynamicRef) that are not yet supported by the validator
@@ -74,12 +75,6 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
7475
tokens: false,
7576
});
7677

77-
// Define tokens for template variables
78-
monaco.languages.setMonarchTokensProvider(TEMPLATE_LANGUAGE, {
79-
tokenizer: {
80-
root: [[/\{\{([^}]+)}}/, VARIABLE_TOKEN]],
81-
},
82-
});
8378
monaco.languages.registerCompletionItemProvider(TEMPLATE_LANGUAGE, {
8479
triggerCharacters: ['{'],
8580
provideCompletionItems(model, position) {
@@ -139,6 +134,10 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
139134
};
140135
},
141136
});
137+
const token = `${VARIABLE_TOKEN}.${TEMPLATE_LANGUAGE}`;
138+
const [markdownShikiGrammar] = markdownShikiLangs;
139+
const repo = markdownShikiGrammar.repository;
140+
142141
/**
143142
* Create the highlighter
144143
* @see https://shiki.style/packages/monaco#usage
@@ -153,10 +152,15 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
153152
'editor.background': 'transparent',
154153
'diffEditor.insertedLineBackground': '#3784ff0d',
155154
'diffEditor.insertedTextBackground': '#3784ff19',
156-
'scrollbarSlider.activeBackground': '#aaa5',
157-
'scrollbarSlider.background': '#ccc5',
158-
'scrollbarSlider.hoverBackground': '#bbb5',
155+
'editorHoverWidget.background': '#fff',
159156
},
157+
tokenColors: [
158+
{
159+
scope: token,
160+
settings: { foreground: '#e67e22', fontStyle: 'bold' },
161+
},
162+
...(githubLightTheme.tokenColors || []),
163+
],
160164
},
161165
{
162166
...githubDarkTheme,
@@ -166,14 +170,43 @@ const monacoState: StateCreator<MonacoState> = (set) => ({
166170
'editor.background': 'transparent',
167171
'diffEditor.insertedLineBackground': '#69a3ff33',
168172
'diffEditor.insertedTextBackground': '#69a3ff4d',
169-
'scrollbarSlider.activeBackground': '#ccc5',
170-
'scrollbarSlider.background': '#aaa5',
171-
'scrollbarSlider.hoverBackground': '#bbb5',
173+
'editorHoverWidget.background': '#141416',
174+
},
175+
tokenColors: [
176+
{
177+
scope: token,
178+
settings: { foreground: '#f39c12', fontStyle: 'bold' },
179+
},
180+
...(githubDarkTheme.tokenColors || []),
181+
],
182+
},
183+
],
184+
langs: [
185+
'javascript',
186+
'typescript',
187+
'json',
188+
'html-derivative',
189+
{
190+
...markdownShikiGrammar,
191+
aliases: [],
192+
displayName: 'Template',
193+
name: TEMPLATE_LANGUAGE,
194+
repository: {
195+
...(repo as Record<string, unknown>),
196+
inline: {
197+
...repo.inline,
198+
patterns: [
199+
{ name: token, match: '\\{\\{[^}]+}}' },
200+
// @ts-expect-error -- exist
201+
...repo.inline.patterns,
202+
],
203+
},
172204
},
173205
},
174206
],
175-
langs: ['javascript', 'typescript', 'json'],
176207
});
208+
monaco.languages.register({ id: TEMPLATE_LANGUAGE });
209+
177210
// Register the themes from Shiki, and provide syntax highlighting for Monaco
178211
shikiToMonaco(highlighter, monaco);
179212
// for cypress

0 commit comments

Comments
 (0)