summaryrefslogtreecommitdiffstats
path: root/src/components/Widgets/ToC/ToC.tsx
blob: 8a2d4937a0f3001808f4ed9f8953c22f48630991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { ExpandableWidget, OrderedList } from '@components/WidgetParts';
import { Heading } from '@ts/types/app';
import useHeadingsTree from '@utils/hooks/useHeadingsTree';
import { useIntl } from 'react-intl';

const ToC = () => {
  const intl = useIntl();
  const headingsTree = useHeadingsTree('article');
  const title = intl.formatMessage({
    defaultMessage: 'Table of contents',
    description: 'ToC: widget title',
  });

  const getItems = (headings: Heading[]) => {
    return headings.map((heading) => {
      return (
        <li key={heading.id}>
          <a href={`#${heading.id}`}>{heading.title}</a>
          {heading.children.length > 0 && (
            <OrderedList items={getItems(heading.children)} />
          )}
        </li>
      );
    });
  };

  return (
    <ExpandableWidget title={title} kind="toc" expand={true} withBorders={true}>
      <OrderedList items={getItems(headingsTree)} />
    </ExpandableWidget>
  );
};

export default ToC;