Skip to content

Commit 9f77d63

Browse files
committed
Use enum for the run property instead of magic strings
1 parent a78af75 commit 9f77d63

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/utils/custom-scripts.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import {
1717
} from 'docc-render/utils/object-properties';
1818
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';
1919

20+
/** Enum for the allowed values of the `run` property in a custom script. */
21+
const Run = {
22+
onLoad: 'on-load',
23+
onLoadAndNavigate: 'on-load-and-navigate',
24+
onNavigate: 'on-navigate',
25+
};
26+
2027
/**
2128
* Returns whether the custom script should be run when the reader navigates to a subpage.
2229
* @param {object} customScript
@@ -25,7 +32,7 @@ import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';
2532
*/
2633
function shouldRunOnPageLoad(customScript) {
2734
return !has(customScript, 'run')
28-
|| customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate';
35+
|| customScript.run === Run.onLoad || customScript.run === Run.onLoadAndNavigate;
2936
}
3037

3138
/**
@@ -36,7 +43,7 @@ function shouldRunOnPageLoad(customScript) {
3643
*/
3744
function shouldRunOnNavigate(customScript) {
3845
return has(customScript, 'run')
39-
&& (customScript.run === 'on-navigate' || customScript.run === 'on-load-and-navigate');
46+
&& (customScript.run === Run.onNavigate || customScript.run === Run.onLoadAndNavigate);
4047
}
4148

4249
/**
@@ -78,7 +85,7 @@ function addScriptElement(customScript) {
7885

7986
scriptElement.src = customScript.url;
8087

81-
// Dynamically-created script elements are `async` by default. But we don't want custom
88+
// Dynamically-created script elements are `async` by default. But we don't want custom
8289
// scripts to be implicitly async by default, because if a documentation author adds `defer` to
8390
// some or all of their custom scripts (meaning that they want the execution order of those
8491
// scripts to be deterministic), then the author's `defer` will be overriden by the implicit

0 commit comments

Comments
 (0)