Skip to content

Commit

Permalink
Fix page is bottom function to handle non-scrollable elements (#373)
Browse files Browse the repository at this point in the history
* fix for page is bottom bug

* fix type file

* formatting and add biome

* remove problematic code

* update to fix bottommode hashes showing as highlight on first load

* final updates for this fix/feature
  • Loading branch information
tscanlin authored Feb 9, 2025
1 parent c7d7333 commit 2d82e2a
Show file tree
Hide file tree
Showing 30 changed files with 22,488 additions and 22,099 deletions.
15 changes: 8 additions & 7 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"presets": [
["@babel/preset-env", {
"targets": {
"esmodules": true
[
"@babel/preset-env",
{
"targets": {
"esmodules": true
}
}
}],
],
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-syntax-dynamic-import"
],
"plugins": ["@babel/plugin-syntax-dynamic-import"],
"ignore": []
}
33 changes: 33 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": false,
"ignore": [".gitignore", "dist/", "test/", "data/"]
},
"formatter": {
"enabled": true,
"indentWidth": 2,
"indentStyle": "space"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"enabled": true,
"quoteStyle": "double",
"semicolons": "asNeeded"
}
}
}
17 changes: 3 additions & 14 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"name": "tocbot",
"description": "Generate a table of contents based on the heading structure of a html document.",
"main": [
"dist/tocbot.js",
"dist/tocbot.css"
],
"authors": [
"Tim Scanlin"
],
"main": ["dist/tocbot.js", "dist/tocbot.css"],
"authors": ["Tim Scanlin"],
"license": "MIT",
"keywords": [
"tocbot",
Expand All @@ -21,11 +16,5 @@
"vanilla"
],
"homepage": "https://github.com/tscanlin/tocbot",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
"ignore": ["**/.*", "node_modules", "bower_components", "test", "tests"]
}
247 changes: 125 additions & 122 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,125 @@
export = tocbot;
export as namespace tocbot;

