Skip to content

Commit da6ab16

Browse files
committed
New UserJsFile addon
This is a new addon that allow users to inject a specific JavaScript file to all the versions to all the documentation pages to allow them to fix issues in old "frozen" (not able to re-build) versions. Requires readthedocs/readthedocs.org#11758
1 parent 3039290 commit da6ab16

7 files changed

+148
-9
lines changed

dist/readthedocs-addons.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/readthedocs-addons.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/data-validation.js

+24
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,29 @@ const addons_linkpreviews = {
493493
},
494494
};
495495

496+
// Validator for UserJsFile Addon
497+
const addons_userjsfile = {
498+
$id: "http://v1.schemas.readthedocs.org/addons.userjsfile.json",
499+
type: "object",
500+
required: ["addons"],
501+
properties: {
502+
addons: {
503+
type: "object",
504+
required: ["userjsfile"],
505+
properties: {
506+
userjsfile: {
507+
type: "object",
508+
required: ["enabled"],
509+
properties: {
510+
enabled: { type: "boolean" },
511+
src: { type: ["string", "null"] },
512+
},
513+
},
514+
},
515+
},
516+
},
517+
};
518+
496519
export const ajv = new Ajv({
497520
allErrors: true,
498521
schemas: [
@@ -505,6 +528,7 @@ export const ajv = new Ajv({
505528
addons_search,
506529
addons_linkpreviews,
507530
addons_filetreediff,
531+
addons_userjsfile,
508532
],
509533
});
510534

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as ethicalads from "./ethicalads";
88
import * as hotkeys from "./hotkeys";
99
import * as linkpreviews from "./linkpreviews";
1010
import * as filetreediff from "./filetreediff";
11+
import * as userjsfile from "./userjsfile";
1112
import {
1213
domReady,
1314
IS_PRODUCTION,
@@ -26,6 +27,7 @@ export function setup() {
2627
hotkeys.HotKeysAddon,
2728
linkpreviews.LinkPreviewsAddon,
2829
filetreediff.FileTreeDiffAddon,
30+
userjsfile.UserJsFileAddon,
2931
];
3032

3133
return new Promise((resolve) => {

src/userjsfile.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { default as objectPath } from "object-path";
2+
import { AddonBase } from "./utils";
3+
4+
const SCRIPT_ID = "readthedocs-addons-user-js-file";
5+
6+
/**
7+
* User JavaScript file.
8+
*
9+
* Allow a user to inject a custom JavaScript file in all the pages.
10+
*/
11+
export class UserJsFileAddon extends AddonBase {
12+
static jsonValidationURI =
13+
"http://v1.schemas.readthedocs.org/addons.userjsfile.json";
14+
static addonEnabledPath = "addons.userjsfile.enabled";
15+
static addonName = "UserJsFile";
16+
static enabledOnHttpStatus = [200, 403, 404, 500];
17+
18+
constructor(config) {
19+
super();
20+
this.config = config;
21+
22+
if (objectPath.get(this.config, "addons.userjsfile.src")) {
23+
this.injectJavaScriptFile();
24+
}
25+
}
26+
27+
injectJavaScriptFile() {
28+
const script = document.createElement("script");
29+
script.id = SCRIPT_ID;
30+
script.src = objectPath.get(this.config, "addons.userjsfile.src");
31+
script.async = true;
32+
33+
document.body.appendChild(script);
34+
}
35+
}

tests/userjsfile.test.html

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<html>
2+
<body>
3+
<script type="module">
4+
import { expect, elementUpdated } from "@open-wc/testing";
5+
import { runTests } from "@web/test-runner-mocha";
6+
import * as userjsfile from "../src/userjsfile";
7+
8+
let config;
9+
10+
runTests(async () => {
11+
beforeEach(() => {
12+
13+
config = {
14+
addons: {
15+
userjsfile: {
16+
enabled: true,
17+
src: "https://myproject.readthedocs.io/en/latest/_static/readthedocs.js",
18+
},
19+
},
20+
};
21+
});
22+
23+
describe("UserJsFile addon", () => {
24+
it("script element is added", async () => {
25+
const addon = new userjsfile.UserJsFileAddon(config);
26+
const element = document.querySelector("#readthedocs-addons-user-js-file");
27+
28+
expect(element.src).to.equal("https://myproject.readthedocs.io/en/latest/_static/readthedocs.js");
29+
expect(element.async).to.equal(true);
30+
});
31+
32+
});
33+
});
34+
</script>
35+
</body>
36+
</html>

tests/userjsfile.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { expect, assert, fixture, html } from "@open-wc/testing";
2+
import { UserJsFileAddon } from "../src/userjsfile";
3+
4+
describe("UserJsFile addon", () => {
5+
it("invalid configuration disables the addon", () => {
6+
expect(
7+
UserJsFileAddon.isEnabled({
8+
addons: {
9+
userjsfile: {
10+
invalid: true,
11+
},
12+
},
13+
}),
14+
).to.be.false;
15+
});
16+
17+
it("is disabled", () => {
18+
expect(
19+
UserJsFileAddon.isEnabled({
20+
addons: {
21+
userjsfile: {
22+
enabled: false,
23+
src: "/en/latest/_static/readthedocs.js",
24+
},
25+
},
26+
}),
27+
).to.be.false;
28+
});
29+
30+
it("valid data and enabled", () => {
31+
expect(
32+
UserJsFileAddon.isEnabled({
33+
addons: {
34+
userjsfile: {
35+
enabled: true,
36+
src: "/en/latest/_static/readthedocs.js",
37+
},
38+
},
39+
}),
40+
).to.be.true;
41+
});
42+
});

0 commit comments

Comments
 (0)