nagaozen / markdown-it-toc-done-right

A table of contents (TOC) plugin for Markdown-it with focus on semantic and security. Made to work gracefully with markdown-it-anchor.
MIT License
133 stars 26 forks source link

Doesn't work with hash routing #76

Closed GVodyanov closed 6 months ago

GVodyanov commented 2 years ago

When using with vue-router hash routing, this link: http://localhost:5173/#/note/5 becomes this http://localhost:5173/#/addizione-e-sottrazione when clicking on a toc heading, causing the page to be empty.

fantasy955 commented 1 year ago

Should there be no way to use native anchor jumps in hash mode? In my case, I added click events to the elements in the TOC to enable anchor jumps: Note that you should execute the code after the TOC generation.

\\ other code
const titleElments = document.querySelectorAll(".reference-link");  // select the title elements in content
\\ other code

let tocContainer = document.querySelector(".markdown-toc-list");  // select your toc cantainer
let liElements = tocContainer.getElementsByTagName("li");
for (let i = 0; i < liElements.length; i++) {
    let el = liElements[i].firstChild;
    el.addEventListener("click", function (event) {
        event.preventDefault();
        titleElments[i].scrollIntoView({
            behavior: "smooth",
        });
    });
    observer.observe(titleElments[i]);
}
nagaozen commented 6 months ago

When using with vue-router hash routing, this link: http://localhost:5173/#/note/5 becomes this http://localhost:5173/#/addizione-e-sottrazione when clicking on a toc heading, causing the page to be empty.

We won't support this because one of our goal is to be semantically correct. Using # to set a history landmark is something we used before History API existed. Therefore, modern sites and apps SHOULD (in RFC 2119 terms) use it instead and keep the hash fragment to SEO.