Skip to content

Commit

Permalink
Refactor code-highlighter.post.ts to use createHighlighter instead of…
Browse files Browse the repository at this point in the history
… getHighlighter
  • Loading branch information
bryantgillespie committed Sep 9, 2024
1 parent 47fb8c5 commit 90ac5fe
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions server/api/code-highlighter.post.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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',
Expand Down

0 comments on commit 90ac5fe

Please sign in to comment.