-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v3] fix
removeLinks
when input node is link (#2431)
- Loading branch information
Dimitri POSTOLOV
authored
Oct 11, 2023
1 parent
e2f4505
commit 31c2ee7
Showing
4 changed files
with
81 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'nextra-theme-docs': patch | ||
--- | ||
|
||
fix `removeLinks` when input node is link |
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
42 changes: 42 additions & 0 deletions
42
packages/nextra-theme-docs/src/utils/remove-links.test.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,42 @@ | ||
import { removeLinks } from './remove-links' | ||
|
||
describe('removeLinks()', () => { | ||
it('should return string', () => { | ||
expect(removeLinks('foo')).toBe('foo') | ||
}) | ||
it('should remove link inside fragment', () => { | ||
const node = ( | ||
<> | ||
foo | ||
<code> | ||
<a href="#">bar</a> | ||
</code> | ||
</> | ||
) | ||
expect(removeLinks(node)).toMatchInlineSnapshot(` | ||
[ | ||
<React.Fragment> | ||
foo | ||
<code> | ||
bar | ||
</code> | ||
</React.Fragment>, | ||
] | ||
`) | ||
}) | ||
it('should remove wrapper link', () => { | ||
const node = ( | ||
<a href="#"> | ||
foo<code>bar</code> | ||
</a> | ||
) | ||
expect(removeLinks(node)).toMatchInlineSnapshot(` | ||
[ | ||
"foo", | ||
<code> | ||
bar | ||
</code>, | ||
] | ||
`) | ||
}) | ||
}) |
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,32 @@ | ||
import type { ReactElement } from 'react' | ||
import { Children, cloneElement } from 'react' | ||
|
||
type TOCElement = ReactElement | string | ||
|
||
function isLink(node: TOCElement): node is ReactElement { | ||
return typeof node !== 'string' && !!node.props.href | ||
} | ||
|
||
export function removeLinks(node: TOCElement): TOCElement[] | string { | ||
if (typeof node === 'string') { | ||
return node | ||
} | ||
// @ts-expect-error fixme | ||
return Children.map(node, child => { | ||
if (isLink(child)) { | ||
child = child.props.children | ||
} | ||
|
||
if (typeof child === 'string') { | ||
return child | ||
} | ||
|
||
if (Array.isArray(child)) { | ||
return removeLinks(child) | ||
} | ||
|
||
const children = removeLinks(child.props.children) | ||
|
||
return cloneElement(child, { children }) | ||
}) | ||
} |