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

Refactor TOC to support anchor specified in metadata #103

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions blocks/toc/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,18 @@ function buildTOCSide(ul, block) {

function buildTOCTop(ul, block) {
block.parentElement.classList.add('flat');
let others = false;
let tocIndex = 0;
[...document.querySelector('main').children].forEach((section, i) => {
if (others) {
const liItem = ul.querySelectorAll('li')[i - tocIndex - 1];
if (liItem) {
const liVar = liItem.innerText.toLowerCase();
section.id = liVar;
const aLink = document.createElement('a');
aLink.href = `#${liVar}`;
aLink.innerText = liItem.innerText;
liItem.innerText = '';
liItem.append(aLink);
}
}
if (section.classList.contains('toc-container')) {
others = true;
tocIndex = i;
}

[...ul.querySelectorAll('li')].forEach((li) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also be implemented for the side TOC as well to maintain consistency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dnbute Unfortunately I don't have this needs for current page. If you have we can do it, or we can do it in the future.

const liVar = li.innerText.toLowerCase();
const aLink = document.createElement('a');
aLink.href = `#${liVar}`;
aLink.innerText = li.innerText;
li.innerText = '';
li.append(aLink);
});
[...document.querySelectorAll('[data-toc-anchor]')].forEach((section) => {
const name = section.dataset.tocAnchor.toLowerCase().trim();
section.id = name;
});
}

Expand Down
Loading