Attempt to rewrite and execute renamed hash links - need advice
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- nextjs, react, typescript
- Domain
- frontend
Research direction
Start with the LegalPageSlugClient component shown in the issue and reproduce the behavior using a renamed heading hash, a table-of-contents click, and a page reload. Done means the renamed hash navigates correctly in both cases; no repository file or test is named, so further project entry points must be identified first.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 645
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NotionX/react-notion-x
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
NotionX/react-notion-x#710 · 4 comments · 15 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
NotionX/react-notion-x#428 · 9 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
NotionX/react-notion-x#708 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 38/100
NotionX/react-notion-x#686 · 1 comment ·
-
loadPageChank Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
NotionX/react-notion-x#675 · 1 comment ·
All issues in NotionX/react-notion-x
Similar issues
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
vercel/react-tweet#225 ·