Skip to content

Commit

Permalink
Move syntax highlighting to web worker (go-gitea#11017)
Browse files Browse the repository at this point in the history
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
silverwind and lafriks committed Apr 13, 2020
1 parent cc4da79 commit 27e3cdd
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ globals:
SimpleMDE: false
u2fApi: false

overrides:
- files: ["web_src/**/*.worker.js"]
env:
worker: true
rules:
no-restricted-globals: [0]

rules:
arrow-body-style: [0]
arrow-parens: [2, always]
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"core-js": "3.6.4",
"css-loader": "3.4.2",
"cssnano": "4.1.10",
"domino": "2.1.4",
"dropzone": "5.7.0",
"fast-glob": "3.2.2",
"fomantic-ui": "2.8.4",
Expand Down Expand Up @@ -44,7 +45,8 @@
"vue-template-compiler": "2.6.11",
"webpack": "4.42.0",
"webpack-cli": "3.3.11",
"webpack-fix-style-only-entries": "0.4.0"
"webpack-fix-style-only-entries": "0.4.0",
"worker-loader": "2.0.0"
},
"devDependencies": {
"eslint": "6.8.0",
Expand Down
23 changes: 15 additions & 8 deletions web_src/js/features/highlight.js
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});
}
}
12 changes: 12 additions & 0 deletions web_src/js/features/highlight.worker.js
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});
};
15 changes: 7 additions & 8 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import './vendor/semanticdropdown.js';
import {svg} from './utils.js';

import initContextPopups from './features/contextpopup.js';
import initHighlight from './features/highlight.js';
import initGitGraph from './features/gitgraph.js';
import initClipboard from './features/clipboard.js';
import initUserHeatmap from './features/userheatmap.js';
import initDateTimePicker from './features/datetimepicker.js';
import createDropzone from './features/dropzone.js';
import highlight from './features/highlight.js';
import ActivityTopAuthors from './components/ActivityTopAuthors.vue';

const {AppSubUrl, StaticUrlPrefix, csrf} = window.config;
Expand All @@ -29,7 +29,6 @@ let previewFileModes;
let simpleMDEditor;
const commentMDEditors = {};
let codeMirrorEditor;
let hljs;

// Silence fomantic's error logging when tabs are used without a target content element
$.fn.tab.settings.silent = true;
Expand All @@ -49,7 +48,7 @@ function initCommentPreviewTab($form) {
$previewPanel.html(data);
emojify.run($previewPanel[0]);
$('pre code', $previewPanel[0]).each(function () {
hljs.highlightBlock(this);
highlight(this);
});
});
});
Expand All @@ -75,7 +74,7 @@ function initEditPreviewTab($form) {
$previewPanel.html(data);
emojify.run($previewPanel[0]);
$('pre code', $previewPanel[0]).each(function () {
hljs.highlightBlock(this);
highlight(this);
});
});
});
Expand Down Expand Up @@ -1011,7 +1010,7 @@ async function initRepository() {
$renderContent.html(data.content);
emojify.run($renderContent[0]);
$('pre code', $renderContent[0]).each(function () {
hljs.highlightBlock(this);
highlight(this);
});
}
const $content = $segment.parent();
Expand Down Expand Up @@ -1337,7 +1336,7 @@ function initWikiForm() {
preview.innerHTML = `<div class="markdown ui segment">${data}</div>`;
emojify.run($('.editor-preview')[0]);
$(preview).find('pre code').each((_, e) => {
hljs.highlightBlock(e);
highlight(e);
});
});
};
Expand Down Expand Up @@ -2633,8 +2632,8 @@ $(document).ready(async () => {
});

// parallel init of lazy-loaded features
[hljs] = await Promise.all([
initHighlight(),
await Promise.all([
highlight(document.querySelectorAll('pre code')),
initGitGraph(),
initClipboard(),
initUserHeatmap(),
Expand Down
14 changes: 14 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
sourceMap: true,
extractComments: false,
terserOptions: {
keep_fnames: /^(HTML|SVG)/, // https://github.com/fgnass/domino/issues/144
output: {
comments: false,
},
Expand Down Expand Up @@ -89,6 +90,19 @@ module.exports = {
test: require.resolve('jquery-datetimepicker'),
use: 'imports-loader?define=>false,exports=>false',
},
{
test: /\.worker\.js$/,
use: [
{
loader: 'worker-loader',
options: {
name: '[name].js',
inline: true,
fallback: false,
},
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
Expand Down

0 comments on commit 27e3cdd

Please sign in to comment.