-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7c7899
commit 7e92625
Showing
7 changed files
with
447 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* This source file is part of the Swift.org open source project | ||
* | ||
* Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
* Licensed under Apache License v2.0 with Runtime Library Exception | ||
* | ||
* See https://swift.org/LICENSE.txt for license information | ||
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import fetchText from 'docc-render/utils/fetch-text'; | ||
import { | ||
copyPresentProperties, | ||
copyPropertyIfPresent, | ||
has, | ||
mustNotHave, | ||
} from 'docc-render/utils/object-properties'; | ||
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper'; | ||
|
||
/** | ||
* Returns whether the custom script should be run when the reader navigates to a subpage. | ||
* @param {object} customScript | ||
* @returns {boolean} Returns whether the custom script has a `run` property with a value of | ||
* "on-load" or "on-load-and-navigate". Also returns true if the `run` property is absent. | ||
*/ | ||
function shouldRunOnPageLoad(customScript) { | ||
return !has(customScript, 'run') | ||
|| customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate'; | ||
} | ||
|
||
/** | ||
* Returns whether the custom script should be run when the reader navigates to a topic. | ||
* @param {object} customScript | ||
* @returns {boolean} Returns whether the custom script has a `run` property with a value of | ||
* "on-navigate" or "on-load-and-navigate". | ||
*/ | ||
function shouldRunOnNavigate(customScript) { | ||
return has(customScript, 'run') | ||
&& (customScript.run === 'on-navigate' || customScript.run === 'on-load-and-navigate'); | ||
} | ||
|
||
/** | ||
* Gets the URL for a local custom script given its name. | ||
* @param {string} customScriptName The name of the custom script as spelled in | ||
* custom-scripts.json. While the actual filename (in the custom-scripts directory) is always | ||
* expected to end in ".js", the name in custom-scripts.json may or may not include the ".js" | ||
* extension. | ||
* @returns {string} The absolute URL where the script is, accounting for baseURL. | ||
* @example | ||
* // if baseURL if '/foo' | ||
* urlGivenScriptName('hello-world') // http://localhost:8080/foo/hello-world.js | ||
* urlGivenScriptName('hello-world.js') // http://localhost:8080/foo/hello-world.js | ||
*/ | ||
function urlGivenScriptName(customScriptName) { | ||
let scriptNameWithExtension = customScriptName; | ||
|
||
// If the provided name does not already include the ".js" extension, add it. | ||
if (customScriptName.slice(-3) !== '.js') { | ||
scriptNameWithExtension = `${customScriptName}.js`; | ||
} | ||
|
||
return resolveAbsoluteUrl(['', 'custom-scripts', scriptNameWithExtension]); | ||
} | ||
|
||
/** | ||
* Add an HTMLScriptElement containing the custom script to the document's head, which runs the | ||
* script on page load. | ||
* @param {object} customScript The custom script, assuming it should be run on page load. | ||
*/ | ||
function addScriptElement(customScript) { | ||
const scriptElement = document.createElement('script'); | ||
|
||
copyPropertyIfPresent('type', customScript, scriptElement); | ||
|
||
if (has(customScript, 'url')) { | ||
mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.'); | ||
mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.'); | ||
|
||
scriptElement.src = customScript.url; | ||
|
||
copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement); | ||
|
||
// If `integrity` is set on an external script, then CORS must be enabled as well. | ||
if (has(customScript, 'integrity')) { | ||
scriptElement.crossOrigin = 'anonymous'; | ||
} | ||
} else if (has(customScript, 'name')) { | ||
mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.'); | ||
|
||
scriptElement.src = urlGivenScriptName(customScript.name); | ||
|
||
copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement); | ||
} else if (has(customScript, 'code')) { | ||
mustNotHave(customScript, 'async', 'Inline script cannot be `async`.'); | ||
mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.'); | ||
mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.'); | ||
|
||
scriptElement.innerHTML = customScript.code; | ||
} else { | ||
throw new Error('Custom script does not have `url`, `name`, or `code` properties.'); | ||
} | ||
|
||
document.head.appendChild(scriptElement); | ||
} | ||
|
||
/** | ||
* Run the custom script using `eval`. Useful for running a custom script anytime after page load, | ||
* namely when the reader navigates to a subpage. | ||
* @param {object} customScript The custom script, assuming it should be run on navigate. | ||
*/ | ||
async function evalScript(customScript) { | ||
let codeToEval; | ||
|
||
if (has(customScript, 'url')) { | ||
mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.'); | ||
mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.'); | ||
|
||
if (has(customScript, 'integrity')) { | ||
// External script with integrity. Must also use CORS. | ||
codeToEval = await fetchText(customScript.url, { | ||
integrity: customScript.integrity, | ||
crossOrigin: 'anonymous', | ||
}); | ||
} else { | ||
// External script without integrity. | ||
codeToEval = await fetchText(customScript.url); | ||
} | ||
} else if (has(customScript, 'name')) { | ||
mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.'); | ||
|
||
const url = urlGivenScriptName(customScript.name); | ||
|
||
if (has(customScript, 'integrity')) { | ||
// Local script with integrity. Do not use CORS. | ||
codeToEval = await fetchText(url, { integrity: customScript.integrity }); | ||
} else { | ||
// Local script without integrity. | ||
codeToEval = await fetchText(url); | ||
} | ||
} else if (has(customScript, 'code')) { | ||
mustNotHave(customScript, 'async', 'Inline script cannot be `async`.'); | ||
mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.'); | ||
mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.'); | ||
|
||
codeToEval = customScript.code; | ||
} else { | ||
throw new Error('Custom script does not have `url`, `name`, or `code` properties.'); | ||
} | ||
|
||
// eslint-disable-next-line no-eval | ||
eval(codeToEval); | ||
} | ||
|
||
/** | ||
* Run all custom scripts that pass the `predicate` using the `executor`. | ||
* @param {(customScript: object) => boolean} predicate | ||
* @param {(customScript: object) => void} executor | ||
* @returns {Promise<void>} | ||
*/ | ||
async function runCustomScripts(predicate, executor) { | ||
const customScriptsFileName = 'custom-scripts.json'; | ||
const url = resolveAbsoluteUrl(`/${customScriptsFileName}`); | ||
|
||
const response = await fetch(url); | ||
if (!response.ok) { | ||
// If the file is absent, fail silently. | ||
return; | ||
} | ||
|
||
const customScripts = await response.json(); | ||
if (!Array.isArray(customScripts)) { | ||
throw new Error(`Content of ${customScriptsFileName} should be an array.`); | ||
} | ||
|
||
customScripts.filter(predicate).forEach(executor); | ||
} | ||
|
||
export async function runCustomPageLoadScripts() { | ||
await runCustomScripts(shouldRunOnPageLoad, addScriptElement); | ||
} | ||
|
||
export async function runCustomNavigateScripts() { | ||
await runCustomScripts(shouldRunOnNavigate, evalScript); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* This source file is part of the Swift.org open source project | ||
* | ||
* Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
* Licensed under Apache License v2.0 with Runtime Library Exception | ||
* | ||
* See https://swift.org/LICENSE.txt for license information | ||
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper'; | ||
|
||
/** | ||
* Fetch the contents of a file as text. | ||
* @param {string} filepath The file path. | ||
* @param {RequestInit?} options Optional request settings. | ||
* @returns {Promise<string>} The text contents of the file. | ||
*/ | ||
export default async function fetchText(filepath, options) { | ||
const url = resolveAbsoluteUrl(filepath); | ||
return fetch(url, options) | ||
.then(r => r.text()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* This source file is part of the Swift.org open source project | ||
* | ||
* Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
* Licensed under Apache License v2.0 with Runtime Library Exception | ||
* | ||
* See https://swift.org/LICENSE.txt for license information | ||
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
/* eslint-disable */ | ||
|
||
/** Convenient shorthand for `Object.hasOwn`. */ | ||
export const has = Object.hasOwn; | ||
/** | ||
* Copies source.property, if it exists, to destination.property. | ||
* @param {string} property | ||
* @param {object} source | ||
* @param {object} destination | ||
*/ | ||
export function copyPropertyIfPresent(property, source, destination) { | ||
if (has(source, property)) { | ||
// eslint-disable-next-line no-param-reassign | ||
destination[property] = source[property]; | ||
} | ||
} | ||
|
||
/** | ||
* Copies all specified properties present in the source to the destination. | ||
* @param {string[]} properties | ||
* @param {object} source | ||
* @param {object} destination | ||
*/ | ||
export function copyPresentProperties(properties, source, destination) { | ||
properties.forEach((property) => { | ||
copyPropertyIfPresent(property, source, destination); | ||
}); | ||
} | ||
|
||
/** | ||
* Throws an error if `object` has the property `property`. | ||
* @param {object} object | ||
* @param {string} property | ||
* @param {string} errorMessage | ||
*/ | ||
export function mustNotHave(object, property, errorMessage) { | ||
if (has(object, property)) { | ||
throw new Error(errorMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.