1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/components/ui/MiniTocs/MiniTocs.tsx
Grace Park 1a6bee4601 Refactor to add RestReferencePage (#25781)
* refactor to add RestReferencePage

* remove structured data from article page

* add back clientsidehightlight js and remove structuredcontent

* update endpoint available for github apps page to use RestReferencePage

* check for minitocs

* use children for ArticlePage for overview endpoints page and move category data to RestReferencePage

* create MiniTocs component

* add MiniTocs

* fix unique key issue

* break into out into own div

* remove some unnecessary ts

* refactor looping section

* simplify markdown component usage

* add key prop

* add key prop

* add unique key

Co-authored-by: Rachael Sewell <rachmari@github.com>
2022-03-08 18:00:38 +00:00

53 lines
1.4 KiB
TypeScript

import cx from 'classnames'
import { ActionList, Heading } from '@primer/react'
import { MiniTocItem } from 'components/context/ArticleContext'
import { Link } from 'components/Link'
import { useTranslation } from 'components/hooks/useTranslation'
export type MiniTocsPropsT = {
pageTitle: string
miniTocItems: MiniTocItem[]
}
const renderTocItem = (item: MiniTocItem) => {
return (
<ActionList.Item
as="li"
key={item.contents}
className={item.platform}
sx={{ listStyle: 'none', padding: '2px' }}
>
<div className={cx('lh-condensed d-block width-full')}>
<div dangerouslySetInnerHTML={{ __html: item.contents }} />
{item.items && item.items.length > 0 ? (
<ul className="ml-3">{item.items.map(renderTocItem)}</ul>
) : null}
</div>
</ActionList.Item>
)
}
export function MiniTocs({ pageTitle, miniTocItems }: MiniTocsPropsT) {
const { t } = useTranslation('pages')
return (
<>
<Heading as="h2" id="in-this-article" className="mb-1" sx={{ fontSize: 1 }}>
<Link href="#in-this-article">{t('miniToc')}</Link>
</Heading>
<ActionList
key={pageTitle}
items={miniTocItems.map((items, i) => {
return {
key: pageTitle + i,
text: pageTitle,
renderItem: () => <ul>{renderTocItem(items)}</ul>,
}
})}
/>
</>
)
}