Skip to content

Commit 88031c0

Browse files
committed
Change type on links
1 parent d0954d1 commit 88031c0

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

packages/module/src/Message/LinkMessage/LinkMessage.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
// ============================================================================
44

55
import React from 'react';
6-
import { ExtraProps } from 'react-markdown';
76
import { Button, ButtonProps } from '@patternfly/react-core';
87
import { ExternalLinkSquareAltIcon } from '@patternfly/react-icons';
98

10-
const LinkMessage = ({ children, ...props }: JSX.IntrinsicElements['a'] & ExtraProps) => {
11-
if (props.target === '_blank') {
9+
const LinkMessage = ({ children, target, href, ...props }: ButtonProps) => {
10+
if (target === '_blank') {
1211
return (
1312
<Button
1413
component="a"
1514
variant="link"
15+
href={href}
1616
icon={<ExternalLinkSquareAltIcon />}
1717
iconPosition="end"
1818
isInline
19-
{...(props as ButtonProps)}
19+
target={target}
20+
{...props}
2021
>
2122
{children}
2223
</Button>
2324
);
2425
}
2526

2627
return (
27-
<Button isInline component="a" variant="link" {...(props as ButtonProps)}>
28+
<Button isInline component="a" href={href} variant="link" {...props}>
2829
{children}
2930
</Button>
3031
);

packages/module/src/Message/Message.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ import ThMessage from './TableMessage/ThMessage';
3838
import { TableProps } from '@patternfly/react-table';
3939
import ImageMessage from './ImageMessage/ImageMessage';
4040
import rehypeUnwrapImages from 'rehype-unwrap-images';
41-
import { PluggableList } from 'react-markdown/lib';
4241
import rehypeExternalLinks from 'rehype-external-links';
4342
import rehypeSanitize from 'rehype-sanitize';
43+
import { PluggableList } from 'react-markdown/lib';
4444
import LinkMessage from './LinkMessage/LinkMessage';
4545

4646
export interface MessageAttachment {
@@ -167,11 +167,18 @@ export const MessageBase: React.FunctionComponent<MessageProps> = ({
167167
isLiveRegion = true,
168168
innerRef,
169169
tableProps,
170-
additionalRehypePlugins = [],
171170
openLinkInNewTab = true,
171+
additionalRehypePlugins = [],
172172
...props
173173
}: MessageProps) => {
174174
const { beforeMainContent, afterMainContent, endContent } = extraContent || {};
175+
let rehypePlugins: PluggableList = [rehypeUnwrapImages];
176+
if (openLinkInNewTab) {
177+
rehypePlugins = rehypePlugins.concat([[rehypeExternalLinks, { target: '_blank' }, rehypeSanitize]]);
178+
}
179+
if (additionalRehypePlugins) {
180+
rehypePlugins.push(...additionalRehypePlugins);
181+
}
175182
let avatarClassName;
176183
if (avatarProps && 'className' in avatarProps) {
177184
const { className, ...rest } = avatarProps;
@@ -181,16 +188,6 @@ export const MessageBase: React.FunctionComponent<MessageProps> = ({
181188
// Keep timestamps consistent between Timestamp component and aria-label
182189
const date = new Date();
183190
const dateString = timestamp ?? `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
184-
185-
const rehypePlugins: PluggableList = [rehypeUnwrapImages];
186-
if (openLinkInNewTab) {
187-
rehypePlugins.concat([rehypeExternalLinks, { target: '_blank' }, rehypeSanitize]);
188-
}
189-
if (additionalRehypePlugins) {
190-
// should be able to supply a custom sanitization schema this way if needed and apply any other plugins
191-
rehypePlugins.concat(additionalRehypePlugins);
192-
}
193-
194191
return (
195192
<section
196193
aria-label={`Message from ${role} - ${dateString}`}
@@ -258,7 +255,11 @@ export const MessageBase: React.FunctionComponent<MessageProps> = ({
258255
},
259256
th: (props) => <ThMessage {...props} />,
260257
img: (props) => <ImageMessage {...props} />,
261-
a: (props) => <LinkMessage {...props} />
258+
a: (props) => (
259+
<LinkMessage href={props.href} rel={props.rel} target={props.target}>
260+
{props.children}
261+
</LinkMessage>
262+
)
262263
}}
263264
remarkPlugins={[remarkGfm]}
264265
rehypePlugins={rehypePlugins}

0 commit comments

Comments
 (0)