Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to rewrite and execute renamed hash links - need advice #527

Open
hellocory opened this issue Oct 26, 2023 · 0 comments
Open

Attempt to rewrite and execute renamed hash links - need advice #527

hellocory opened this issue Oct 26, 2023 · 0 comments

Comments

@hellocory
Copy link

hellocory commented Oct 26, 2023

I'm still some-what new to React, and NextJS, so I might be way out of my depth here... but I am trying to make it so the header hash links are the same name as the header.

Example:

a header normally has this hashlink: #239293823o98127dasdjh2dad
but instead, I want it to be #header-name.

I have accomplished this, but now I am running into the issue where the functionality just stops.

I need it to work and behave like normal hashes, but it's not actually loading the the part that is in the URL when you reload, or when you click the item via Table of Contents. Not sure what my next step is. Here is what I currently have:

const LegalPageSlugClient = ({ data, recordMap }: Props) => {
  const [hydrated, setHydrated] = useState(false);
  console.log(recordMap);

  useEffect(() => {
    const hashLinks = document.querySelectorAll(".notion-hash-link");
    const headerContent = document.querySelectorAll(".notion-h-title");

    // Create a map to match the hash link elements to their respective headers
    const hashToHeaderMap = new Map();

    // Iterate through hashLinks to map them
    hashLinks.forEach((link) => {
      const href = link.getAttribute("href");

      if (href && typeof href === "string" && href.startsWith("#")) {
        const headingId = href.slice(1); // Remove the "#" character
        const headingText = link.nextElementSibling?.textContent || "";
        if (headingText) {
          hashToHeaderMap.set(headingId, headingText);
        }
      }
    });

    // Iterate through headerContent to create anchor elements
    headerContent.forEach((header) => {
      const headerId = header.getAttribute("id");
      if (headerId) {
        const headerAnchor = document.createElement("div");
        headerAnchor.classList.add("notion-header-anchor");
        headerAnchor.setAttribute("id", headerId);
        header.insertAdjacentElement("beforebegin", headerAnchor);
      }
    });

    // interate through "notion-table-of-contents-item" and update those hrefs as well.
    const tableOfContents = document.querySelectorAll(
      ".notion-table-of-contents-item",
    );

    tableOfContents.forEach((link) => {
      link.addEventListener("click", (event) => {
        event.preventDefault(); // Prevent default behavior
        const href = link.getAttribute("href");

        if (href && typeof href === "string" && href.startsWith("#")) {
          const headingId = href.slice(1); // Remove the "#" character
          const targetElement = document.querySelector(`#${headingId}`);
          if (targetElement) {
            targetElement.scrollIntoView({ behavior: "smooth" });
          }
        }
      });
    });

    // Update hash link href attributes
    hashLinks.forEach((link) => {
      const href = link.getAttribute("href");

      if (href && typeof href === "string" && href.startsWith("#")) {
        const headingId = href.slice(1); // Remove the "#" character
        const headingText = hashToHeaderMap.get(headingId);
        if (headingText) {
          link.setAttribute("href", `#${nicePathName(headingText)}`);
        }
      }
    });

    setHydrated(true);
    const hash = window.location.hash;
    console.log(hash);
    // if (hash && hydrated) {
    //   const targetElement = document.querySelector(hash);
    //   if (targetElement) {
    //     targetElement.scrollIntoView({ behavior: "smooth" });
    //   }
    // }
  }, [hydrated]);

Let me know what I can do! :)

PS: Ignore my noobish sudo-code :P I learn by steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant