-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load highlight to webworker if available
- Loading branch information
Showing
7 changed files
with
84 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import hljs from 'highlight.js/lib/core'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import json from 'highlight.js/lib/languages/json'; | ||
import { stacktraceJS } from './languages/stacktrace'; | ||
|
||
hljs.registerLanguage('json', json); | ||
hljs.registerLanguage('stacktrace', stacktraceJS); | ||
|
||
export const highlighter = hljs; |
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,47 @@ | ||
import { nanoid } from 'nanoid'; | ||
|
||
const isWebworkerSupported = typeof window.Worker !== 'undefined'; | ||
let highlightWorker: Worker | null = null; | ||
const messageQueue = new Map<string, { resolve: any; reject: any }>(); | ||
|
||
export async function asyncHighlight(code: string, language: string): Promise<string> { | ||
if (isWebworkerSupported) { | ||
if (!highlightWorker) { | ||
highlightWorker = new Worker( | ||
/* webpackChunkName: "highlight-worker" */ new URL('./worker.ts', import.meta.url) | ||
); | ||
highlightWorker.onmessage = ({ data }) => { | ||
const { id, code } = data; | ||
if (messageQueue.has(id)) { | ||
const { resolve } = messageQueue.get(id) as any; | ||
resolve(code); | ||
} | ||
}; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const messageId = nanoid(5); | ||
highlightWorker?.postMessage({ id: messageId, code, language }); | ||
messageQueue.set(messageId, { | ||
resolve: (formattedCode: string) => { | ||
messageQueue.delete(messageId); | ||
resolve(formattedCode); | ||
}, | ||
reject: () => { | ||
messageQueue.delete(messageId); | ||
reject(); | ||
}, | ||
}); | ||
setTimeout(() => reject(), 60 * 1000); | ||
}); | ||
} else { | ||
const { highlighter } = await import( | ||
/* webpackChunkName: "highlight" */ | ||
/* webpackMode: "lazy-once" */ | ||
/* webpackPreload: true */ | ||
'./config' | ||
); | ||
|
||
return highlighter.highlightAuto(code, [language]).value || ''; | ||
} | ||
} |
File renamed without changes.
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 { highlighter } from './config'; | ||
|
||
self.onmessage = ({ data = {} }) => { | ||
const { id = '', code = '', language = '' } = data; | ||
if (!id || !code || !language) { | ||
return; | ||
} | ||
|
||
const resp = highlighter.highlightAuto(code, [language]); | ||
|
||
self.postMessage({ code: resp.value, id }); | ||
}; |
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