Skip to content

Commit

Permalink
fix(docs): fix improper links of the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Kadirsaglm committed Sep 1, 2024
1 parent 2cf4255 commit f9c73f2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/__docs__/src/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ import type {
TOCHeadingData
} from './props'

// set sequential id to each heading of the examples
const setExampleHeadingIds = (headings: NodeListOf<HTMLHeadingElement>) => {
const headingsArray = Array.from(headings)
const parentExampleHeadingIndex = headingsArray.findIndex(
(h) => h.id === 'Examples'
)

if (parentExampleHeadingIndex === -1) return

const childExampleHeadings: HTMLHeadingElement[] = []

for (let i = parentExampleHeadingIndex + 1; i < headingsArray.length; i++) {
const h = headingsArray[i]
if (h.tagName === 'H4') {
childExampleHeadings.push(h)
} else {
break
}
}

childExampleHeadings.forEach((h, i) => {
// eslint-disable-next-line no-param-reassign
h.id = `Examples-${i + 1}`
})
}

class TableOfContents extends Component<
TableOfContentsProps,
TableOfContentsState
Expand Down Expand Up @@ -84,6 +110,10 @@ class TableOfContents extends Component<
const headings = pageElement?.querySelectorAll<HTMLHeadingElement>(
'h1, h2, h3, h4, h5, h6'
)

// to have proper links to the headings of the examples, we need to set heading ids manually
setExampleHeadingIds(headings)

const TOCData: TableOfContentsState['TOCData'] = []
let hasLinkToTitle = false

Expand Down

0 comments on commit f9c73f2

Please sign in to comment.