···460460 self.title(name)
461461 } else if package == self.package && module == self.module {
462462 self.link(eco_format!("#{name}"), self.title(name), None)
463463+ } else if package == self.package {
464464+ // If we are linking to the current package, we might be viewing the
465465+ // documentation locally and so we need to generate a relative link.
466466+467467+ let mut module_path = module.split('/').peekable();
468468+ let mut current_module = self.module.split('/');
469469+470470+ // The documentation page for the final segment of the module is just
471471+ // an html file by itself, so it doesn't form part of the path and doesn't
472472+ // need to be backtracked using `..`.
473473+ let module_name = module_path.next_back().unwrap_or(module);
474474+ _ = current_module.next_back();
475475+476476+ // The two modules might have some sharer part of the path, which we
477477+ // don't need to traverse back through. However, if the two modules are
478478+ // something like `gleam/a/wibble/wobble` and `gleam/b/wibble/wobble`,
479479+ // the `wibble` folders are two different folders despite being at the
480480+ // same position with the same name.
481481+ let mut encountered_different_path = false;
482482+ let mut path = Vec::new();
483483+484484+ // Calculate how far backwards in the directory tree we need to walk
485485+ for segment in current_module {
486486+ // If this is still part of the shared path, we can just skip it:
487487+ // no need to go back and forth through the same directory in the
488488+ // path!
489489+ if !encountered_different_path && module_path.peek() == Some(&segment) {
490490+ _ = module_path.next();
491491+ } else {
492492+ encountered_different_path = true;
493493+ path.push("..");
494494+ }
495495+ }
496496+497497+ // Once we have walked backwards, we walk forwards again to the correct
498498+ // page.
499499+ path.extend(module_path);
500500+ path.push(module_name);
501501+502502+ let qualified_name = docvec![
503503+ self.variable(EcoString::from(module_name)),
504504+ ".",
505505+ self.title(name)
506506+ ];
507507+508508+ let title = eco_format!("{module}.{{type {name}}}");
509509+510510+ self.link(
511511+ eco_format!("{path}.html#{name}", path = path.join("/")),
512512+ qualified_name,
513513+ Some(title),
514514+ )
463515 } else {
464516 let module_name = module.split('/').next_back().unwrap_or(module);
465517 let qualified_name = docvec![