Skip to content

Commit 8010104

Browse files
authored
Resolver filename fixes (#532)
Make flyout work when `readthedocs.resolver.filename` is `null`. In that case, we get this value from the META tag added by Cloudflare. This was the original intent here, but I missed allowing `null` in the data validation. Besides, even if the data validation allows `null`, there was another bug since I was re-defining the variable (`const ...`) instead of overriding its value. We didn't catch this locally because we have a different rule that makes the request to always contain `url=` parameter and that case is not affected by this. Besides, I didn't find this when doing QA after deploy because I tested with examples that sent `url=` parameter. I'll see if I can write a test for this in another PR.
1 parent 8b084b3 commit 8010104

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

src/data-validation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ const addons_flyout = {
250250
type: "object",
251251
required: ["filename"],
252252
properties: {
253-
filename: { type: "string" },
253+
filename: { type: ["string", "null"] },
254254
},
255255
},
256256
},
@@ -463,7 +463,7 @@ const addons_notifications = {
463463
type: "object",
464464
required: ["filename"],
465465
properties: {
466-
filename: { type: "string" },
466+
filename: { type: ["string", "null"] },
467467
},
468468
},
469469
},

src/utils.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ export function getLinkWithFilename(url, resolverFilename) {
360360
// Normal case for most of the documentation tools.
361361
// Get the resolver's filename returned by the application (as HTTP header)
362362
// and injected by Cloudflare Worker as a meta HTML tag
363-
const resolverFilename = getMetadataValue(
364-
"readthedocs-resolver-filename",
365-
);
363+
resolverFilename = getMetadataValue("readthedocs-resolver-filename");
366364
}
367365
}
368366

tests/flyout.test.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
import * as flyout from "../src/flyout";
1111

1212
let config;
13+
let metaResolverFilename;
1314

1415
runTests(async () => {
1516
beforeEach(() => {
17+
metaResolverFilename = document.querySelector(
18+
"meta[name='readthedocs-resolver-filename']",
19+
);
20+
metaResolverFilename.content = "/";
21+
1622
config = {
1723
addons: {
1824
search: {
@@ -312,6 +318,42 @@
312318
);
313319
expect(searchForm).to.not.exist;
314320
});
321+
322+
it("flyout with readthedocs.resolver.filename being null", async () => {
323+
metaResolverFilename.content = "/guides/tests.html";
324+
325+
// Disable the search from the config object
326+
config.readthedocs.resolver.filename = null;
327+
328+
const addon = new flyout.FlyoutAddon(config);
329+
const element = document.querySelector("readthedocs-flyout");
330+
await elementUpdated(element);
331+
const versionLink = element.shadowRoot.querySelector(
332+
"main > dl.versions > dd > a",
333+
);
334+
expect(versionLink).dom.to.equal(
335+
'<a href="https://project.readthedocs.io/en/stable/guides/tests.html">stable</a>',
336+
);
337+
});
338+
339+
it("flyout with readthedocs.resolver.filename and different meta.content", async () => {
340+
metaResolverFilename.content = "/guides/tests.html";
341+
342+
// Add a different value for the filename than the meta tag.
343+
// This happens on SPA documentation tools
344+
config.readthedocs.resolver.filename =
345+
"/examples/single-page-application.html";
346+
347+
const addon = new flyout.FlyoutAddon(config);
348+
const element = document.querySelector("readthedocs-flyout");
349+
await elementUpdated(element);
350+
const versionLink = element.shadowRoot.querySelector(
351+
"main > dl.versions > dd > a",
352+
);
353+
expect(versionLink).dom.to.equal(
354+
'<a href="https://project.readthedocs.io/en/stable/examples/single-page-application.html">stable</a>',
355+
);
356+
});
315357
});
316358
});
317359
</script>

tests/utils.test.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
const tool = new utils.DocumentationTool();
151151

152152
expect(tool.getDocumentationTool()).to.be.equal("mkdocs-material");
153+
expect(tool.isSinglePageApplication()).to.be.false;
153154
expect(tool.isSphinx()).to.be.false;
154155
expect(tool.isSphinxAlabasterLikeTheme()).to.be.false;
155156
expect(tool.isPelican()).to.be.false;
@@ -177,13 +178,36 @@
177178
const tool = new utils.DocumentationTool();
178179

179180
expect(tool.getDocumentationTool()).to.be.equal("docusaurus");
181+
expect(tool.isSinglePageApplication()).to.be.true;
180182
expect(tool.isJekyll()).to.be.false;
181183
expect(tool.isDocusaurus()).to.be.true;
182184
expect(tool.getRootSelector()).to.be.equal("article div.markdown");
183185
expect(tool.getLinkSelector()).to.be.equal(
184186
"article div.markdown p a",
185187
);
186188
});
189+
190+
it("VitePress", async () => {
191+
element = document.createElement("meta");
192+
element.name = "generator";
193+
element.content = "VitePress generator 0.1.2";
194+
document.head.appendChild(element);
195+
196+
const content = `<article id="content"><p>This is a <a href="#">link</a></p></article>`;
197+
document.body.insertAdjacentHTML("beforeend", content);
198+
199+
// We can't rely on the DocumentationTool exported since the
200+
// constructur is triggered before we add the required element to
201+
// the DOM.
202+
const tool = new utils.DocumentationTool();
203+
204+
expect(tool.getDocumentationTool()).to.be.equal("vitepress");
205+
expect(tool.isSinglePageApplication()).to.be.true;
206+
expect(tool.isJekyll()).to.be.false;
207+
expect(tool.isDocusaurus()).to.be.false;
208+
expect(tool.getRootSelector()).to.be.equal("article");
209+
expect(tool.getLinkSelector()).to.be.equal("article p a");
210+
});
187211
});
188212
});
189213
</script>

0 commit comments

Comments
 (0)