Add handlers for smart-case, spaced node queries
This commit is contained in:
parent
435cf4d18f
commit
be6578bee2
5 changed files with 53 additions and 22 deletions
|
|
@ -1,4 +1,3 @@
|
|||
use crate::prelude::*;
|
||||
use axum::response::IntoResponse as _;
|
||||
use axum::{body::Body, extract::Path, http::Response, response::Redirect};
|
||||
|
||||
|
|
@ -8,9 +7,12 @@ use crate::{syntax::serial::populate_graph, router::handlers, types::Node};
|
|||
|
||||
pub async fn node(Path(id): Path<String>) -> Response<Body> {
|
||||
let graph = populate_graph();
|
||||
let empty_node = Node::new(Some(format!("Could not find node ID {id}.")));
|
||||
let (node_match, exact) = graph.find_node(&id);
|
||||
let node = node_match.unwrap_or(empty_node.clone());
|
||||
let result = graph.find_node(&id);
|
||||
let nodes: Vec<Node> = graph.nodes.into_values().collect();
|
||||
let not_found = result.node.is_none();
|
||||
let node = result
|
||||
.node
|
||||
.unwrap_or(Node::new(Some(format!("Could not find node ID {id}."))));
|
||||
|
||||
if !node.redirect.is_empty() {
|
||||
return Redirect::permanent(
|
||||
|
|
@ -19,20 +21,18 @@ pub async fn node(Path(id): Path<String>) -> Response<Body> {
|
|||
.into_response();
|
||||
}
|
||||
|
||||
if !exact {
|
||||
log!("Redirecting non-exact match to {}", node.id);
|
||||
if result.redirect {
|
||||
return Redirect::permanent(format!("/node/{}", node.id).as_str())
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let mut context = tera::Context::default();
|
||||
context.insert("node", &node);
|
||||
context.insert("nodes", &nodes);
|
||||
context.insert("text", &content::parse(&node.text, &graph.meta.config));
|
||||
context.insert("incoming", &graph.incoming.get(&id));
|
||||
context.insert("config", &graph.meta.config);
|
||||
|
||||
let not_found = node == empty_node;
|
||||
|
||||
handlers::template::by_filename(
|
||||
"node.html",
|
||||
&context,
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ pub async fn search(Form(query): Form<Query>) -> Redirect {
|
|||
Redirect::permanent(format!("/node/{}", query.node).as_str())
|
||||
}
|
||||
|
||||
pub async fn redirect(Form(query): Form<Query>) -> Redirect {
|
||||
Redirect::permanent(format!("/node/{}", query.node).as_str())
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Query {
|
||||
node: String,
|
||||
|
|
@ -63,4 +67,13 @@ mod tests {
|
|||
let response = page("HBvcwqT8wLk6hxk1GdvNcEzJ6IiZ2Fod").await;
|
||||
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn id_redirect() {
|
||||
let query = Form(Query {
|
||||
node: String::from("ancHOr syntaX"),
|
||||
});
|
||||
let response = search(query).await;
|
||||
assert!(response.status_code() == StatusCode::PERMANENT_REDIRECT);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue