-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { getHighlighter, setWasm, toShikiTheme } from 'shiki'; | ||
// @ts-ignore | ||
import wasmUrl from 'shiki/dist/onig.wasm?url'; | ||
import csharp from 'shiki/languages/csharp.tmLanguage.json'; | ||
import graphql from 'shiki/languages/graphql.tmLanguage.json'; | ||
import java from 'shiki/languages/java.tmLanguage.json'; | ||
import kotlin from 'shiki/languages/kotlin.tmLanguage.json'; | ||
import scala from 'shiki/languages/scala.tmLanguage.json'; | ||
import sql from 'shiki/languages/sql.tmLanguage.json'; | ||
import typescript from 'shiki/languages/typescript.tmLanguage.json'; | ||
import githubDark from 'shiki/themes/github-dark.json'; | ||
import githubLight from 'shiki/themes/github-light.json'; | ||
|
||
const themeMap: Record<string, any> = { | ||
dark: githubDark, | ||
light: githubLight, | ||
}; | ||
|
||
const languages: Array<any> = [ | ||
{ | ||
id: 'typescript', | ||
scopeName: 'source.ts', | ||
displayName: 'TypeScript', | ||
aliases: ['ts'], | ||
grammar: typescript, | ||
}, | ||
{ | ||
id: 'sql', | ||
scopeName: 'source.sql', | ||
displayName: 'SQL', | ||
grammar: sql, | ||
}, | ||
{ | ||
id: 'graphql', | ||
scopeName: 'source.graphql', | ||
displayName: 'GraphQL', | ||
aliases: ['gql'], | ||
grammar: graphql, | ||
}, | ||
{ | ||
id: 'csharp', | ||
scopeName: 'source.cs', | ||
displayName: 'C#', | ||
aliases: ['c#', 'cs'], | ||
grammar: csharp, | ||
}, | ||
{ | ||
id: 'java', | ||
scopeName: 'source.java', | ||
displayName: 'Java', | ||
grammar: java, | ||
}, | ||
{ | ||
id: 'kotlin', | ||
scopeName: 'source.kotlin', | ||
displayName: 'Kotlin', | ||
aliases: ['kt', 'kts'], | ||
grammar: kotlin, | ||
}, | ||
{ | ||
id: 'scala', | ||
scopeName: 'source.scala', | ||
displayName: 'Scala', | ||
grammar: scala, | ||
}, | ||
]; | ||
|
||
function getThemeKey(theme?: string): 'dark' | 'light' { | ||
return theme === 'dark' || theme === 'light' ? theme : 'dark'; | ||
} | ||
|
||
export class ShikiService { | ||
private ready: Promise<void>; | ||
|
||
constructor() { | ||
this.ready = new Promise(async (resolve, reject) => { | ||
try { | ||
const wasmResponse = await fetch(wasmUrl); | ||
setWasm(wasmResponse); | ||
resolve(); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
async codeToHtml( | ||
code: string, | ||
{ | ||
lang, | ||
theme, | ||
}: { | ||
lang: | ||
| 'sql' | ||
| 'typescript' | ||
| 'graphql' | ||
| 'csharp' | ||
| 'java' | ||
| 'kotlin' | ||
| 'scala'; | ||
theme?: 'dark' | 'light'; | ||
} | ||
) { | ||
return await this.ready.then(async () => { | ||
const highlighter = await getHighlighter({ | ||
theme: toShikiTheme(Reflect.get(themeMap, getThemeKey(theme))), | ||
langs: languages, | ||
}); | ||
|
||
return highlighter.codeToHtml(code, { lang }); | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/erd-editor-shiki-worker/src/shiki.shared-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Comlink from 'comlink'; | ||
|
||
import { ShikiService } from './shiki.service'; | ||
|
||
declare var self: SharedWorkerGlobalScope; | ||
|
||
const service = new ShikiService(); | ||
|
||
self.onconnect = event => { | ||
const port = event.ports[0]; | ||
Comlink.expose(service, port); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare const __APP_VERSION__: string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters