Skip to content

Commit

Permalink
Use enum for the run property instead of magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucca-mito committed Jan 12, 2025
1 parent a78af75 commit 9f77d63
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/utils/custom-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import {
} from 'docc-render/utils/object-properties';
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';

/** Enum for the allowed values of the `run` property in a custom script. */
const Run = {
onLoad: 'on-load',
onLoadAndNavigate: 'on-load-and-navigate',
onNavigate: 'on-navigate',
};

/**
* Returns whether the custom script should be run when the reader navigates to a subpage.
* @param {object} customScript
Expand All @@ -25,7 +32,7 @@ import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';
*/
function shouldRunOnPageLoad(customScript) {
return !has(customScript, 'run')
|| customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate';
|| customScript.run === Run.onLoad || customScript.run === Run.onLoadAndNavigate;
}

/**
Expand All @@ -36,7 +43,7 @@ function shouldRunOnPageLoad(customScript) {
*/
function shouldRunOnNavigate(customScript) {
return has(customScript, 'run')
&& (customScript.run === 'on-navigate' || customScript.run === 'on-load-and-navigate');
&& (customScript.run === Run.onNavigate || customScript.run === Run.onLoadAndNavigate);
}

/**
Expand Down Expand Up @@ -78,7 +85,7 @@ function addScriptElement(customScript) {

scriptElement.src = customScript.url;

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

0 comments on commit 9f77d63

Please sign in to comment.