Files
twenty/packages/twenty-website/src/shared-utils/wrapHeadingsWithAnchor.tsx
Ady Beraud 40bd42efc4 Added Algolia Search (#5524)
-Added Algolia Search Box :

<img width="707" alt="Screenshot 2024-05-22 at 10 05 13"
src="https://github.com/twentyhq/twenty/assets/102751374/d26f9748-2a80-4690-88ca-16b078c52915">

-Added Algolia Search Bar:

<img width="294" alt="Screenshot 2024-05-22 at 10 05 56"
src="https://github.com/twentyhq/twenty/assets/102751374/ad503894-4ae1-41e4-bd4b-6241f7679142">

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
2024-05-22 12:06:00 +02:00

42 lines
1.0 KiB
TypeScript

import React, {
Children,
cloneElement,
isValidElement,
ReactElement,
ReactNode,
} from 'react';
export const wrapHeadingsWithAnchor = (children: ReactNode): ReactNode => {
const hasChildren = (
element: ReactElement,
): element is ReactElement<{ children: ReactNode }> => {
return element.props.children !== undefined;
};
return Children.map(children, (child) => {
if (
isValidElement(child) &&
typeof child.type === 'string' &&
['h1', 'h2', 'h3', 'h4'].includes(child.type)
) {
const id = child.props.children
.toString()
.replace(/\s+/g, '-')
.toLowerCase();
return cloneElement(child as ReactElement<any>, {
id: id,
className: 'anchor',
children: <a href={`#${id}`}>{child.props.children}</a>,
});
}
if (isValidElement(child) && hasChildren(child)) {
return cloneElement(child, {
children: wrapHeadingsWithAnchor(child.props.children),
});
}
return child;
});
};