Skip to content

Commit acd6743

Browse files
LeaVerouDmitrySharabin
authored andcommitted
Move documentReady to async utils
1 parent eedc845 commit acd6743

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/auto-start.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Prism from './global';
22
import autoloader from './plugins/autoloader/prism-autoloader';
3+
import { documentReady } from './util/async';
34

45
Prism.components.add(autoloader);
56

@@ -22,7 +23,6 @@ export const PrismConfig = {
2223
* ```
2324
*
2425
* @default false
25-
* @public
2626
*/
2727
manual: false,
2828
};
@@ -34,29 +34,11 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') {
3434
PrismConfig.manual = true;
3535
}
3636

37-
const highlightAutomaticallyCallback = () => {
37+
void documentReady().then(() => {
3838
if (!PrismConfig.manual) {
3939
Prism.highlightAll();
4040
}
41-
};
42-
43-
// If the document state is "loading", then we'll use DOMContentLoaded.
44-
// If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
45-
// DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
46-
// might take longer one animation frame to execute which can create a race condition where only some plugins have
47-
// been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
48-
// See https://github.com/PrismJS/prism/issues/2102
49-
// See https://github.com/PrismJS/prism/issues/3535
50-
const readyState = document.readyState;
51-
if (
52-
readyState === 'loading' ||
53-
(readyState === 'interactive' && script && script.defer && !script.async)
54-
) {
55-
document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
56-
}
57-
else {
58-
window.requestAnimationFrame(highlightAutomaticallyCallback);
59-
}
41+
});
6042
}
6143
else {
6244
PrismConfig.manual = true;

src/util/async.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function documentReady (document = globalThis.document) {
2+
const script = document.currentScript as HTMLScriptElement | null;
3+
4+
if (!document) {
5+
return Promise.reject();
6+
}
7+
8+
// If the document state is "loading", then we'll use DOMContentLoaded.
9+
// If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
10+
// DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
11+
// might take longer one animation frame to execute which can create a race condition where only some plugins have
12+
// been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
13+
// See https://github.com/PrismJS/prism/issues/2102
14+
// See https://github.com/PrismJS/prism/issues/3535
15+
const readyState = document.readyState;
16+
if (
17+
readyState === 'loading' ||
18+
(readyState === 'interactive' && script && script.defer && !script.async)
19+
) {
20+
return new Promise(resolve => {
21+
document.addEventListener('DOMContentLoaded', resolve, { once: true });
22+
});
23+
}
24+
25+
return Promise.resolve();
26+
}

0 commit comments

Comments
 (0)