From 28c8422e6bfa8761cdf4eee81de9a1141e5d42ff Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Wed, 13 Jun 2018 23:13:38 +0200 Subject: [PATCH] Tweak full text search representation of documents --- assets/search.js | 2 +- src/db.rs | 7 +++---- src/rendering.rs | 20 +++++++++++++------- src/resources/search_resource.rs | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/assets/search.js b/assets/search.js index f5d45ed..6ed8fa6 100644 --- a/assets/search.js +++ b/assets/search.js @@ -37,7 +37,7 @@ function debouncer(interval, callback) { const query = input.value; fetch( - "_search?snippet_size=10&limit=4&q=" + encodeURIComponent(query), + "_search?snippet_size=14&limit=4&q=" + encodeURIComponent(query), { headers: { "Accept": "application/json", diff --git a/src/db.rs b/src/db.rs index b8c73e1..e406de4 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,6 +4,8 @@ use diesel::sql_types::*; use r2d2::{CustomizeConnection, Pool}; use r2d2_diesel::{self, ConnectionManager}; +use rendering; + embed_migrations!(); #[derive(Debug)] @@ -23,10 +25,7 @@ impl CustomizeConnection for SqliteInitial sqlfunc::markdown_to_fts::register_impl( conn, - |text: String| { - use rendering; - rendering::render_markdown_for_fts(&text) - } + |text: String| rendering::render_markdown_for_fts(&text) ).map_err(|x| r2d2_diesel::Error::QueryError(x))?; Ok(()) diff --git a/src/rendering.rs b/src/rendering.rs index dca1ad8..f26d31f 100644 --- a/src/rendering.rs +++ b/src/rendering.rs @@ -1,5 +1,5 @@ -use pulldown_cmark::{Parser, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML}; -use pulldown_cmark::Event::Text; +use pulldown_cmark::{Parser, Tag, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML}; +use pulldown_cmark::Event::{Text, End}; pub fn render_markdown(src: &str) -> String { let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; @@ -9,6 +9,10 @@ pub fn render_markdown(src: &str) -> String { buf } +fn is_html_special(c: char) -> bool { + c == '&' || c == '<' || c == '>' +} + pub fn render_markdown_for_fts(src: &str) -> String { let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; let p = Parser::new_ext(src, opts); @@ -16,14 +20,16 @@ pub fn render_markdown_for_fts(src: &str) -> String { for event in p { match event { - Text(text) => buf.push_str(&text), + Text(text) => + buf.push_str(&text.replace(is_html_special, " ")), + End(Tag::Link(uri, _title)) => { + buf.push_str(" ("); + buf.push_str(&uri.replace(is_html_special, " ")); + buf.push_str(") "); + } _ => buf.push_str(" "), } } - buf.replace('&', ""); - buf.replace('<', ""); - buf.replace('>', ""); - buf } diff --git a/src/resources/search_resource.rs b/src/resources/search_resource.rs index e0ea905..32bdcc7 100644 --- a/src/resources/search_resource.rs +++ b/src/resources/search_resource.rs @@ -12,7 +12,7 @@ use state::State; use web::{Resource, ResponseFuture}; const DEFAULT_LIMIT: u32 = 10; -const DEFAULT_SNIPPET_SIZE: u32 = 25; +const DEFAULT_SNIPPET_SIZE: u32 = 30; type BoxResource = Box;