Files
twenty/packages/twenty-website/src/shared-utils/wrapHeadingsWithAnchor.tsx
martmull ed7c48e12a Fix use as draft (#9718)
- remove delete serverless function when archiving workflow version
- update copy serverless function to reset serverless function to old
version
- remove createNewWorkflowVersion and use createDraftFromWorkflowVersion
- fix step update issue and optimistic rendering when generate draft
from active version
2025-01-21 15:44:52 +01:00

42 lines
1023 B
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,
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;
});
};