Skip to content

Commit e9dcc7b

Browse files
authored
Rollup merge of rust-lang#56709 - GuillaumeGomez:reduce-search-index, r=QuietMisdreavus
Remove unneeded extra chars to reduce search-index size Before: ``` 2013782 Dec 11 10:16 build/x86_64-unknown-linux-gnu/doc/search-index.js ``` After: ``` 1736597 Dec 11 10:50 build/x86_64-unknown-linux-gnu/doc/search-index.js ``` No changes in the output of the search. r? @QuietMisdreavus
2 parents 33a94f1 + 987bf2e commit e9dcc7b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
806806
}
807807

808808
pub fn plain_summary_line(md: &str) -> String {
809+
plain_summary_line_full(md, false)
810+
}
811+
812+
pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
809813
struct ParserWrapper<'a> {
810814
inner: Parser<'a>,
811815
is_in: isize,
@@ -852,7 +856,21 @@ pub fn plain_summary_line(md: &str) -> String {
852856
s.push_str(&t);
853857
}
854858
}
855-
s
859+
if limit_length && s.chars().count() > 60 {
860+
let mut len = 0;
861+
let mut ret = s.split_whitespace()
862+
.take_while(|p| {
863+
// + 1 for the added character after the word.
864+
len += p.chars().count() + 1;
865+
len < 60
866+
})
867+
.collect::<Vec<_>>()
868+
.join(" ");
869+
ret.push('…');
870+
ret
871+
} else {
872+
s
873+
}
856874
}
857875

858876
pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {

src/librustdoc/html/render.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
698698
ty: item.type_(),
699699
name: item.name.clone().unwrap(),
700700
path: fqp[..fqp.len() - 1].join("::"),
701-
desc: plain_summary_line(item.doc_value()),
701+
desc: plain_summary_line_short(item.doc_value()),
702702
parent: Some(did),
703703
parent_idx: None,
704704
search_type: get_index_search_type(&item),
@@ -736,7 +736,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
736736
}
737737

738738
let crate_doc = krate.module.as_ref().map(|module| {
739-
plain_summary_line(module.doc_value())
739+
plain_summary_line_short(module.doc_value())
740740
}).unwrap_or(String::new());
741741

742742
let mut crate_data = BTreeMap::new();
@@ -1481,7 +1481,7 @@ impl DocFolder for Cache {
14811481
ty: item.type_(),
14821482
name: s.to_string(),
14831483
path: path.join("::"),
1484-
desc: plain_summary_line(item.doc_value()),
1484+
desc: plain_summary_line_short(item.doc_value()),
14851485
parent,
14861486
parent_idx: None,
14871487
search_type: get_index_search_type(&item),
@@ -1673,7 +1673,7 @@ impl<'a> Cache {
16731673
ty: item.type_(),
16741674
name: item_name.to_string(),
16751675
path: path.clone(),
1676-
desc: plain_summary_line(item.doc_value()),
1676+
desc: plain_summary_line_short(item.doc_value()),
16771677
parent: None,
16781678
parent_idx: None,
16791679
search_type: get_index_search_type(&item),
@@ -2388,7 +2388,13 @@ fn shorter<'a>(s: Option<&'a str>) -> String {
23882388
#[inline]
23892389
fn plain_summary_line(s: Option<&str>) -> String {
23902390
let line = shorter(s).replace("\n", " ");
2391-
markdown::plain_summary_line(&line[..])
2391+
markdown::plain_summary_line_full(&line[..], false)
2392+
}
2393+
2394+
#[inline]
2395+
fn plain_summary_line_short(s: Option<&str>) -> String {
2396+
let line = shorter(s).replace("\n", " ");
2397+
markdown::plain_summary_line_full(&line[..], true)
23922398
}
23932399

23942400
fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result {

0 commit comments

Comments
 (0)