Optimizing Syntax Highlighting: Upgrading Shiki in svgl
Improving Performance and Accuracy
In the svgl project, maintaining high-quality code rendering is a priority. Syntax highlighting is a core feature that relies on powerful underlying tools to ensure that code snippets are both legible and performant. Recently, we decided to upgrade our Shiki configuration to leverage the latest improvements in syntax highlighting performance and language support.
The Upgrade Path
Shiki provides a robust way to transform source code into highlighted HTML and CSS. By keeping the configuration up-to-date, we ensure that the application benefits from better tree-sitter support and smaller bundle sizes. The configuration typically involves defining themes and languages to support within the Svelte environment.
Here is how we represent the integration of a syntax highlighter in our workflow:
import { createHighlighter } from 'shiki';
// Initialize the highlighter with custom themes
const highlighter = await createHighlighter({
themes: ['github-dark', 'github-light'],
langs: ['javascript', 'svelte']
});
// Render code into HTML
const html = highlighter.codeToHtml('console.log("Hello Svelte");', {
lang: 'javascript',
theme: 'github-dark'
});
This configuration ensures that whenever a user views a component, the highlighting process remains snappy and consistent. By upgrading the dependency, we have ensured that our rendering pipeline handles modern JavaScript syntax and edge-case grammar more effectively than previous versions.
Key Takeaways
- Regular dependency updates keep your syntax highlighting fast and accurate.
- Centralizing your highlighter configuration allows for easy theme swapping across your Svelte components.
- Always test rendering across light and dark modes after updating highlighters to ensure color contrast remains compliant with accessibility standards.
Next, try auditing your project's bundle size after an upgrade to see how the latest optimizations impact your load times.
Generated with Gitvlg.com