0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Perf: reuse Shiki highlighters per theme/lang (#3130)

* reuse Shiki highlighters per theme/lang

* chore: adding changeset
This commit is contained in:
Tony Sullivan 2022-04-18 16:30:19 +00:00 committed by GitHub
parent 9e35758ec3
commit 394ab90547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Updates `<Code />` component to cache and reuse Shiki highlighters

View file

@ -1,6 +1,6 @@
---
import type * as shiki from 'shiki';
import { getHighlighter } from 'shiki';
import { getHighlighter } from './Shiki.js';
export interface Props {
/** The code to highlight. Required. */

View file

@ -0,0 +1,22 @@
import { getHighlighter as getShikiHighlighter } from 'shiki';
// Caches Promise<Highligher> for reuse when the same theme and langs are provided
const _resolvedHighlighters = new Map();
function stringify(opts) {
// Always sort keys before stringifying to make sure objects match regardless of parameter ordering
return JSON.stringify(opts, Object.keys(opts).sort());
}
export function getHighlighter(opts) {
const key = stringify(opts);
// Highlighter has already been requested, reuse the same instance
if (_resolvedHighlighters.has(key)) { return _resolvedHighlighters.get(key) }
// Start the async getHighlighter call and cache the Promise
const highlighter = getShikiHighlighter(opts);
_resolvedHighlighters.set(key, highlighter);
return highlighter;
}