forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move syntax highlighting to web worker (go-gitea#11017)
This should eliminate page freezes when loading big files/diff. `highlightBlock` is needed to preserve existing nodes when highlighting and for that, highlight.js needs access to the DOM API so I added a DOM implementation to make it work, which adds around 300kB to the output file size of the lazy-loaded `highlight.js`. Co-authored-by: Lauris BH <[email protected]>
- Loading branch information
1 parent
cc4da79
commit 27e3cdd
Showing
7 changed files
with
83 additions
and
17 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 +1,19 @@ | ||
export default async function initHighlight() { | ||
if (!window.config || !window.config.HighlightJS) return; | ||
export default async function highlight(elementOrNodeList) { | ||
if (!window.config || !window.config.HighlightJS || !elementOrNodeList) return; | ||
const nodes = 'length' in elementOrNodeList ? elementOrNodeList : [elementOrNodeList]; | ||
if (!nodes.length) return; | ||
|
||
const hljs = await import(/* webpackChunkName: "highlight" */'highlight.js'); | ||
const {default: Worker} = await import(/* webpackChunkName: "highlight" */'./highlight.worker.js'); | ||
const worker = new Worker(); | ||
|
||
const nodes = [].slice.call(document.querySelectorAll('pre code') || []); | ||
for (let i = 0; i < nodes.length; i++) { | ||
hljs.highlightBlock(nodes[i]); | ||
} | ||
worker.addEventListener('message', ({data}) => { | ||
const {index, html} = data; | ||
nodes[index].outerHTML = html; | ||
}); | ||
|
||
return hljs; | ||
for (let index = 0; index < nodes.length; index++) { | ||
const node = nodes[index]; | ||
if (!node) continue; | ||
worker.postMessage({index, html: node.outerHTML}); | ||
} | ||
} |
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 {highlightBlock} from 'highlight.js'; | ||
import {createWindow} from 'domino'; | ||
|
||
self.onmessage = function ({data}) { | ||
const window = createWindow(); | ||
self.document = window.document; | ||
|
||
const {index, html} = data; | ||
document.body.innerHTML = html; | ||
highlightBlock(document.body.firstChild); | ||
self.postMessage({index, html: document.body.innerHTML}); | ||
}; |
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