-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(react): heading with links not rendered #41
Changes from all commits
a86764d
e36c181
336b282
ccd701d
ef595a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcms/rich-text-react-renderer': patch | ||
--- | ||
|
||
Fix heading with links not being rendered |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { | |
} from './defaultElements'; | ||
import { RenderText } from './RenderText'; | ||
import { getElements } from './util/getElements'; | ||
import { elementIsEmpty } from './util/elementIsEmpty'; | ||
|
||
function RenderNode({ | ||
node, | ||
|
@@ -65,15 +66,15 @@ function RenderElement({ | |
const { nodeId, nodeType } = rest; | ||
|
||
/** | ||
* Checks if element has empty text, so it can be removed. | ||
* Checks if the element is empty, so that it can be removed. | ||
* | ||
* Elements that can be removed with empty text are defined in `defaultRemoveEmptyElements` | ||
*/ | ||
if ( | ||
defaultRemoveEmptyElements?.[ | ||
elementKeys[type] as keyof RemoveEmptyElementType | ||
] && | ||
children[0].text === '' | ||
elementIsEmpty({ children }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hard to distinguish with all the formatting changes, is that -with the separate function definition- the actual/only fix of this PR? (makes sense to me, just making sure i did not miss anything else among the noise) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's the only fix. I created this small utility function to check if an element is empty, so we can reuse it if needed. |
||
) { | ||
return <Fragment />; | ||
} | ||
|
@@ -85,7 +86,7 @@ function RenderElement({ | |
* Since there won't be duplicated ID's, it's safe to use the first element. | ||
*/ | ||
const referenceValues = isEmbed | ||
? references?.filter((ref) => ref.id === nodeId)[0] | ||
? references?.filter(ref => ref.id === nodeId)[0] | ||
: null; | ||
|
||
/** | ||
|
@@ -251,9 +252,7 @@ export function RichText({ | |
If it isn't defined and there's embed elements, it will show a warning | ||
*/ | ||
if (__DEV__) { | ||
const embedElements = elements.filter( | ||
(element) => element.type === 'embed' | ||
); | ||
const embedElements = elements.filter(element => element.type === 'embed'); | ||
|
||
if (embedElements.length > 0 && !references) { | ||
console.warn( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
ElementNode, | ||
isElement, | ||
isText, | ||
Text, | ||
} from '@graphcms/rich-text-types'; | ||
|
||
export function elementIsEmpty({ | ||
children, | ||
}: { | ||
children: (ElementNode | Text)[]; | ||
}): boolean { | ||
// Checks if the children array has more than one element. | ||
// It may have a link inside, that's why we need to check this condition. | ||
if (children.length > 1) { | ||
const hasText = children.filter(function f(child): boolean | number { | ||
if (isText(child) && child.text !== '') { | ||
return true; | ||
} | ||
|
||
if (isElement(child)) { | ||
return (child.children = child.children.filter(f)).length; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
return hasText.length > 0 ? false : true; | ||
} else if (children[0].text === '') return true; | ||
|
||
return true; | ||
} | ||
Comment on lines
+8
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we maybe use recursion here? I mean what if there is deeper nesting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't think of a use case where we have deeper nesting with the children's object, but this function will check if there's more than one item in the array of children. If it has, it will filter it using recursion. Note that the callback of the filter method is a function that we reuse if the node is an element. If it's a text and it's not empty, we return true. When there's text inside it, the I think that's it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 did you change your autoformater config?
import type
is better and becoming the "best practice"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or is one of these not a TS type but an actual Class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to remove it 😓
There was a problem with ESLint not recognizing it as a valid import. The only fix was removing it because I couldn't update TSDX ESLint version. I tried fixing it with Yarn resolutions, but it didn't work.
We probably need to move away from TSDX. The project is abandoned. See jaredpalmer/tsdx#1065.