diff --git a/src/components/learn.rs b/src/components/learn.rs index 2ee8c858c..0716d218d 100644 --- a/src/components/learn.rs +++ b/src/components/learn.rs @@ -16,6 +16,13 @@ pub struct DocsContentHighlighted(pub bool); pub static HIGHLIGHT_DOCS_CONTENT: Atom = Atom(|_| DocsContentHighlighted(false)); +/// The Markdown file path needs to be appended to this, including the first slash! +const GITHUB_API_URL: &str = "https://api.github.com/repos/DioxusLabs/docsite/contents/docs-src/0.4/en"; +/// Use this URL while loading the file-specific URL. +const GITHUB_EDIT_PAGE_FALLBACK_URL: &str = "https://github.com/DioxusLabs/docsite"; +/// The Markdown file path needs to be appended to this, including the first slash! +const GITHUB_EDIT_PAGE_EDIT_URL: &str = "https://github.com/DioxusLabs/docsite/edit/master/docs-src/0.4/en"; + #[inline_props] pub fn Learn(cx: Scope) -> Element { let show_sidebar_button = use_atom_state(cx, &SHOW_DOCS_NAV); @@ -182,6 +189,21 @@ fn RightNav(cx: Scope) -> Element { }; let page = use_book(cx); let padding_map = ["pl-2", "pl-4", "pl-6", "pl-8", "pl-10"]; + let page_url = page.to_string(); + + // This is the URL for the file if that file is not a directory that uses /index.md + // page_url starts with '/', so we don't need to worry about that + let github_api_url = format!("{GITHUB_API_URL}{page_url}.md"); + + let edit_github_url = use_future(cx, &page_url, |page_url| async move { + // If the file is not found, that means that we have to use /index.md + if reqwest::get(github_api_url).await.unwrap().status() == reqwest::StatusCode::NOT_FOUND { + format!("{GITHUB_EDIT_PAGE_EDIT_URL}{page_url}/index.md") + } else { + format!("{GITHUB_EDIT_PAGE_EDIT_URL}{page_url}.md") + } + }); + // That might be a naive approach, but it's the easiest render! { div { @@ -195,6 +217,12 @@ fn RightNav(cx: Scope) -> Element { } } } + h2 { class: "py-4 font-semibold", + match edit_github_url.value() { + Some(url) => rsx!(a { href: "{url}", "Edit this page!" }), + None => rsx!(a { href: "{GITHUB_EDIT_PAGE_FALLBACK_URL}", "Edit this page!" }) + } + } h2 { class: "py-4 font-semibold", "Go to version" } DocVersionNav {} } diff --git a/src/components/nav.rs b/src/components/nav.rs index 3f91bff61..b466cc2a0 100644 --- a/src/components/nav.rs +++ b/src/components/nav.rs @@ -248,12 +248,12 @@ fn SearchModal(cx: Scope) -> Element { async move { // debounce the search if *last_key_press.read() - js_sys::Date::now() > 100. { - results.set(SEARCH_INDEX.search(&*search_text.current())); + results.set(SEARCH_INDEX.search(&search_text.current())); last_key_press.set(js_sys::Date::now()); } else { gloo_timers::future::TimeoutFuture::new(100).await; - results.set(SEARCH_INDEX.search(&*search_text.current())); + results.set(SEARCH_INDEX.search(&search_text.current())); } } }); diff --git a/src/doc_examples/query_segments.rs b/src/doc_examples/query_segments.rs index 61055229c..6c50e45e6 100644 --- a/src/doc_examples/query_segments.rs +++ b/src/doc_examples/query_segments.rs @@ -62,4 +62,4 @@ fn App(cx: Scope) -> Element { render! { Router::{} } } -fn main() {} \ No newline at end of file +fn main() {} diff --git a/src/lib.rs b/src/lib.rs index 643bd583a..c4dc22af2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,7 +115,7 @@ pub enum Route { #[end_nest] #[end_layout] #[end_nest] - #[redirect("/docs/0.3/:..segments", |segments: Vec| Route::DocsO3 { segments: segments })] + #[redirect("/docs/0.3/:..segments", |segments: Vec| Route::DocsO3 { segments })] #[redirect("/docs/:.._segments", |_segments: Vec| Route::Docs { child: BookRoute::Index {} })] #[route("/:..segments")] #[route("/:..segments")]