This has already been implemented in the current development version of JohnCMS and is planned for release in the next few weeks.
If you need it right away, you can take a look at the current implementation in the repository:
https://github.com/johncms/johncms/blob/9.x/modules/library/config/routes.php
Please note that the Library module has been significantly refactored since the legacy version, so the implementation is quite different from the code you're currently working with. It should be viewed more as a reference than a drop-in solution.