From 90ac5fe0167cd458658352b3949793aef58bcb12 Mon Sep 17 00:00:00 2001 From: bryantgillespie Date: Mon, 9 Sep 2024 13:30:36 -0400 Subject: [PATCH] Refactor code-highlighter.post.ts to use createHighlighter instead of getHighlighter --- server/api/code-highlighter.post.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/api/code-highlighter.post.ts b/server/api/code-highlighter.post.ts index efbf586d..360e996e 100644 --- a/server/api/code-highlighter.post.ts +++ b/server/api/code-highlighter.post.ts @@ -1,5 +1,5 @@ import type { ThemeInput } from 'shiki'; -import { getHighlighter } from 'shiki'; +import { createHighlighter } from 'shiki'; import lightTheme from '~/assets/shiki/directus-light.json'; import darkTheme from '~/assets/shiki/directus-dark.json'; @@ -9,17 +9,21 @@ interface CodeHighlighterBody { background: string; } +let shiki: any + export default defineEventHandler(async (event) => { const body = await readBody(event); - const { snippet, language, background } = body as CodeHighlighterBody; - - try { - const shiki = await getHighlighter({ + if(!shiki) { + shiki = await createHighlighter({ themes: [lightTheme as unknown as ThemeInput, darkTheme as unknown as ThemeInput], langs: ['graphql', 'http', 'typescript', 'sql', 'json'], }); + } + const { snippet, language, background } = body as CodeHighlighterBody; + + try { const codeSnippet = shiki.codeToHtml(snippet, { lang: language, theme: background === 'dark-night' ? 'directus-dark' : 'directus-light',