Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New CustomScript addon #431

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions public/_/readthedocs-addons.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@
],
"default_filter": "subprojects:example/latest"
},
"userjsfile": {
"enabled": true,
"src": "userjsfile.js"
},
"hotkeys": {
"enabled": true,
"doc_diff": {
Expand Down
3 changes: 3 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ <h2 id="ethicalads">Ethical Ad</h2>
data-ea-type="readthedocs-sidebar">
</div>

<h2 id="userjsfile">UserJsFile</h2>
<p>Content of the section</p>

<h2 id="search">Search</h2>
<p>Hit <code>/</code> to trigger the search modal, or click on the input from the flyout.</p>
<readthedocs-search></readthedocs-search>
Expand Down
2 changes: 2 additions & 0 deletions public/userjsfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const section = document.querySelector("#userjsfile + p");
section.innerHTML = "This was injected by the userjsfile script";
24 changes: 24 additions & 0 deletions src/data-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,29 @@ const addons_linkpreviews = {
},
};

// Validator for UserJsFile Addon
const addons_userjsfile = {
$id: "http://v1.schemas.readthedocs.org/addons.userjsfile.json",
type: "object",
required: ["addons"],
properties: {
addons: {
type: "object",
required: ["userjsfile"],
properties: {
userjsfile: {
type: "object",
required: ["enabled"],
properties: {
enabled: { type: "boolean" },
src: { type: ["string", "null"] },
},
},
},
},
},
};

export const ajv = new Ajv({
allErrors: true,
schemas: [
Expand All @@ -505,6 +528,7 @@ export const ajv = new Ajv({
addons_search,
addons_linkpreviews,
addons_filetreediff,
addons_userjsfile,
],
});

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as ethicalads from "./ethicalads";
import * as hotkeys from "./hotkeys";
import * as linkpreviews from "./linkpreviews";
import * as filetreediff from "./filetreediff";
import * as userjsfile from "./userjsfile";
import {
domReady,
IS_PRODUCTION,
Expand All @@ -26,6 +27,7 @@ export function setup() {
hotkeys.HotKeysAddon,
linkpreviews.LinkPreviewsAddon,
filetreediff.FileTreeDiffAddon,
userjsfile.UserJsFileAddon,
];

return new Promise((resolve) => {
Expand Down
35 changes: 35 additions & 0 deletions src/userjsfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { default as objectPath } from "object-path";
import { AddonBase } from "./utils";

const SCRIPT_ID = "readthedocs-addons-user-js-file";

/**
* User JavaScript file.
*
* Allow a user to inject a custom JavaScript file in all the pages.
*/
export class UserJsFileAddon extends AddonBase {
static jsonValidationURI =
"http://v1.schemas.readthedocs.org/addons.userjsfile.json";
static addonEnabledPath = "addons.userjsfile.enabled";
static addonName = "UserJsFile";
static enabledOnHttpStatus = [200, 403, 404, 500];

constructor(config) {
super();
this.config = config;

if (objectPath.get(this.config, "addons.userjsfile.src")) {
this.injectJavaScriptFile();
}
}

injectJavaScriptFile() {
const script = document.createElement("script");
script.id = SCRIPT_ID;
script.src = objectPath.get(this.config, "addons.userjsfile.src");
script.async = true;

document.body.appendChild(script);
}
}
38 changes: 38 additions & 0 deletions tests/userjsfile.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<body>
<script type="module">
import { expect, elementUpdated } from "@open-wc/testing";
import { runTests } from "@web/test-runner-mocha";
import * as userjsfile from "../src/userjsfile";

let config;

runTests(async () => {
beforeEach(() => {
config = {
addons: {
userjsfile: {
enabled: true,
src: "https://myproject.readthedocs.io/en/latest/_static/readthedocs.js",
},
},
};
});

describe("UserJsFile addon", () => {
it("script element is added", async () => {
const addon = new userjsfile.UserJsFileAddon(config);
const element = document.querySelector(
"#readthedocs-addons-user-js-file",
);

expect(element.src).to.equal(
"https://myproject.readthedocs.io/en/latest/_static/readthedocs.js",
);
expect(element.async).to.equal(true);
});
});
});
</script>
</body>
</html>
42 changes: 42 additions & 0 deletions tests/userjsfile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, assert, fixture, html } from "@open-wc/testing";
import { UserJsFileAddon } from "../src/userjsfile";

describe("UserJsFile addon", () => {
it("invalid configuration disables the addon", () => {
expect(
UserJsFileAddon.isEnabled({
addons: {
userjsfile: {
invalid: true,
},
},
}),
).to.be.false;
});

it("is disabled", () => {
expect(
UserJsFileAddon.isEnabled({
addons: {
userjsfile: {
enabled: false,
src: "/en/latest/_static/readthedocs.js",
},
},
}),
).to.be.false;
});

it("valid data and enabled", () => {
expect(
UserJsFileAddon.isEnabled({
addons: {
userjsfile: {
enabled: true,
src: "/en/latest/_static/readthedocs.js",
},
},
}),
).to.be.true;
});
});