Tweak full text search representation of documents

This commit is contained in:
Magnus Hoff 2018-06-13 23:13:38 +02:00
parent d577eabc9b
commit 28c8422e6b
4 changed files with 18 additions and 13 deletions

View file

@ -37,7 +37,7 @@ function debouncer(interval, callback) {
const query = input.value; const query = input.value;
fetch( fetch(
"_search?snippet_size=10&limit=4&q=" + encodeURIComponent(query), "_search?snippet_size=14&limit=4&q=" + encodeURIComponent(query),
{ {
headers: { headers: {
"Accept": "application/json", "Accept": "application/json",

View file

@ -4,6 +4,8 @@ use diesel::sql_types::*;
use r2d2::{CustomizeConnection, Pool}; use r2d2::{CustomizeConnection, Pool};
use r2d2_diesel::{self, ConnectionManager}; use r2d2_diesel::{self, ConnectionManager};
use rendering;
embed_migrations!(); embed_migrations!();
#[derive(Debug)] #[derive(Debug)]
@ -23,10 +25,7 @@ impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitial
sqlfunc::markdown_to_fts::register_impl( sqlfunc::markdown_to_fts::register_impl(
conn, conn,
|text: String| { |text: String| rendering::render_markdown_for_fts(&text)
use rendering;
rendering::render_markdown_for_fts(&text)
}
).map_err(|x| r2d2_diesel::Error::QueryError(x))?; ).map_err(|x| r2d2_diesel::Error::QueryError(x))?;
Ok(()) Ok(())

View file

@ -1,5 +1,5 @@
use pulldown_cmark::{Parser, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML}; use pulldown_cmark::{Parser, Tag, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML};
use pulldown_cmark::Event::Text; use pulldown_cmark::Event::{Text, End};
pub fn render_markdown(src: &str) -> String { pub fn render_markdown(src: &str) -> String {
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
@ -9,6 +9,10 @@ pub fn render_markdown(src: &str) -> String {
buf buf
} }
fn is_html_special(c: char) -> bool {
c == '&' || c == '<' || c == '>'
}
pub fn render_markdown_for_fts(src: &str) -> String { pub fn render_markdown_for_fts(src: &str) -> String {
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
let p = Parser::new_ext(src, opts); let p = Parser::new_ext(src, opts);
@ -16,14 +20,16 @@ pub fn render_markdown_for_fts(src: &str) -> String {
for event in p { for event in p {
match event { 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.push_str(" "),
} }
} }
buf.replace('&', "");
buf.replace('<', "");
buf.replace('>', "");
buf buf
} }

View file

@ -12,7 +12,7 @@ use state::State;
use web::{Resource, ResponseFuture}; use web::{Resource, ResponseFuture};
const DEFAULT_LIMIT: u32 = 10; const DEFAULT_LIMIT: u32 = 10;
const DEFAULT_SNIPPET_SIZE: u32 = 25; const DEFAULT_SNIPPET_SIZE: u32 = 30;
type BoxResource = Box<Resource + Sync + Send>; type BoxResource = Box<Resource + Sync + Send>;