declare namespace tocbot {
/**
* @see https://github.com/tscanlin/tocbot#options
*/
interface IStaticOptions {
// Where to render the table of contents.
tocSelector?: string
// Where to grab the headings to build the table of contents.
contentSelector?: string
// Which headings to grab inside of the contentSelector element.
headingSelector?: string
// Headings that match the ignoreSelector will be skipped.
ignoreSelector?: string
// For headings inside relative or absolute positioned containers within content.
hasInnerContainers?: boolean
// Main class to add to links.
linkClass?: string
// Extra classes to add to links.
extraLinkClasses?: string
// Class to add to active links // the link corresponding to the top most heading on the page.
activeLinkClass?: string
// Main class to add to lists.
listClass?: string
// Extra classes to add to lists.
extraListClasses?: string
// Class that gets added when a list should be collapsed.
isCollapsedClass?: string
// Class that gets added when a list should be able
// to be collapsed but isn't necessarily collapsed.
collapsibleClass?: string
// Class to add to list items.
listItemClass?: string
// Class to add to active list items.
activeListItemClass?: string
// How many heading levels should not be collapsed.
// For example; number 6 will show everything since
// there are only 6 heading levels and number 0 will collapse them all.
// The sections that are hidden will open
// and close as you scroll to headings within them.
collapseDepth?: number
// Smooth scrolling enabled.
scrollSmooth?: boolean
// Smooth scroll duration.
scrollSmoothDuration?: number
// Smooth scroll offset.
scrollSmoothOffset?: number
// Callback for scroll end.
scrollEndCallback?(e: WheelEvent): void
// Headings offset between the headings and the top of the document (this is meant for minor adjustments).
headingsOffset?: number
// Timeout between events firing to make sure it's
// not too rapid (for performance reasons).
throttleTimeout?: number
// Element to add the positionFixedClass to.
positionFixedSelector?: string | null
// Fixed position class to add to make sidebar fixed after scrolling
// down past the fixedSidebarOffset.
positionFixedClass?: string
// fixedSidebarOffset can be any number but by default is set
// to auto which sets the fixedSidebarOffset to the sidebar
// element's offsetTop from the top of the document on init.
fixedSidebarOffset?: 'auto' | number
// includeHtml can be set to true to include the HTML markup from the
// heading node instead of just including the innerText.
includeHtml?: boolean
// includeTitleTags automatically sets the html title tag of the link
// to match the title. This can be useful for SEO purposes or
// when truncating titles.
includeTitleTags?: boolean
// onclick function to apply to all links in toc. will be called with
// the event as the first parameter; and this can be used to stop // propagation; prevent default or perform action
onClick?: (e: MouseEvent) => void
// orderedList can be set to false to generate unordered lists (ul)
// instead of ordered lists (ol)
orderedList?: boolean
// If there is a fixed article scroll container; set to calculate titles' offset
scrollContainer?: string | null
// prevent ToC DOM rendering if it's already rendered by an external system
skipRendering?: boolean
// Optional callback to change heading labels.
// For example it can be used to cut down and put ellipses on multiline headings you deem too long.
// Called each time a heading is parsed. Expects a string in return, the modified label to display.
headingLabelCallback?: (headingLabel: string) => string
// ignore headings that are hidden in DOM
ignoreHiddenElements?: boolean
// Optional callback to modify properties of parsed headings.
// The heading element is passed in node parameter and information parsed by default parser is provided in obj parameter.
// Function has to return the same or modified obj.
// The heading will be excluded from TOC if nothing is returned.
headingObjectCallback?: (obj: object, node: HTMLElement) => object | void
// Set the base path, useful if you use a `base` tag in `head`.
basePath?: string
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync?: boolean
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset?: number
// Enable the URL hash to update with the proper heading ID as
// a user scrolls the page.
enableUrlHashUpdateOnScroll?: boolean
}

/**
* Initialize tocbot with an options object.
* @see https://github.com/tscanlin/tocbot#init
*/
function init(options?: IStaticOptions): void
/**
* Destroy tocbot and remove event listeners.
* @see https://github.com/tscanlin/tocbot#destroy
*/
function destroy(): void
/**
* Refresh tocbot if the document changes and it needs to be rebuilt.
* @see https://github.com/tscanlin/tocbot#refresh
*/
function refresh(options?: IStaticOptions): void
}
export = tocbot
export as namespace tocbot

