You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, everyone! I'm using shiki to highlight code on the server-side. Here's what I'm doing:
I create and await single instance of the highlighter by calling getHighlighter: I pass in a theme (from the existing ones), and I pass an array of languages.
Then I use this instance to highlight all of the code that I need, by calling codeToThemedTokens and
then renderToHtml.
This works great when we create our project's build. However, I've found that during development, it is incredibly slow. My benchmarks indicate that each page takes around 12 to 15 seconds to load. Each page has around 6 to 12 code blocks to highlight, and each of these code blocks have an HTML and a tsx version.
I ended up opting out of using languages all together to make the DX a bit better. The code comes formatted as usual but without any syntax highlighting.
Any suggestions on how could we could improve this? I would like to provide an alternative to load languages but without having to wait long to work with it during development.
import{getHighlighterasgetHighlighterFromShiki,renderToHtml,typeHighlighter,typeLang,typeTheme,}from"shiki";/** ✅ Config */consttheme: Theme="github-dark";constlangs: Lang[]=["html","tsx"];constbg: React.CSSProperties["backgroundColor"]="#011627";exportasyncfunctiongetHighlighter(){/** Preload NO languages in development */constisDevelopment=process.env.NODE_ENV==="development";/* ✅ Create a highlighter instance with a theme */returnawaitgetHighlighterFromShiki({
theme,langs: isDevelopment ? [] : langs,});}exportasyncfunctionhighlight(highlighter: Highlighter,code: string,lang: Lang="tsx"){/** Request NO languages in development */constisDevelopment=process.env.NODE_ENV==="development";/* ✅ Highlight your code using the right syntax */consttokens=highlighter.codeToThemedTokens(code,isDevelopment ? "" : lang,theme);/* ⚠️ Optional: Custom rendering of code blocks */returnrenderToHtml(tokens,{
bg,elements: {pre({ className, style, children }){return`<pre class="${className}" style="${style}">${children}</pre>`;},line({ children, className, index }){return`<span data-line=${index+1} class=${className}>${children}</span>`;},},});}
The text was updated successfully, but these errors were encountered:
Hello, everyone! I'm using
shiki
to highlight code on the server-side. Here's what I'm doing:getHighlighter
: I pass in a theme (from the existing ones), and I pass an array of languages.codeToThemedTokens
andthen
renderToHtml
.This works great when we create our project's build. However, I've found that during development, it is incredibly slow. My benchmarks indicate that each page takes around 12 to 15 seconds to load. Each page has around 6 to 12 code blocks to highlight, and each of these code blocks have an
HTML
and atsx
version.I ended up opting out of using languages all together to make the DX a bit better. The code comes formatted as usual but without any syntax highlighting.
Any suggestions on how could we could improve this? I would like to provide an alternative to load languages but without having to wait long to work with it during development.
The text was updated successfully, but these errors were encountered: