Skip to content

Commit 87b9d67

Browse files
committed
rustdoc: Write indexes in output
1 parent 3ed6f65 commit 87b9d67

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/rustdoc/markdown_pass.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ fn write_mod_contents(
223223
) {
224224
write_brief(ctxt, doc.brief());
225225
write_desc(ctxt, doc.desc());
226+
if option::is_some(doc.index) {
227+
write_index(ctxt, option::get(doc.index));
228+
}
226229

227230
for itemtag in doc.items {
228231
alt itemtag {
@@ -251,6 +254,35 @@ fn should_write_crate_description() {
251254
assert str::contains(markdown, "this is the crate");
252255
}
253256

257+
fn write_index(ctxt: ctxt, index: doc::index) {
258+
if vec::is_empty(index.entries) {
259+
ret;
260+
}
261+
262+
for entry in index.entries {
263+
let header = header_text_(entry.kind, entry.name);
264+
let id = entry.link;
265+
ctxt.w.write_line(#fmt("* [%s](#%s)", header, id));
266+
}
267+
ctxt.w.write_line("");
268+
}
269+
270+
#[test]
271+
fn should_write_index() {
272+
let markdown = test::render("mod a { } mod b { }");
273+
assert str::contains(
274+
markdown,
275+
"\n\n* [Module `a`](#module-a)\n\
276+
* [Module `b`](#module-b)\n\n"
277+
);
278+
}
279+
280+
#[test]
281+
fn should_not_write_index_if_no_entries() {
282+
let markdown = test::render("");
283+
assert !str::contains(markdown, "\n\n\n");
284+
}
285+
254286
fn write_nmod(ctxt: ctxt, doc: doc::nmoddoc) {
255287
write_header(ctxt, h1, doc::nmodtag(doc));
256288

@@ -905,6 +937,8 @@ mod test {
905937
#debug("doc (path): %?", doc);
906938
let doc = attr_pass::mk_pass().f(srv, doc);
907939
#debug("doc (attr): %?", doc);
940+
let doc = markdown_index_pass::mk_pass().f(srv, doc);
941+
#debug("doc (index): %?", doc);
908942
(srv, doc)
909943
}
910944
}

0 commit comments

Comments
 (0)