declare namespace tocbot {
/**
* @see https://github.com/tscanlin/tocbot#options
*/
interface IStaticOptions {
// Where to render the table of contents.
tocSelector?: string
// Where to grab the headings to build the table of contents.
contentSelector?: string
// Which headings to grab inside of the contentSelector element.
headingSelector?: string
// Headings that match the ignoreSelector will be skipped.
ignoreSelector?: string
// For headings inside relative or absolute positioned containers within content.
hasInnerContainers?: boolean
// Main class to add to links.
linkClass?: string
// Extra classes to add to links.
extraLinkClasses?: string
// Class to add to active links // the link corresponding to the top most heading on the page.
activeLinkClass?: string
// Main class to add to lists.
listClass?: string
// Extra classes to add to lists.
extraListClasses?: string
// Class that gets added when a list should be collapsed.
isCollapsedClass?: string
// Class that gets added when a list should be able
// to be collapsed but isn't necessarily collapsed.
collapsibleClass?: string
// Class to add to list items.
listItemClass?: string
// Class to add to active list items.
activeListItemClass?: string
// How many heading levels should not be collapsed.
// For example; number 6 will show everything since
// there are only 6 heading levels and number 0 will collapse them all.
// The sections that are hidden will open
// and close as you scroll to headings within them.
collapseDepth?: number
// Smooth scrolling enabled.
scrollSmooth?: boolean
// Smooth scroll duration.
scrollSmoothDuration?: number
// Smooth scroll offset.
scrollSmoothOffset?: number
// Callback for scroll end.
scrollEndCallback?(e: WheelEvent): void
// Headings offset between the headings and the top of the document (this is meant for minor adjustments).
headingsOffset?: number
// Timeout between events firing to make sure it's
// not too rapid (for performance reasons).
throttleTimeout?: number
// Element to add the positionFixedClass to.
positionFixedSelector?: string | null
// Fixed position class to add to make sidebar fixed after scrolling
// down past the fixedSidebarOffset.
positionFixedClass?: string
// fixedSidebarOffset can be any number but by default is set
// to auto which sets the fixedSidebarOffset to the sidebar
// element's offsetTop from the top of the document on init.
fixedSidebarOffset?: "auto" | number
// includeHtml can be set to true to include the HTML markup from the
// heading node instead of just including the innerText.
includeHtml?: boolean
// includeTitleTags automatically sets the html title tag of the link
// to match the title. This can be useful for SEO purposes or
// when truncating titles.
includeTitleTags?: boolean
// onclick function to apply to all links in toc. will be called with
// the event as the first parameter; and this can be used to stop // propagation; prevent default or perform action
onClick?: (e: MouseEvent) => void
// orderedList can be set to false to generate unordered lists (ul)
// instead of ordered lists (ol)
orderedList?: boolean
// If there is a fixed article scroll container; set to calculate titles' offset
scrollContainer?: string | null
// prevent ToC DOM rendering if it's already rendered by an external system
skipRendering?: boolean
// Optional callback to change heading labels.
// For example it can be used to cut down and put ellipses on multiline headings you deem too long.
// Called each time a heading is parsed. Expects a string in return, the modified label to display.
headingLabelCallback?: (headingLabel: string) => string
// ignore headings that are hidden in DOM
ignoreHiddenElements?: boolean
// Optional callback to modify properties of parsed headings.
// The heading element is passed in node parameter and information parsed by default parser is provided in obj parameter.
// Function has to return the same or modified obj.
// The heading will be excluded from TOC if nothing is returned.
headingObjectCallback?: (obj: object, node: HTMLElement) => object | void
// Set the base path, useful if you use a `base` tag in `head`.
basePath?: string
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync?: boolean
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset?: number
// Enable the URL hash to update with the proper heading ID as
// a user scrolls the page.
enableUrlHashUpdateOnScroll?: boolean
// Threshold for when bottom mode should be enabled to handle
// highlighting links that cannot be scrolled to.
bottomModeThreshold?: number
}

/**
* Initialize tocbot with an options object.
* @see https://github.com/tscanlin/tocbot#init
*/
function init(options?: IStaticOptions): void
/**
* Destroy tocbot and remove event listeners.
* @see https://github.com/tscanlin/tocbot#destroy
*/
function destroy(): void
/**
* Refresh tocbot if the document changes and it needs to be rebuilt.
* @see https://github.com/tscanlin/tocbot#refresh
*/
function refresh(options?: IStaticOptions): void
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { destroy, init, refresh } from './src/js/index-esm.js'
import { destroy, init, refresh } from "./src/js/index-esm.js"
const tocbot = { destroy, init, refresh }

export { destroy, init, refresh }
Expand Down
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { destroy, init, refresh } from './src/js/index-esm.js'
import { destroy, init, refresh } from "./src/js/index-esm.js"
const tocbot = { destroy, init, refresh }

export { destroy, init, refresh }
Expand Down
8 changes: 4 additions & 4 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assetPrefix = '/tocbot'
const assetPrefix = "/tocbot"

export default {
assetPrefix,
Expand All @@ -7,7 +7,7 @@ export default {
return newConfig
},
exportPathMap: () => ({
'/': { page: '/' },
'/changelog': { page: '/changelog' }
})
"/": { page: "/" },
"/changelog": { page: "/changelog" },
}),
}
Loading

0 comments on commit 2d82e2a

Please sign in to comment.