mirror of
https://github.com/discourse/discourse.git
synced 2025-01-21 12:24:01 +08:00
d09f283e91
Syntax highlighting is a CPU-intensive process which we run a lot while rendering posts and while using the composer preview. Moving it to a background worker releases the main thread to the browser, which makes the UX much smoother.
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import componentTest from "helpers/component-test";
|
|
import {
|
|
waitForHighlighting,
|
|
setupHighlightJs
|
|
} from "discourse/lib/highlight-syntax";
|
|
|
|
const LONG_CODE_BLOCK = "puts a\n".repeat(15000);
|
|
|
|
moduleForComponent("highlighted-code", { integration: true });
|
|
|
|
componentTest("highlighting code", {
|
|
template: "{{highlighted-code lang='ruby' code=code}}",
|
|
|
|
beforeEach() {
|
|
setupHighlightJs({
|
|
highlightJsUrl: "/assets/highlightjs/highlight-test-bundle.min.js",
|
|
highlightJsWorkerUrl: "/assets/highlightjs-worker.js"
|
|
});
|
|
},
|
|
|
|
async test(assert) {
|
|
this.set("code", "def test; end");
|
|
await waitForHighlighting();
|
|
assert.equal(
|
|
find("code.ruby.hljs .hljs-function .hljs-keyword")
|
|
.text()
|
|
.trim(),
|
|
"def"
|
|
);
|
|
}
|
|
});
|
|
|
|
componentTest("highlighting code limit", {
|
|
template: "{{highlighted-code lang='ruby' code=code}}",
|
|
|
|
beforeEach() {
|
|
setupHighlightJs({
|
|
highlightJsUrl: "/assets/highlightjs/highlight-test-bundle.min.js",
|
|
highlightJsWorkerUrl: "/assets/highlightjs-worker.js"
|
|
});
|
|
},
|
|
|
|
async test(assert) {
|
|
this.set("code", LONG_CODE_BLOCK);
|
|
await waitForHighlighting();
|
|
assert.equal(
|
|
find("code")
|
|
.text()
|
|
.trim(),
|
|
LONG_CODE_BLOCK.trim()
|
|
);
|
|
}
|
|
});
|