-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathsearch.rs
More file actions
172 lines (152 loc) · 4.13 KB
/
search.rs
File metadata and controls
172 lines (152 loc) · 4.13 KB
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::DocNodeWithContext;
use super::GenerateCtx;
use super::RenderContext;
use super::UrlResolveKind;
use crate::html::util::Id;
use crate::html::util::IdBuilder;
use crate::html::util::IdKind;
use crate::js_doc::JsDocTag;
use serde::Deserialize;
use serde::Serialize;
use serde_json::json;
use std::borrow::Cow;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlimKindCtx {
pub char: char,
pub kind: Cow<'static, str>,
pub title: Cow<'static, str>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchIndexNode {
pub id: Id,
pub kind: Vec<SlimKindCtx>,
pub name: Box<str>,
pub file: Box<str>,
pub doc: Box<str>,
pub url: Box<str>,
#[serde(skip_serializing_if = "is_empty", default)]
pub category: Box<str>,
pub deprecated: bool,
}
fn is_empty(s: &str) -> bool {
s.is_empty()
}
pub fn doc_nodes_into_search_index_node(
ctx: &RenderContext,
symbol: &DocNodeWithContext,
parent_id: Option<Id>,
) -> Vec<SearchIndexNode> {
let kinds = symbol
.get_kind_ctxs()
.into_iter()
.map(|kind| SlimKindCtx {
char: kind.char,
kind: kind.kind,
title: kind.title,
})
.collect();
let deprecated = super::util::all_deprecated(
&symbol.declarations.iter().collect::<Vec<_>>(),
);
let doc = super::jsdoc::strip(
ctx,
&symbol.declarations[0]
.js_doc
.doc
.clone()
.unwrap_or_default(),
)
.into_boxed_str();
let name = symbol.get_qualified_name();
let abs_url = ctx.ctx.resolve_path(
super::UrlResolveKind::Root,
super::UrlResolveKind::Symbol {
file: &symbol.origin,
symbol: name,
},
);
let category = symbol.declarations[0]
.js_doc
.tags
.iter()
.find_map(|x| {
if let JsDocTag::Category { doc } = x {
Some(doc.clone())
} else {
None
}
})
.unwrap_or_default();
let id = parent_id.unwrap_or_else(|| {
IdBuilder::new(ctx)
.kind(IdKind::Namespace)
.name(name)
.build_unregistered()
});
let mut out = vec![SearchIndexNode {
id: id.clone(),
kind: kinds,
name: html_escape::encode_text(name).into(),
file: html_escape::encode_double_quoted_attribute(&symbol.origin.path)
.into(),
doc,
url: abs_url.into_boxed_str(),
category,
deprecated,
}];
if let Some(drilldowns) = symbol.get_drilldown_symbols() {
out.extend(
drilldowns
.into_iter()
.filter(|drilldown_node| !drilldown_node.is_internal(ctx.ctx))
.flat_map(|drilldown_node| {
doc_nodes_into_search_index_node(
ctx,
&drilldown_node,
Some(id.clone()),
)
}),
);
}
out
}
pub fn generate_search_index(ctx: &GenerateCtx) -> serde_json::Value {
let doc_nodes = ctx.doc_nodes.values().flat_map(|nodes| {
crate::html::partition::flatten_namespace(
ctx,
Box::new(nodes.iter().map(Cow::Borrowed)),
)
});
let render_ctx = RenderContext::new(ctx, &[], UrlResolveKind::AllSymbols);
let mut seen = std::collections::HashSet::new();
let mut doc_nodes = doc_nodes
// Symbols hidden from the rendered docs (e.g. `@internal`) must also be
// hidden from search, otherwise they remain findable there. `@ignore`
// symbols are already dropped earlier, but `is_internal` is only filtered
// in the listing views, so the search index has to apply it too. See #590.
.filter(|node| !node.is_internal(ctx))
.flat_map(|node| doc_nodes_into_search_index_node(&render_ctx, &node, None))
.filter(|node| seen.insert((node.name.clone(), node.file.clone())))
.collect::<Vec<_>>();
doc_nodes.sort_by(|a, b| a.file.cmp(&b.file));
let search_index = json!({
"kind": "search",
"nodes": doc_nodes
});
search_index
}
pub(crate) fn get_search_index_file(
ctx: &GenerateCtx,
) -> Result<String, anyhow::Error> {
let search_index = generate_search_index(ctx);
let search_index_str = serde_json::to_string(&search_index)?;
let index = format!(
r#"(function () {{
window.DENO_DOC_SEARCH_INDEX = {};
}})()"#,
search_index_str
);
Ok(index)
}