-
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
10 changed files
with
316 additions
and
10 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
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
92 changes: 89 additions & 3 deletions
92
packages/erd-editor/src/components/primitives/code-block/CodeBlock.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
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,10 @@ | ||
import { Language } from '@/constants/schema'; | ||
|
||
export const LanguageToLangMap: Record<number, string> = { | ||
[Language.TypeScript]: 'typescript', | ||
[Language.GraphQL]: 'graphql', | ||
[Language.csharp]: 'csharp', | ||
[Language.Java]: 'java', | ||
[Language.Kotlin]: 'kotlin', | ||
[Language.Scala]: 'scala', | ||
}; |
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,30 @@ | ||
import * as Comlink from 'comlink'; | ||
|
||
import type { ShikiService as ShikiServiceType } from '@/workers/shiki.worker'; | ||
import ShikiWorker from '@/workers/shiki.worker?worker&inline'; | ||
|
||
class ShikiService { | ||
private static instance: ShikiService; | ||
|
||
private worker: Comlink.Remote<ShikiServiceType>; | ||
private constructor() { | ||
this.worker = Comlink.wrap<ShikiServiceType>(new ShikiWorker()); | ||
} | ||
|
||
static getInstance() { | ||
if (!ShikiService.instance) { | ||
ShikiService.instance = new ShikiService(); | ||
} | ||
|
||
return ShikiService.instance; | ||
} | ||
|
||
async codeToHtml( | ||
...args: Parameters<InstanceType<typeof ShikiServiceType>['codeToHtml']> | ||
) { | ||
const [code, { lang, theme }] = args; | ||
return await this.worker.codeToHtml(code, { lang, theme }); | ||
} | ||
} | ||
|
||
export { ShikiService }; |
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,118 @@ | ||
import * as Comlink from 'comlink'; | ||
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 }); | ||
}); | ||
} | ||
} | ||
|
||
const service = new ShikiService(); | ||
|
||
Comlink.expose(service); |
Oops, something went wrong.