Skip to content

web search done right

Move snippet highlighting into svelte. This also removes the need to trust that the api has correctly sanitized the snippet from svelte.

Mikkel Denkerc02924d05ccb
diff --git a/Cargo.lock b/Cargo.lock
index 9c325c8a..ee98e4b3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1786,15 +1786,6 @@ dependencies = [
"digest",
]
-[[package]]
-name = "html-escape"
-version = "0.2.13"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6d1ad449764d627e22bfd7cd5e8868264fc9236e07c752972b4080cd351cb476"
-dependencies = [
- "utf8-width",
-]
-
[[package]]
name = "html5ever"
version = "0.26.0"
@@ -4199,7 +4190,6 @@ dependencies = [
"futures",
"half 2.3.1",
"hashbrown 0.14.0",
- "html-escape",
"http",
"image",
"indicatif 0.16.2",
@@ -5072,12 +5062,6 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcfc827f90e53a02eaef5e535ee14266c1d569214c6aa70133a624d8a3164ba"
-[[package]]
-name = "utf8-width"
-version = "0.1.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
-
[[package]]
name = "utf8parse"
version = "0.2.1"
diff --git a/Cargo.toml b/Cargo.toml
index d4f7af65..b2be43de 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,13 +28,12 @@ tantivy = "0.19.2"
thiserror = "1.0.31"
toml = "0.5.9"
tracing = "0.1.34"
-tracing-subscriber = { version = "0.3.11", features= ["env-filter"] }
+tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
whatlang = "0.16.0"
tokio = { version = "1.23.1", features = ["full"] }
axum = "0.6.9"
axum-extra = { version = "0.6.0", features = ["spa"] }
axum-macros = "0.3.4"
-html-escape = "0.2.11"
logos = "0.12.1"
csv = "1.1.6"
fst = { version = "0.4.7", features = ["levenshtein"] }
@@ -102,4 +101,4 @@ rkyv = { version = "0.7.42", default_features = false, features = [
"validation",
"size_64",
] }
-mime = "0.3.17"
\ No newline at end of file
+mime = "0.3.17"
diff --git a/core/Cargo.toml b/core/Cargo.toml
index c953a5be..4587f2c7 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -39,7 +39,6 @@ whatlang = { workspace = true }
tokio = { workspace = true }
axum = { workspace = true }
axum-extra = { workspace = true }
-html-escape = { workspace = true }
logos = { workspace = true }
csv = { workspace = true }
fst = { workspace = true }
diff --git a/core/src/api/docs.rs b/core/src/api/docs.rs
index a172689d..d5c2cb9f 100644
--- a/core/src/api/docs.rs
+++ b/core/src/api/docs.rs
@@ -49,6 +49,10 @@ use utoipa_swagger_ui::SwaggerUi;
crate::search_prettifier::StackOverflowQuestion,
crate::search_prettifier::CodeOrText,
+ crate::snippet::TextSnippet,
+ crate::snippet::TextSnippetFragmentKind,
+ crate::snippet::TextSnippetFragment,
+
crate::bangs::UrlWrapper,
crate::widgets::Widget,
diff --git a/core/src/inverted_index.rs b/core/src/inverted_index.rs
index 959f1ce5..8fc76aec 100644
--- a/core/src/inverted_index.rs
+++ b/core/src/inverted_index.rs
@@ -44,6 +44,7 @@ use crate::ranking::pipeline::RankingWebsite;
use crate::ranking::SignalAggregator;
use crate::schema::{FastField, Field, TextField, ALL_FIELDS};
use crate::search_ctx::Ctx;
+use crate::snippet::TextSnippet;
use crate::tokenizer::{BigramTokenizer, Identity, TrigramTokenizer};
use crate::webgraph::NodeID;
use crate::webpage::region::Region;
@@ -519,7 +520,7 @@ pub struct RetrievedWebpage {
pub title: String,
pub url: String,
pub body: String,
- pub snippet: String,
+ pub snippet: TextSnippet,
pub dirty_body: String,
pub description: Option<String>,
pub dmoz_description: Option<String>,
diff --git a/core/src/search_prettifier/mod.rs b/core/src/search_prettifier/mod.rs
index 5c289952..9f25e4f0 100644
--- a/core/src/search_prettifier/mod.rs
+++ b/core/src/search_prettifier/mod.rs
@@ -27,6 +27,7 @@ use utoipa::ToSchema;
use crate::{
inverted_index::RetrievedWebpage,
ranking::{Signal, SignalScore},
+ snippet::TextSnippet,
spell::{self, CorrectionTerm},
webpage::url_ext::UrlExt,
};
@@ -41,7 +42,7 @@ pub use self::stack_overflow::{stackoverflow_snippet, StackOverflowAnswer, Stack
pub enum Snippet {
Normal {
date: Option<String>,
- text: String,
+ text: TextSnippet,
},
StackOverflowQA {
question: StackOverflowQuestion,
@@ -85,13 +86,6 @@ impl From<spell::Correction> for HighlightedSpellCorrection {
}
}
-pub fn html_escape(s: &str) -> String {
- html_escape::decode_html_entities(s)
- .chars()
- .filter(|c| !matches!(c, '<' | '>' | '&'))
- .collect()
-}
-
fn prettify_url(url: &Url) -> String {
let mut pretty_url = url.clone();
pretty_url.set_query(None);
@@ -191,10 +185,8 @@ impl From<RetrievedWebpage> for DisplayedWebpage {
let domain = url.root_domain().unwrap_or_default().to_string();
let pretty_url = prettify_url(&url);
- let title = html_escape(&webpage.title);
-
Self {
- title,
+ title: webpage.title,
site: url.host_str().unwrap_or_default().to_string(),
url: webpage.url,
pretty_url,
diff --git a/core/src/snippet.rs b/core/src/snippet.rs
index 8a1dcf3c..0e0fa677 100644
--- a/core/src/snippet.rs
+++ b/core/src/snippet.rs
@@ -14,14 +14,15 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+use std::ops::Range;
+
use crate::config::SnippetConfig;
use crate::query::Query;
-use crate::search_prettifier::html_escape;
use crate::spell::sentence_ranges;
use crate::tokenizer::{BigramTokenizer, Stemmed, Tokenizer, TrigramTokenizer};
use crate::webpage::region::Region;
use hashbrown::{HashMap, HashSet};
-use std::ops::Range;
+use utoipa::ToSchema;
use itertools::Itertools;
use whatlang::Lang;
@@ -45,37 +46,32 @@ struct PassageCandidate {
doc_terms: HashMap<String, u64>,
}
-#[derive(Debug)]
-struct SnippetString {
- fragment: String,
- highlighted: Vec<Range<usize>>,
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, ToSchema)]
+#[serde(rename_all = "camelCase")]
+pub enum TextSnippetFragmentKind {
+ Normal,
+ Highlighted,
}
-const HIGHLIGHTEN_PREFIX: &str = "<b>";
-const HIGHLIGHTEN_POSTFIX: &str = "</b>";
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, ToSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct TextSnippetFragment {
+ kind: TextSnippetFragmentKind,
+ text: String,
+}
-impl SnippetString {
- fn to_html(&self) -> String {
- let mut html = String::new();
- let mut start_from: usize = 0;
+#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, ToSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct TextSnippet {
+ fragments: Vec<TextSnippetFragment>,
+}
- for item in self.highlighted.iter().filter(|item| item.start < item.end) {
- if item.start < start_from {
- start_from = item.end;
- continue;
- }
- html.push_str(&html_escape(&self.fragment[start_from..item.start]));
- html.push_str(HIGHLIGHTEN_PREFIX);
- html.push_str(&html_escape(&self.fragment[item.clone()]));
- html.push_str(HIGHLIGHTEN_POSTFIX);
- start_from = item.end;
- }
- html.push_str(&html_escape(
- &self.fragment[start_from..self.fragment.len()],
- ));
- html
- }
+struct SnippetBuilder {
+ fragment: String,
+ highlights: Vec<Range<usize>>,
+}
+impl SnippetBuilder {
fn highlight(&mut self, terms: &HashSet<String>, lang: whatlang::Lang) {
for tokenizer in [
Tokenizer::Stemmed(Stemmed::with_forced_language(lang)),
@@ -86,24 +82,55 @@ impl SnippetString {
tantivy::tokenizer::Tokenizer::token_stream(&tokenizer, &self.fragment);
while let Some(tok) = stream.next() {
if terms.contains(&tok.text) {
- self.highlighted.push(tok.offset_from..tok.offset_to);
+ self.highlights.push(tok.offset_from..tok.offset_to);
}
}
}
// remove overlapping ranges
- self.highlighted.sort_by(|a, b| a.start.cmp(&b.start));
- self.highlighted
+ self.highlights.sort_by(|a, b| a.start.cmp(&b.start));
+ self.highlights
.dedup_by(|a, b| a.start == b.start && a.end > b.end);
}
+
+ fn build(self) -> TextSnippet {
+ let mut fragments = Vec::new();
+
+ let mut last_end = 0;
+
+ for range in self.highlights {
+ if range.start > last_end {
+ fragments.push(TextSnippetFragment {
+ kind: TextSnippetFragmentKind::Normal,
+ text: self.fragment[last_end..range.start].to_string(),
+ });
+ }
+
+ fragments.push(TextSnippetFragment {
+ kind: TextSnippetFragmentKind::Highlighted,
+ text: self.fragment[range.start..range.end].to_string(),
+ });
+
+ last_end = range.end;
+ }
+
+ if last_end < self.fragment.len() {
+ fragments.push(TextSnippetFragment {
+ kind: TextSnippetFragmentKind::Normal,
+ text: self.fragment[last_end..].to_string(),
+ });
+ }
+
+ TextSnippet { fragments }
+ }
}
-fn snippet_string(
+fn snippet_string_builder(
text: &str,
terms: &[String],
lang: whatlang::Lang,
config: SnippetConfig,
-) -> SnippetString {
+) -> SnippetBuilder {
let tokenizer = Tokenizer::Stemmed(Stemmed::with_forced_language(lang));
let terms: HashSet<String> = terms
@@ -144,9 +171,9 @@ fn snippet_string(
.collect();
if passages.is_empty() {
- let mut snippet = SnippetString {
+ let mut snippet = SnippetBuilder {
fragment: text.chars().take(config.desired_num_chars).collect(),
- highlighted: Vec::new(),
+ highlights: Vec::new(),
};
snippet.highlight(&terms, lang);
@@ -199,9 +226,9 @@ fn snippet_string(
.expect("passages cannot be empty at this point");
let best_passage = &passages[best_idx];
- let mut snippet = SnippetString {
+ let mut snippet = SnippetBuilder {
fragment: best_passage.text.clone(),
- highlighted: Vec::new(),
+ highlights: Vec::new(),
};
if snippet.fragment.len() > config.desired_num_chars + config.delta_num_chars {
@@ -236,7 +263,16 @@ fn snippet_string(
snippet
}
-pub fn generate(query: &Query, text: &str, region: &Region, config: SnippetConfig) -> String {
+fn snippet_string(
+ text: &str,
+ terms: &[String],
+ lang: whatlang::Lang,
+ config: SnippetConfig,
+) -> TextSnippet {
+ snippet_string_builder(text, terms, lang, config).build()
+}
+
+pub fn generate(query: &Query, text: &str, region: &Region, config: SnippetConfig) -> TextSnippet {
let lang = match region.lang() {
Some(lang) => lang,
None => match config.num_words_for_lang_detection {
@@ -252,22 +288,20 @@ pub fn generate(query: &Query, text: &str, region: &Region, config: SnippetConfi
};
if text.is_empty() {
- return text.to_string();
+ return TextSnippet {
+ fragments: vec![TextSnippetFragment {
+ kind: TextSnippetFragmentKind::Normal,
+ text: "".to_string(),
+ }],
+ };
}
match config.max_considered_words {
Some(num_words) => {
let text = text.split_whitespace().take(num_words).join(" ");
-
- let snippet = snippet_string(&text, query.simple_terms(), lang, config);
-
- snippet.to_html()
- }
- None => {
- let snippet = snippet_string(text, query.simple_terms(), lang, config);
-
- snippet.to_html()
+ snippet_string(&text, query.simple_terms(), lang, config)
}
+ None => snippet_string(text, query.simple_terms(), lang, config),
}
}
@@ -293,13 +327,30 @@ to the project are from community members.[15]
Rust won first place for "most loved programming language" in the Stack Overflow Developer
Survey in 2016, 2017, and 2018."#;
- fn snippet_text(snippet: Snippet) -> String {
+ fn text_snippet(snippet: Snippet) -> TextSnippet {
match snippet {
Snippet::Normal { date: _, text } => text,
_ => panic!("The snippet was not text"),
}
}
+ const HIGHLIGHTEN_PREFIX: &str = "<b>";
+ const HIGHLIGHTEN_POSTFIX: &str = "</b>";
+
+ fn highlight(snippet: Snippet) -> String {
+ let text = text_snippet(snippet);
+
+ text.fragments
+ .into_iter()
+ .map(|TextSnippetFragment { kind, text }| match kind {
+ TextSnippetFragmentKind::Normal => text,
+ TextSnippetFragmentKind::Highlighted => {
+ format!("{HIGHLIGHTEN_PREFIX}{}{HIGHLIGHTEN_POSTFIX}", text)
+ }
+ })
+ .collect()
+ }
+
#[test]
fn snippet_during_search() {
let mut index = Index::temporary().expect("Unable to open index");
@@ -337,7 +388,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(result.num_hits, 1);
assert_eq!(result.webpages.len(), 1);
- assert_eq!(snippet_text(result.webpages[0].snippet.clone()), format!("{HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is a systems programming {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX} sponsored by Mozilla which describes it as a \"safe, concurrent, practical {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX}\", supporting functional and imperative-procedural paradigms. {HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is syntactically similar to C++[according to whom?"));
+ assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("{HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is a systems programming {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX} sponsored by Mozilla which describes it as a \"safe, concurrent, practical {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX}\", supporting functional and imperative-procedural paradigms. {HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is syntactically similar to C++[according to whom?"));
}
#[test]
@@ -377,7 +428,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(result.num_hits, 1);
assert_eq!(result.webpages.len(), 1);
- assert_eq!(snippet_text(result.webpages[0].snippet.clone()), format!("Rust is a systems programming language sponsored by Mozilla which {HIGHLIGHTEN_PREFIX}describes{HIGHLIGHTEN_POSTFIX} it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++[according to whom?"));
+ assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("Rust is a systems programming language sponsored by Mozilla which {HIGHLIGHTEN_PREFIX}describes{HIGHLIGHTEN_POSTFIX} it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++[according to whom?"));
}
#[test]
@@ -418,7 +469,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(result.num_hits, 1);
assert_eq!(result.webpages.len(), 1);
assert_eq!(
- snippet_text(result.webpages[0].snippet.clone()),
+ highlight(result.webpages[0].snippet.clone()),
format!("Rust is a systems programming language sponsored by Mozilla which describes it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural {HIGHLIGHTEN_PREFIX}paradigms{HIGHLIGHTEN_POSTFIX}. Rust is syntactically similar to C++[according to whom?")
);
}
@@ -426,13 +477,15 @@ Survey in 2016, 2017, and 2018."#;
#[test]
fn empty_query() {
assert_eq!(
- snippet_string(
- "this is a test",
- &[],
- whatlang::Lang::Eng,
- SnippetConfig::default()
- )
- .fragment
+ highlight(Snippet::Normal {
+ date: None,
+ text: snippet_string(
+ "this is a test",
+ &[],
+ whatlang::Lang::Eng,
+ SnippetConfig::default()
+ )
+ })
.as_str(),
"this is a test"
);
@@ -441,28 +494,32 @@ Survey in 2016, 2017, and 2018."#;
#[test]
fn empty_text() {
assert_eq!(
- snippet_string(
- "",
- &["test".to_string()],
- whatlang::Lang::Eng,
- SnippetConfig::default()
- )
- .fragment
+ highlight(Snippet::Normal {
+ date: None,
+ text: snippet_string(
+ "",
+ &["test".to_string()],
+ whatlang::Lang::Eng,
+ SnippetConfig::default()
+ )
+ })
.as_str(),
""
);
assert_eq!(
- snippet_string("", &[], whatlang::Lang::Eng, SnippetConfig::default())
- .fragment
- .as_str(),
+ highlight(Snippet::Normal {
+ date: None,
+ text: snippet_string("", &[], whatlang::Lang::Eng, SnippetConfig::default())
+ })
+ .as_str(),
""
);
}
#[test]
fn compounded_terms() {
- let mut snip = snippet_string(
+ let mut snip = snippet_string_builder(
"this is a test",
&["thisis".to_string()],
whatlang::Lang::Eng,
@@ -473,6 +530,14 @@ Survey in 2016, 2017, and 2018."#;
terms.insert("thisis".to_string());
snip.highlight(&terms, whatlang::Lang::Eng);
- assert_eq!(snip.to_html().as_str(), "<b>this is</b> a test");
+
+ assert_eq!(
+ highlight(Snippet::Normal {
+ date: None,
+ text: snip.build()
+ })
+ .as_str(),
+ "<b>this is</b> a test"
+ );
}
}
diff --git a/frontend/src/lib/api/index.ts b/frontend/src/lib/api/index.ts
index 38e54e9b..5004dbe1 100644
--- a/frontend/src/lib/api/index.ts
+++ b/frontend/src/lib/api/index.ts
@@ -144,6 +144,7 @@ export const api = {
export type SearchResults = components['schemas']['ApiSearchResult'];
export type WebsitesResult = components['schemas']['WebsitesResult'];
export type Webpage = components['schemas']['DisplayedWebpage'];
+export type TextSnippet = components['schemas']['TextSnippet'];
export type Sidebar = components['schemas']['Sidebar'];
export type Entity = components['schemas']['DisplayedEntity'];
export type Widget = components['schemas']['Widget'];
diff --git a/frontend/src/lib/api/schema.d.ts b/frontend/src/lib/api/schema.d.ts
index 892a6c87..5a407c77 100644
--- a/frontend/src/lib/api/schema.d.ts
+++ b/frontend/src/lib/api/schema.d.ts
@@ -3,42 +3,39 @@
* Do not make direct changes to the file.
*/
+
/** OneOf type helpers */
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
-type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
-type OneOf<T extends any[]> = T extends [infer Only]
- ? Only
- : T extends [infer A, infer B, ...infer Rest]
- ? OneOf<[XOR<A, B>, ...Rest]>
- : never;
+type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
+type OneOf<T extends any[]> = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR<A, B>, ...Rest]> : never;
export interface paths {
- '/beta/api/alice': {
- get: operations['alice_route'];
+ "/beta/api/alice": {
+ get: operations["alice_route"];
};
- '/beta/api/autosuggest': {
- post: operations['route'];
+ "/beta/api/autosuggest": {
+ post: operations["route"];
};
- '/beta/api/explore/export': {
- post: operations['explore_export_optic'];
+ "/beta/api/explore/export": {
+ post: operations["explore_export_optic"];
};
- '/beta/api/fact_check': {
- post: operations['fact_check_route'];
+ "/beta/api/fact_check": {
+ post: operations["fact_check_route"];
};
- '/beta/api/search': {
- post: operations['api'];
+ "/beta/api/search": {
+ post: operations["api"];
};
- '/beta/api/sites/export': {
- post: operations['sites_export_optic'];
+ "/beta/api/sites/export": {
+ post: operations["sites_export_optic"];
};
- '/beta/api/summarize': {
- get: operations['summarize_route'];
+ "/beta/api/summarize": {
+ get: operations["summarize_route"];
};
- '/beta/api/webgraph/knows_site': {
- post: operations['knows_site'];
+ "/beta/api/webgraph/knows_site": {
+ post: operations["knows_site"];
};
- '/beta/api/webgraph/similar_sites': {
- post: operations['similar_sites'];
+ "/beta/api/webgraph/similar_sites": {
+ post: operations["similar_sites"];
};
}
@@ -60,18 +57,16 @@ export interface components {
query: string;
returnRankingSignals?: boolean;
safeSearch?: boolean | null;
- selectedRegion?: components['schemas']['Region'] | null;
- siteRankings?: components['schemas']['SiteRankings'] | null;
- };
- ApiSearchResult:
- | (components['schemas']['WebsitesResult'] & {
- /** @enum {string} */
- type: 'websites';
- })
- | (components['schemas']['BangHit'] & {
- /** @enum {string} */
- type: 'bang';
- });
+ selectedRegion?: components["schemas"]["Region"] | null;
+ siteRankings?: components["schemas"]["SiteRankings"] | null;
+ };
+ ApiSearchResult: (components["schemas"]["WebsitesResult"] & {
+ /** @enum {string} */
+ type: "websites";
+ }) | (components["schemas"]["BangHit"] & {
+ /** @enum {string} */
+ type: "bang";
+ });
Bang: {
c?: string | null;
d?: string | null;
@@ -82,26 +77,24 @@ export interface components {
u: string;
};
BangHit: {
- bang: components['schemas']['Bang'];
- redirectTo: components['schemas']['UrlWrapper'];
+ bang: components["schemas"]["Bang"];
+ redirectTo: components["schemas"]["UrlWrapper"];
};
Calculation: {
- expr: components['schemas']['Expr'];
+ expr: components["schemas"]["Expr"];
input: string;
/** Format: double */
result: number;
};
- CodeOrText:
- | {
- /** @enum {string} */
- type: 'code';
- value: string;
- }
- | {
- /** @enum {string} */
- type: 'text';
- value: string;
- };
+ CodeOrText: {
+ /** @enum {string} */
+ type: "code";
+ value: string;
+ } | {
+ /** @enum {string} */
+ type: "text";
+ value: string;
+ };
DisplayedAnswer: {
answer: string;
body: string;
@@ -112,10 +105,10 @@ export interface components {
};
DisplayedEntity: {
imageBase64?: string | null;
- info: (string & string)[][];
+ info: ((string & string)[])[];
/** Format: float */
matchScore: number;
- relatedEntities: components['schemas']['DisplayedEntity'][];
+ relatedEntities: components["schemas"]["DisplayedEntity"][];
smallAbstract: string;
title: string;
};
@@ -124,53 +117,44 @@ export interface components {
domain: string;
prettyUrl: string;
rankingSignals?: {
- [key: string]: components['schemas']['SignalScore'];
+ [key: string]: components["schemas"]["SignalScore"];
} | null;
site: string;
- snippet: components['schemas']['Snippet'];
+ snippet: components["schemas"]["Snippet"];
title: string;
url: string;
};
/** @description base64 encoded `EncryptedState` */
EncodedEncryptedState: string;
EncodedSavedState: string;
- ExecutionState:
- | {
- query: string;
- /** @enum {string} */
- type: 'beginSearch';
- }
- | {
- query: string;
- result: components['schemas']['SimplifiedWebsite'][];
- /** @enum {string} */
- type: 'searchResult';
- }
- | {
- text: string;
- /** @enum {string} */
- type: 'speaking';
- }
- | {
- state: components['schemas']['EncodedEncryptedState'];
- /** @enum {string} */
- type: 'done';
- };
+ ExecutionState: {
+ query: string;
+ /** @enum {string} */
+ type: "beginSearch";
+ } | {
+ query: string;
+ result: components["schemas"]["SimplifiedWebsite"][];
+ /** @enum {string} */
+ type: "searchResult";
+ } | {
+ text: string;
+ /** @enum {string} */
+ type: "speaking";
+ } | {
+ state: components["schemas"]["EncodedEncryptedState"];
+ /** @enum {string} */
+ type: "done";
+ };
ExploreExportOpticParams: {
chosenSites: string[];
similarSites: string[];
};
- Expr: OneOf<
- [
- {
- /** Format: double */
- Number: number;
- },
- {
- Op: Record<string, never>[];
- },
- ]
- >;
+ Expr: OneOf<[{
+ /** Format: double */
+ Number: number;
+ }, {
+ Op: Record<string, never>[];
+ }]>;
FactCheckParams: {
claim: string;
evidence: string;
@@ -183,38 +167,34 @@ export interface components {
highlighted: string;
raw: string;
};
- KnowsSite:
- | {
- site: string;
- /** @enum {string} */
- type: 'known';
- }
- | {
- /** @enum {string} */
- type: 'unknown';
- };
+ KnowsSite: {
+ site: string;
+ /** @enum {string} */
+ type: "known";
+ } | {
+ /** @enum {string} */
+ type: "unknown";
+ };
/** @enum {string} */
- Region: 'All' | 'Denmark' | 'France' | 'Germany' | 'Spain' | 'US';
+ Region: "All" | "Denmark" | "France" | "Germany" | "Spain" | "US";
ScoredSite: {
description?: string | null;
/** Format: double */
score: number;
site: string;
};
- Sidebar:
- | {
- /** @enum {string} */
- type: 'entity';
- value: components['schemas']['DisplayedEntity'];
- }
- | {
- /** @enum {string} */
- type: 'stackOverflow';
- value: {
- answer: components['schemas']['StackOverflowAnswer'];
- title: string;
- };
- };
+ Sidebar: {
+ /** @enum {string} */
+ type: "entity";
+ value: components["schemas"]["DisplayedEntity"];
+ } | {
+ /** @enum {string} */
+ type: "stackOverflow";
+ value: {
+ answer: components["schemas"]["StackOverflowAnswer"];
+ title: string;
+ };
+ };
SignalScore: {
/** Format: double */
coefficient: number;
@@ -237,56 +217,63 @@ export interface components {
liked: string[];
};
SitesExportOpticParams: {
- siteRankings: components['schemas']['SiteRankings'];
- };
- Snippet:
- | {
- date?: string | null;
- text: string;
- /** @enum {string} */
- type: 'normal';
- }
- | {
- answers: components['schemas']['StackOverflowAnswer'][];
- question: components['schemas']['StackOverflowQuestion'];
- /** @enum {string} */
- type: 'stackOverflowQA';
- };
+ siteRankings: components["schemas"]["SiteRankings"];
+ };
+ Snippet: ({
+ date?: string | null;
+ text: components["schemas"]["TextSnippet"];
+ /** @enum {string} */
+ type: "normal";
+ }) | {
+ answers: components["schemas"]["StackOverflowAnswer"][];
+ question: components["schemas"]["StackOverflowQuestion"];
+ /** @enum {string} */
+ type: "stackOverflowQA";
+ };
StackOverflowAnswer: {
accepted: boolean;
- body: components['schemas']['CodeOrText'][];
+ body: components["schemas"]["CodeOrText"][];
date: string;
/** Format: int32 */
upvotes: number;
url: string;
};
StackOverflowQuestion: {
- body: components['schemas']['CodeOrText'][];
+ body: components["schemas"]["CodeOrText"][];
};
Suggestion: {
highlighted: string;
raw: string;
};
+ TextSnippet: {
+ fragments: components["schemas"]["TextSnippetFragment"][];
+ };
+ TextSnippetFragment: {
+ kind: components["schemas"]["TextSnippetFragmentKind"];
+ text: string;
+ };
+ /** @enum {string} */
+ TextSnippetFragmentKind: "normal" | "highlighted";
/**
* Url
* @description Wrapper around `Url` that implements `ToSchema` for `Url`.
*/
UrlWrapper: string;
WebsitesResult: {
- directAnswer?: components['schemas']['DisplayedAnswer'] | null;
- discussions?: components['schemas']['DisplayedWebpage'][] | null;
+ directAnswer?: components["schemas"]["DisplayedAnswer"] | null;
+ discussions?: components["schemas"]["DisplayedWebpage"][] | null;
hasMoreResults: boolean;
numHits: number;
searchDurationMs: number;
- sidebar?: components['schemas']['Sidebar'] | null;
- spellCorrectedQuery?: components['schemas']['HighlightedSpellCorrection'] | null;
- webpages: components['schemas']['DisplayedWebpage'][];
- widget?: components['schemas']['Widget'] | null;
+ sidebar?: components["schemas"]["Sidebar"] | null;
+ spellCorrectedQuery?: components["schemas"]["HighlightedSpellCorrection"] | null;
+ webpages: components["schemas"]["DisplayedWebpage"][];
+ widget?: components["schemas"]["Widget"] | null;
};
Widget: {
/** @enum {string} */
- type: 'calculator';
- value: components['schemas']['Calculation'];
+ type: "calculator";
+ value: components["schemas"]["Calculation"];
};
};
responses: never;
@@ -301,19 +288,20 @@ export type $defs = Record<string, never>;
export type external = Record<string, never>;
export interface operations {
+
alice_route: {
parameters: {
query: {
message: string;
optic?: string | null;
- prevState?: components['schemas']['EncodedSavedState'] | null;
+ prevState?: components["schemas"]["EncodedSavedState"] | null;
};
};
responses: {
/** @description Interact with Alice */
200: {
content: {
- 'application/json': components['schemas']['ExecutionState'];
+ "application/json": components["schemas"]["ExecutionState"];
};
};
};
@@ -328,7 +316,7 @@ export interface operations {
/** @description Autosuggest */
200: {
content: {
- 'application/json': components['schemas']['Suggestion'][];
+ "application/json": components["schemas"]["Suggestion"][];
};
};
};
@@ -336,14 +324,14 @@ export interface operations {
explore_export_optic: {
requestBody: {
content: {
- 'application/json': components['schemas']['ExploreExportOpticParams'];
+ "application/json": components["schemas"]["ExploreExportOpticParams"];
};
};
responses: {
/** @description Export explored sites as an optic */
200: {
content: {
- 'text/plain': string;
+ "text/plain": string;
};
};
};
@@ -351,14 +339,14 @@ export interface operations {
fact_check_route: {
requestBody: {
content: {
- 'application/json': components['schemas']['FactCheckParams'];
+ "application/json": components["schemas"]["FactCheckParams"];
};
};
responses: {
/** @description Fact check the given claim against the given evidence */
200: {
content: {
- 'application/json': components['schemas']['FactCheckResponse'];
+ "application/json": components["schemas"]["FactCheckResponse"];
};
};
};
@@ -366,14 +354,14 @@ export interface operations {
api: {
requestBody: {
content: {
- 'application/json': components['schemas']['ApiSearchQuery'];
+ "application/json": components["schemas"]["ApiSearchQuery"];
};
};
responses: {
/** @description Search results */
200: {
content: {
- 'application/json': components['schemas']['ApiSearchResult'];
+ "application/json": components["schemas"]["ApiSearchResult"];
};
};
};
@@ -381,14 +369,14 @@ export interface operations {
sites_export_optic: {
requestBody: {
content: {
- 'application/json': components['schemas']['SitesExportOpticParams'];
+ "application/json": components["schemas"]["SitesExportOpticParams"];
};
};
responses: {
/** @description Export site rankings as an optic */
200: {
content: {
- 'text/plain': string;
+ "text/plain": string;
};
};
};
@@ -404,7 +392,7 @@ export interface operations {
/** @description Summarize a website */
200: {
content: {
- 'text/plain': string;
+ "text/plain": string;
};
};
};
@@ -419,7 +407,7 @@ export interface operations {
/** @description Whether the site is known */
200: {
content: {
- 'application/json': components['schemas']['KnowsSite'];
+ "application/json": components["schemas"]["KnowsSite"];
};
};
};
@@ -427,14 +415,14 @@ export interface operations {
similar_sites: {
requestBody: {
content: {
- 'application/json': components['schemas']['SimilarSitesParams'];
+ "application/json": components["schemas"]["SimilarSitesParams"];
};
};
responses: {
/** @description List of similar sites */
200: {
content: {
- 'application/json': components['schemas']['ScoredSite'][];
+ "application/json": components["schemas"]["ScoredSite"][];
};
};
};
diff --git a/frontend/src/routes/search/Result.svelte b/frontend/src/routes/search/Result.svelte
index ac21f8c8..778e11de 100644
--- a/frontend/src/routes/search/Result.svelte
+++ b/frontend/src/routes/search/Result.svelte
@@ -1,6 +1,6 @@
<script lang="ts">
import AdjustVertical from '~icons/heroicons/adjustments-vertical';
- import type { Webpage } from '$lib/api';
+ import type { Webpage, TextSnippet } from '$lib/api';
import { createEventDispatcher } from 'svelte';
import { clearSummary, summariesStore } from '$lib/stores';
import Summary from './Summary.svelte';
@@ -55,7 +55,13 @@
<div class="inline">
<span id="snippet-text" class="snippet-text [&:nth-child(2)]:before:content-['—']">
{webpage.snippet.date || ''}
- {@html webpage.snippet.text}
+ {#each webpage.snippet.text.fragments as fragment}
+ {#if fragment.kind == "normal"}
+ {fragment.text}
+ {:else if fragment.kind == "highlighted"}
+ <b>{fragment.text}</b>
+ {/if}
+ {/each}
</span>
</div>
</div>