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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::*;
/// Search and lookup utilities for `DocSite`.
impl DocSite {
/// Searches pages matching `query` (case-insensitive) across title, route, and content blocks.
///
/// # Arguments
///
/// - `&Self` - Documentation site reference.
/// - `&str` - Search query text.
///
/// # Returns
///
/// - `Vec<&DocPage>` - Matching pages in display order.
#[must_use]
pub fn search(&self, query: &str) -> Vec<&DocPage> {
let needle: String = query.trim().to_ascii_lowercase();
if needle.is_empty() {
return self.get_pages().iter().collect();
}
self.get_pages()
.iter()
.filter(|page: &&DocPage| page.matches_query(&needle))
.collect()
}
/// Looks up a page by its canonical route path.
///
/// # Arguments
///
/// - `&Self` - Documentation site reference.
/// - `&str` - Route path to locate.
///
/// # Returns
///
/// - `Option<&DocPage>` - Matching page if present.
#[must_use]
pub fn find_by_route(&self, route: &str) -> Option<&DocPage> {
self.get_pages()
.iter()
.find(|page: &&DocPage| page.get_route() == route)
}
/// Builds compact full-text search index entries for every page in this site.
///
/// # Arguments
///
/// - `&Self` - Documentation site reference.
///
/// # Returns
///
/// - `Vec<SearchIndexEntry>` - Ordered search records ready for JSON serialization.
#[must_use]
pub fn search_entries(&self) -> Vec<SearchIndexEntry> {
self.get_pages()
.iter()
.map(|page: &DocPage| {
let headings: Vec<String> = page
.get_toc()
.iter()
.map(|item: &DocTocItem| item.get_title().clone())
.collect();
let mut excerpt: String = String::new();
for block in page.get_blocks().iter() {
if excerpt.len() >= 360 {
break;
}
match block {
DocBlock::Paragraph(text)
| DocBlock::ListItem(text)
| DocBlock::BlockQuote(text) => {
if !excerpt.is_empty() {
excerpt.push(' ');
}
excerpt.push_str(text);
}
DocBlock::Table { headers, rows } => {
for h in headers.iter() {
if !excerpt.is_empty() {
excerpt.push(' ');
}
excerpt.push_str(h);
}
for r in rows.iter().take(3) {
for cell in r.iter() {
if !excerpt.is_empty() {
excerpt.push(' ');
}
excerpt.push_str(cell);
}
}
}
DocBlock::Image { alt, .. } => {
if !alt.is_empty() {
if !excerpt.is_empty() {
excerpt.push(' ');
}
excerpt.push_str(alt);
}
}
DocBlock::Heading { .. }
| DocBlock::Code { .. }
| DocBlock::HorizontalRule => {}
}
}
let trimmed: String = excerpt.chars().take(360).collect();
SearchIndexEntry::new(
page.get_route().clone(),
page.get_title().clone(),
page.get_section().clone(),
page.get_summary().clone(),
headings,
trimmed,
)
})
.collect()
}
}
/// Query matching and body attachment helpers for `DocPage`.
impl DocPage {
/// Stores the raw Markdown body slice for full-fidelity HTML generation.
///
/// # Arguments
///
/// - `&Self` - Mutable documentation page reference.
/// - `String` - Raw Markdown body text without frontmatter.
pub(crate) fn set_markdown_body(&mut self, body: String) {
self.markdown_body = body;
}
/// Returns `true` if `needle` (already lowercased) matches the title, route, summary, or any block.
///
/// # Arguments
///
/// - `&Self` - Documentation page reference.
/// - `&str` - Lowercased search query.
///
/// # Returns
///
/// - `bool` - Whether the page matches.
#[must_use]
pub fn matches_query(&self, needle: &str) -> bool {
if self.get_title().to_ascii_lowercase().contains(needle)
|| self.get_route().to_ascii_lowercase().contains(needle)
|| self.get_summary().to_ascii_lowercase().contains(needle)
{
return true;
}
self.get_blocks()
.iter()
.any(|block: &DocBlock| match block {
DocBlock::Heading { text, .. } => text.to_ascii_lowercase().contains(needle),
DocBlock::Paragraph(text)
| DocBlock::ListItem(text)
| DocBlock::BlockQuote(text) => text.to_ascii_lowercase().contains(needle),
DocBlock::Code { code, .. } => code.to_ascii_lowercase().contains(needle),
DocBlock::Image { alt, .. } => alt.to_ascii_lowercase().contains(needle),
DocBlock::Table { headers, rows } => {
let in_headers: bool = headers
.iter()
.any(|h: &String| h.to_ascii_lowercase().contains(needle));
let in_rows: bool = rows.iter().any(|row: &Vec<String>| {
row.iter()
.any(|cell: &String| cell.to_ascii_lowercase().contains(needle))
});
in_headers || in_rows
}
DocBlock::HorizontalRule => false,
})
}
}