Skip to content

Commit

Permalink
Merge pull request #124 from tigerros/docsite#120
Browse files Browse the repository at this point in the history
Create an "edit page" button
  • Loading branch information
ealmloff authored Sep 20, 2023
2 parents c05fa5c + 80174fa commit 9299d94
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/components/learn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub struct DocsContentHighlighted(pub bool);
pub static HIGHLIGHT_DOCS_CONTENT: Atom<DocsContentHighlighted> =
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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/doc_examples/query_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ fn App(cx: Scope) -> Element {
render! { Router::<Route>{} }
}

fn main() {}
fn main() {}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub enum Route {
#[end_nest]
#[end_layout]
#[end_nest]
#[redirect("/docs/0.3/:..segments", |segments: Vec<String>| Route::DocsO3 { segments: segments })]
#[redirect("/docs/0.3/:..segments", |segments: Vec<String>| Route::DocsO3 { segments })]
#[redirect("/docs/:.._segments", |_segments: Vec<String>| Route::Docs { child: BookRoute::Index {} })]
#[route("/:..segments")]
#[route("/:..segments")]
Expand Down

0 comments on commit 9299d94

Please sign in to comment.