Skip to content

Commit 7483ba3

Browse files
committed
Fix #135, docs.rs latest version path compatibility
1 parent 543f813 commit 7483ba3

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

extension/script/add-search-index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
if (location.hostname === "docs.rs") { // docs.rs pages
44
// Parse crate info from location pathname.
55
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
6+
// Since this PR (https://github.com/rust-lang/docs.rs/pull/1527) merged,
7+
// the latest version path has changed:
8+
// from https://docs.rs/tokio/1.14.0/tokio/ to https://docs.rs/tokio/latest/tokio/
9+
//
10+
// If we parse the crate version from url is 'latest',
11+
// we should reparse it from the DOM to get the correct value.
12+
if (crateVersion === 'latest') {
13+
let versionText = document.querySelector('nav.sidebar > div.block.version > p').textContent;
14+
crateVersion = versionText.split(' ')[1];
15+
}
616
window.postMessage({
717
direction: "rust-search-extension",
818
message: {
@@ -11,8 +21,8 @@
1121
searchIndex: window.searchIndex,
1222
},
1323
}, "*");
14-
} else if (location.pathname.startsWith("/nightly/nightly-rustc/")
15-
&& location.hostname === "doc.rust-lang.org") { // rustc pages
24+
} else if (location.pathname.startsWith("/nightly/nightly-rustc/") &&
25+
location.hostname === "doc.rust-lang.org") { // rustc pages
1626
window.postMessage({
1727
direction: 'rust-search-extension:rustc',
1828
message: {

extension/script/docs-rs.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function highlight() {
2626
if (link) {
2727
let target = link.parentElement;
2828
target.classList.add("rse-active");
29-
target.scrollIntoView({behavior: "auto", block: "nearest"});
29+
target.scrollIntoView({ behavior: "auto", block: "nearest" });
3030
}
3131
}
3232
});
@@ -64,6 +64,7 @@ document.addEventListener("DOMContentLoaded", () => {
6464
highlight();
6565
});
6666

67+
// Using separate event listener to avoid network requesting latency for feature flags menu enhancement.
6768
document.addEventListener("DOMContentLoaded", async () => {
6869
let menus = document.querySelector("form>.pure-menu-list:not(.pure-menu-right)");
6970
if (!menus) return;
@@ -77,14 +78,24 @@ document.addEventListener("DOMContentLoaded", async () => {
7778
}
7879
});
7980

80-
// Using separate event listener to avoid network requesting latency for feature flags menu enhancement.
8181
document.addEventListener("DOMContentLoaded", async () => {
8282
let menus = document.querySelector("form>.pure-menu-list:not(.pure-menu-right)");
8383
if (!menus) return;
8484

85+
// Since this PR (https://github.com/rust-lang/docs.rs/pull/1527) merged,
86+
// the latest version path has changed:
87+
// from https://docs.rs/tokio/1.14.0/tokio/ to https://docs.rs/tokio/latest/tokio/
88+
//
89+
// If we parse the crate version from url is 'latest',
90+
// we should reparse it from the DOM to get the correct value.
91+
if (crateVersion === 'latest') {
92+
let versionText = document.querySelector('nav.sidebar > div.block.version > p').textContent;
93+
crateVersion = versionText.split(' ')[1];
94+
}
95+
8596
// Exclude /crate/** pages
8697
if (menus.children.length >= 3 && !location.pathname.includes("/crate/")) {
87-
chrome.runtime.sendMessage({crateName, action: "crate:check"}, crate => {
98+
chrome.runtime.sendMessage({ crateName, action: "crate:check" }, crate => {
8899
if (crate) {
89100
insertAddToExtensionElement(getState(crate.version));
90101
} else {
@@ -153,7 +164,7 @@ function insertAddToExtensionElement(state) {
153164
li.onclick = () => {
154165
// Toggle search index added state
155166
if (state === "latest") {
156-
chrome.runtime.sendMessage({crateName, action: "crate:remove"}, response => {
167+
chrome.runtime.sendMessage({ crateName, action: "crate:remove" }, response => {
157168
insertAddToExtensionElement(getState(undefined));
158169
});
159170
} else {
@@ -199,7 +210,7 @@ function insertAddToExtensionElement(state) {
199210
if (menu.querySelector("#rse-here")) {
200211
menu.querySelector("#rse-here").onclick = () => {
201212
let url = chrome.runtime.getURL("manage/crates.html");
202-
chrome.runtime.sendMessage({action: "open-url", url});
213+
chrome.runtime.sendMessage({ action: "open-url", url });
203214
};
204215
}
205216
}
@@ -208,7 +219,7 @@ window.addEventListener("message", function (event) {
208219
if (event.source === window &&
209220
event.data &&
210221
event.data.direction === "rust-search-extension") {
211-
chrome.runtime.sendMessage({action: "crate:add", ...event.data.message},
222+
chrome.runtime.sendMessage({ action: "crate:add", ...event.data.message },
212223
(response) => {
213224
if (response) {
214225
insertAddToExtensionElement(getState(event.data.message.crateVersion));

0 commit comments

Comments
 (0)