How is it possible to add code highlighting to the ai responses in the message list e.g. by using shiki? (Vaadin 25.1)
The MessageList supports mark down formatted text. So that is one approach.
Yes, I am aware of MD. I already activated it.
But I cannot manage to let shiki do the formating of code blocks to get an enhanced rendering.
Vaadin Markdown (also used internally by MessageList) does not (yet at least) provide an API for defining plugins for the renderer. However, the component internally uses the “marked” library, which means you can register “marked” plugins globally. The following worked for me:
-
Add “shiki” as a dependency to “package.json”.
-
Add
<script src="./marked-plugins.js" type="module"></script>to “index.html”. -
Create a “marked-plugins.js” file in the “frontend” directory, with the following content:
import { marked } from "marked";
import { createHighlighter } from "shiki";
const highlighter = await createHighlighter({
langs: ["js", "ts", "java", "html", "css", "json", "xml", "bash", "sql", "md"],
themes: ["min-light"],
});
marked.use({
renderer: {
code({ text, lang }) {
return highlighter.codeToHtml(text, {
lang: lang || "text",
theme: "min-light",
});
},
},
});
Thank you very much! That works fine!
I had to add marked to “package.json, too”
I believe “marked”: “16.4.2” i the correct version to use with Vaadin 25.1.0?
Glad to hear it works. I wonder why “marked” isn’t added automatically by Vaadin… anyway version “16.4.2” is fine since the dependency is “^16.0.0”
