Refactoring javascript toc