From f7755363eefcb13fd7898add42ea2d491dfb3a18 Mon Sep 17 00:00:00 2001 From: Folyd Date: Sun, 20 Dec 2020 00:27:14 +0800 Subject: [PATCH] Link source page's issue and since label --- extension/script/doc-rust-lang-org.css | 3 ++ extension/script/doc-rust-lang-org.js | 54 +++++++++++++++++++------- manifest.jsonnet | 8 +--- 3 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 extension/script/doc-rust-lang-org.css diff --git a/extension/script/doc-rust-lang-org.css b/extension/script/doc-rust-lang-org.css new file mode 100644 index 0000000..345ebf3 --- /dev/null +++ b/extension/script/doc-rust-lang-org.css @@ -0,0 +1,3 @@ +.rse-link:hover { + text-decoration: underline; +} \ No newline at end of file diff --git a/extension/script/doc-rust-lang-org.js b/extension/script/doc-rust-lang-org.js index c952c6d..01fe42f 100644 --- a/extension/script/doc-rust-lang-org.js +++ b/extension/script/doc-rust-lang-org.js @@ -1,21 +1,23 @@ +const RUST_RELEASE_README_URL = "https://github.com/rust-lang/rust/blob/master/RELEASES.md"; let target = location.pathname.includes("/nightly/") ? "nightly" : "stable"; document.addEventListener("DOMContentLoaded", () => { - for (let span of document.querySelectorAll("span.since")) { - let version = span.textContent; - span.innerHTML = ` - ${version} - `; - } + if (["/src/", "/stable/src/", "/nightly/src/"].some(p => location.pathname.startsWith(p))) { + // Source code pages + linkSourcePageUrls(); + } else { + // Docs page + linkDocPageSinceUrls(); - let version = localStorage.getItem(`rust-search-extension:${target}`); - let now = new Date(); - let today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0) - if (version && today <= Date.parse(version)) { - // Check version between localStorage and today to ensure update search index once a day. - return; + let version = localStorage.getItem(`rust-search-extension:${target}`); + let now = new Date(); + let today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0) + if (version && today <= Date.parse(version)) { + // Check version between localStorage and today to ensure update search index once a day. + return; + } + injectScripts(["script/add-std-search-index.js"]); } - injectScripts(["script/add-std-search-index.js"]); }); window.addEventListener("message", function (event) { @@ -31,4 +33,28 @@ window.addEventListener("message", function (event) { } ); } -}); \ No newline at end of file +}); + +function linkDocPageSinceUrls() { + for (let span of document.querySelectorAll("span.since")) { + let version = span.textContent; + let href = `${RUST_RELEASE_README_URL}?version=${version}`; + span.innerHTML = `${version}`; + } +} + +// Link issue and since urls in source pages. +function linkSourcePageUrls() { + for (let span of document.querySelectorAll("span.attribute>span.string")) { + let text = span.textContent; + if (/^"[0-9]*"$/g.test(text)) { + // #[unstable(feature = "xxx", issue = "62358")] + let href = `https://github.com/rust-lang/rust/issues/${text.replace('"', '').trim()}`; + span.innerHTML = `${text}`; + } else if (/^"\d\.\d+\.?\d?"$/g.test(text)) { + // #[stable(feature = "xxx", since = "1.23.0")] + let href = `${RUST_RELEASE_README_URL}?version=${text.replace('"', '').trim()}`; + span.innerHTML = `${text}`; + } + } +} \ No newline at end of file diff --git a/manifest.jsonnet b/manifest.jsonnet index 1966d6c..2aba6e0 100644 --- a/manifest.jsonnet +++ b/manifest.jsonnet @@ -27,13 +27,9 @@ local json = manifest.new( css=['script/docs-rs.css'], ) .addContentScript( - matches=['*://doc.rust-lang.org/nightly/std/*'], + matches=['*://doc.rust-lang.org/*'], js=utils.js_files('script', ['lib', 'doc-rust-lang-org']), - css=[], -).addContentScript( - matches=['*://doc.rust-lang.org/std/*', '*://doc.rust-lang.org/stable/std/*'], - js=utils.js_files('script', ['lib', 'doc-rust-lang-org']), - css=[], + css=['script/doc-rust-lang-org.css'], ).addContentScript( matches=['*://rust.extension.sh/update', '*://extension.sh/update/'], js=utils.js_files('script', ['rust-extension-sh']),