@@ -223,6 +223,9 @@ fn write_mod_contents(
223
223
) {
224
224
write_brief ( ctxt, doc. brief ( ) ) ;
225
225
write_desc ( ctxt, doc. desc ( ) ) ;
226
+ if option:: is_some ( doc. index ) {
227
+ write_index ( ctxt, option:: get ( doc. index ) ) ;
228
+ }
226
229
227
230
for itemtag in doc. items {
228
231
alt itemtag {
@@ -251,6 +254,35 @@ fn should_write_crate_description() {
251
254
assert str:: contains ( markdown, "this is the crate" ) ;
252
255
}
253
256
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
+
254
286
fn write_nmod ( ctxt : ctxt , doc : doc:: nmoddoc ) {
255
287
write_header ( ctxt, h1, doc:: nmodtag ( doc) ) ;
256
288
@@ -905,6 +937,8 @@ mod test {
905
937
#debug ( "doc (path): %?" , doc) ;
906
938
let doc = attr_pass:: mk_pass ( ) . f ( srv, doc) ;
907
939
#debug ( "doc (attr): %?" , doc) ;
940
+ let doc = markdown_index_pass:: mk_pass ( ) . f ( srv, doc) ;
941
+ #debug ( "doc (index): %?" , doc) ;
908
942
( srv, doc)
909
943
}
910
944
}
0 commit comments