-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified HTML for Algolia Crawler (#5441)
* Modified HTML for Algolia Crawler * Added anchor tags within user-guide headers To implement Algolia correctly, my changes would need to be deployed first, as it cannot run on localhost. In the meantime, I simulated Algolia crawling locally using Cheerio, and it worked successfully on my end.
- Loading branch information
1 parent
afad993
commit 9125e95
Showing
4 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/twenty-website/src/shared-utils/wrapHeadingsWithAnchor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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, | ||
children: <a href={`#${id}`}>{child.props.children}</a>, | ||
}); | ||
} | ||
|
||
if (isValidElement(child) && hasChildren(child)) { | ||
return cloneElement(child, { | ||
children: wrapHeadingsWithAnchor(child.props.children), | ||
}); | ||
} | ||
|
||
return child; | ||
}); | ||
}; |