badboy / mdbook-toc

A preprocessor for mdbook to add inline Table of Contents support.
Mozilla Public License 2.0
163 stars 20 forks source link

Generate unique slugs for identically named headers #8

Closed NBonaparte closed 3 years ago

NBonaparte commented 3 years ago

mdBook creates unique slugs for headings by appending -{n}, where n is the number of times a heading with the same name has appeared before:

https://github.com/rust-lang/mdBook/blob/7af4b1dfe80903e3a2288355a2a396e7458866ee/src/renderer/html_handlebars/hbs_renderer.rs#L742-L764

This follows the same process to ensure the links match. I also optimized string handling to reduce allocations (using write!() instead of format!() + push()/push_str()), and also applied clippy fixes.