Skip to content

Commit

Permalink
Merge pull request #1241 from VamshiReddy02/enhance-anchore-link
Browse files Browse the repository at this point in the history
Enhanced the anchor-link to individual table rows
  • Loading branch information
Timothy McCallum authored Apr 5, 2024
2 parents 07d3c38 + 63344f4 commit e63fc6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
26 changes: 17 additions & 9 deletions static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,17 @@ const addAnchorLinks = () => {
elementsToProcess.forEach(element => {
let uniqueId;
if (element.tagName.toLowerCase() === 'tr') {
const closestHeading = element.closest('.content').querySelector('.heading-anchor');
const rowText = Array.from(element.cells).map(cell => cell.textContent.trim()).join(' ').toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
let closestHeading = element.closest('table').previousElementSibling;
while(closestHeading && !closestHeading.matches('h1, h2, h3, h4')) {
closestHeading = closestHeading.previousElementSibling;
}
const firstColumnName = element.cells[0].textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');

if (closestHeading) {
const headingText = closestHeading.textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
uniqueId = `${headingText}-${rowText}`;
const headingId = closestHeading.getAttribute('id');
uniqueId = `${headingId}-${firstColumnName}`;
} else {
uniqueId = rowText;
uniqueId = firstColumnName;
}
} else {
uniqueId = element.textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
Expand All @@ -1136,10 +1139,15 @@ const addAnchorLinks = () => {
anchor.addEventListener("click", (e) => {
e.preventDefault();
window.location = anchor.href;
document.querySelector(anchor.getAttribute('href')).scrollIntoView({
behavior: 'smooth',
block: 'start'
})
let targetId = anchor.getAttribute('href').substring(1);
let targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
})
}

})
})
}
Expand Down
26 changes: 17 additions & 9 deletions static/js/src/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ const addAnchorLinks = () => {
elementsToProcess.forEach(element => {
let uniqueId;
if (element.tagName.toLowerCase() === 'tr') {
const closestHeading = element.closest('.content').querySelector('.heading-anchor');
const rowText = Array.from(element.cells).map(cell => cell.textContent.trim()).join(' ').toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
let closestHeading = element.closest('table').previousElementSibling;
while(closestHeading && !closestHeading.matches('h1, h2, h3, h4')) {
closestHeading = closestHeading.previousElementSibling;
}
const firstColumnName = element.cells[0].textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');

if (closestHeading) {
const headingText = closestHeading.textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
uniqueId = `${headingText}-${rowText}`;
const headingId = closestHeading.getAttribute('id');
uniqueId = `${headingId}-${firstColumnName}`;
} else {
uniqueId = rowText;
uniqueId = firstColumnName;
}
} else {
uniqueId = element.textContent.trim().toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-');
Expand All @@ -110,10 +113,15 @@ const addAnchorLinks = () => {
anchor.addEventListener("click", (e) => {
e.preventDefault();
window.location = anchor.href;
document.querySelector(anchor.getAttribute('href')).scrollIntoView({
behavior: 'smooth',
block: 'start'
})
let targetId = anchor.getAttribute('href').substring(1);
let targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
})
}

})
})
}
Expand Down

0 comments on commit e63fc6f

Please sign in to comment.