Skip to content

Commit

Permalink
Move default title to the meta object, add default image
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Jun 21, 2022
1 parent 4c4c442 commit 85c4781
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
11 changes: 8 additions & 3 deletions pagefind/features/fragments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ Feature: Fragments
Given I have a "public/index.html" file with the body:
"""
<p data-result>Nothing</p>
<p data-result-two>Nothing</p>
"""
Given I have a "public/cat/index.html" file with the content:
"""
<html>
<head>
<meta data-pagefind-meta="image[content]" content="/kitty.jpg" property="og:image">
<meta data-pagefind-meta="social-image[content]" content="/kitty.jpg" property="og:image">
</head>
<body>
<img src="/logo.png" />
<h1 data-pagefind-filter="title">
Cat Post.
</h1>
<span data-pagefind-ignore data-pagefind-filter="animal">cats</span>
<img src="/cat.png" />
<p>A post about the 'felines'</p>
<p>This post has some <span data-pagefind-meta="adjective">gnarly</span> things to test the fragment formatting.</p>
</body>
Expand All @@ -35,11 +38,13 @@ Feature: Fragments
let search = await pagefind.search("cat");
let data = await search.results[0].data();
document.querySelector('[data-result]').innerText = data.title;
document.querySelector('[data-result]').innerText = data.meta.title;
document.querySelector('[data-result-two]').innerText = data.meta.image;
}
"""
Then There should be no logs
Then The selector "[data-result]" should contain "Cat Post."
Then The selector "[data-result-two]" should contain "/cat.png"

Scenario: Search results return nicely formatted content
When I evaluate:
Expand Down Expand Up @@ -96,7 +101,7 @@ Feature: Fragments
let search = await pagefind.search("cat");
let data = await search.results[0].data();
document.querySelector('[data-result]').innerText = data.meta.image + " — " + data.meta.adjective;
document.querySelector('[data-result]').innerText = data.meta["social-image"] + " — " + data.meta.adjective;
}
"""
Then There should be no logs
Expand Down
1 change: 0 additions & 1 deletion pagefind/src/fossick/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ impl Fossicker {
page_number: 0,
data: PageFragmentData {
url: build_url(&self.file_path, options),
title: data.title.clone(),
content: data.digest.clone(),
filters: data.filters.clone(),
meta: data.meta.clone(),
Expand Down
27 changes: 12 additions & 15 deletions pagefind/src/fossick/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub struct DomParser<'a> {
#[derive(Default, Debug)]
struct DomParserData {
current_node: Rc<RefCell<DomParsingNode>>,
title: Option<String>,
filters: HashMap<String, Vec<String>>,
meta: HashMap<String, String>,
}
Expand All @@ -65,7 +64,6 @@ struct DomParsingNode {
#[derive(Debug)]
pub struct DomParserResult {
pub digest: String,
pub title: String,
pub filters: HashMap<String, Vec<String>>,
pub meta: HashMap<String, String>,
}
Expand All @@ -92,6 +90,7 @@ impl<'a> DomParser<'a> {
let should_ignore_el = el.has_attribute("data-pagefind-ignore") || REMOVE_SELECTORS.contains(&el.tag_name().as_str());
let filter = el.get_attribute("data-pagefind-filter").map(|attr| parse_attr_string(attr, el));
let meta = el.get_attribute("data-pagefind-meta").map(|attr| parse_attr_string(attr, el));
let tag_name = el.tag_name();

let node = Rc::new(RefCell::new(DomParsingNode{
parent: Some(Rc::clone(&data.borrow().current_node)),
Expand All @@ -106,7 +105,7 @@ impl<'a> DomParser<'a> {
data.current_node = Rc::clone(&node);
}

let can_have_content = el.on_end_tag(enclose! { (data, node) move |end| {
let can_have_content = el.on_end_tag(enclose! { (data, node, tag_name) move |end| {
let mut data = data.borrow_mut();
let mut node = node.borrow_mut();

Expand All @@ -131,6 +130,10 @@ impl<'a> DomParser<'a> {
if let Some((meta, value)) = node.get_attribute_pair(&node.meta) {
data.meta.insert(meta, value);
}
// Try to capture the first title on the page (if unset)
if tag_name == "h1" && !data.meta.contains_key("title") {
data.meta.insert("title".into(), normalize_content(&node.current_value));
}

// If we bail out now, the content won't be persisted anywhere
// and the node + children will be dropped.
Expand Down Expand Up @@ -196,6 +199,12 @@ impl<'a> DomParser<'a> {
if let Some((meta, value)) = node.get_attribute_pair(&node.meta) {
data.meta.insert(meta, value);
}
// Try to capture the first image _after_ a title (if unset)
if tag_name == "img" && data.meta.contains_key("title") && !data.meta.contains_key("image") {
if let Some(src) = el.get_attribute("src") {
data.meta.insert("image".into(), src);
}
}
}
Ok(())
})},
Expand All @@ -206,17 +215,6 @@ impl<'a> DomParser<'a> {
node.current_value.push_str(el.as_str());
Ok(())
})},
// Track the first h1 on the page as the title to return in search
// TODO: This doesn't handle a chunk boundary,
// we can instead handle this by marking the node as a title and handling it in end_node
enclose! { (data) text!("h1", move |el| {
let mut data = data.borrow_mut();
let text = normalize_content(el.as_str());
if data.title.is_none() && !text.is_empty() {
data.title = Some(text);
}
Ok(())
})},
],
..Settings::default()
},
Expand Down Expand Up @@ -254,7 +252,6 @@ impl<'a> DomParser<'a> {
let node = node.borrow();
DomParserResult {
digest: normalize_content(&node.current_value),
title: data.title.unwrap_or_default(),
filters: data.filters,
meta: data.meta,
}
Expand Down
1 change: 0 additions & 1 deletion pagefind/src/fragments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use serde::Serialize;
#[derive(Serialize, Debug)]
pub struct PageFragmentData {
pub url: String,
pub title: String,
pub content: String,
pub word_count: usize,
pub filters: HashMap<String, Vec<String>>,
Expand Down

0 comments on commit 85c4781

Please sign in to comment.