-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
72 lines (65 loc) · 2.32 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use pagefind::service::api::PagefindIndex;
#[tokio::main]
pub async fn main() {
// read from file data.json
let mut index = PagefindIndex::new(pagefind::PagefindInboundConfig {
source: "source".into(),
site: "site".into(),
bundle_dir: None,
output_subdir: None,
output_path: None,
root_selector: "root_selector".into(),
exclude_selectors: vec![],
glob: "**/*.{html}".into(),
force_language: None,
serve: false,
verbose: false,
logfile: None,
keep_index_url: false,
service: false,
})
.expect("config is valid");
let data = std::fs::read_to_string("data.json").unwrap();
let json_value: serde_json::Value = serde_json::from_str(&data).unwrap();
if let serde_json::Value::Array(items) = &json_value {
for item in items {
// If you want to treat each item as an object
if let serde_json::Value::Object(obj) = item {
// Iterate over the key-value pairs in the object
let chunk_html = &obj["chunk_html"];
let link = &obj["link"];
// get metadata from the object
let metadata = &obj["metadata"];
match index
.add_record(
link.to_string(),
chunk_html.to_string(),
"en".to_string(),
metadata.as_object().map(|m| {
m.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}),
None,
None,
)
.await
{
Ok(_) => {
println!("Successfully added record");
}
Err(_) => {
println!("Failed to add record");
}
}
}
}
}
let files = index.get_files().await;
for file in files.iter() {
// Need to write the file to the output path
println!("file {:?}", file.path);
println!("content {:?}", file.content);
}
index.write_files(Some("static/pagefind".to_string())).await;
}