Skip to content
Open
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
28 changes: 18 additions & 10 deletions src/client/theme-default/composables/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ export function useActiveAnchor(

const scrollY = window.scrollY
const innerHeight = window.innerHeight
const offsetHeight = document.body.offsetHeight
const isBottom = scrollY + innerHeight - offsetHeight >= 0

// resolvedHeaders may be repositioned, hidden or fix positioned
const headers = resolvedHeaders
Expand All @@ -154,20 +152,30 @@ export function useActiveAnchor(
return
}

// page bottom - highlight last link
if (isBottom) {
activateLink(headers.at(-1)?.link ?? null)
return
}

// find the last header above the top of viewport
const maxScroll = Math.max(
0,
document.documentElement.scrollHeight - innerHeight
)
const bottomProgress =
maxScroll > 0
? Math.min(
1,
Math.max(0, (scrollY - (maxScroll - innerHeight)) / innerHeight)
)
: 0

// find the last header above the active line
let activeLink: string | null = null
for (const { link, top, scrollMarginTop } of headers) {
if (top > scrollY + scrollMarginTop + 4) {
const baseOffset = scrollMarginTop + 4
const activeLine =
scrollY + baseOffset + (innerHeight - baseOffset) * bottomProgress
if (top > activeLine) {
break
}
activeLink = link
}

activateLink(activeLink)
}

Expand Down