Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ const CardBody = ({
};

const Card: React.FC<CardProps> = ({ link, ...props }) => {
const isExternalLink =
link !== undefined && /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(link);

return link ? (
<Link
href={link}
target='_blank'
rel='noopener noreferrer'
target={isExternalLink ? '_blank' : undefined}
rel={isExternalLink ? 'noopener noreferrer' : undefined}
data-test='card-link'
>
<CardBody link={link} {...props} />
Expand Down
10 changes: 8 additions & 2 deletions components/StyledMarkdownBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ export const StyledMarkdownBlock = ({ markdown }: StyledMarkdownBlockProps) => {
as={href}
href='/'
title={title}
target='_blank'
rel='noopener noreferrer'
className={combinedClassName} // Use the combined className
>
{children}
</Link>
) : href.charAt(0) === '#' ? (
<a
href={href}
title={title}
className={combinedClassName} // Use the combined className
>
{children}
</a>
) : (
<a
href={href}
Expand Down
23 changes: 18 additions & 5 deletions cypress/components/Card.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,28 @@ describe('Card Component', () => {
cy.get('[data-test="card-title"]').should('have.text', RoadmapProps.title);
cy.get('[data-test="card-body"]').should('have.text', RoadmapProps.body);
cy.get('[data-test="card-read-more"]').should('have.text', 'Read More');
cy.get('[data-test="card-link"]').should(
'have.attr',
'href',
RoadmapProps.link,
);
cy.get('[data-test="card-link"]')
.should('have.attr', 'href', RoadmapProps.link)
.and('have.attr', 'target', '_blank');
cy.get('[data-test="card-title"]').should('have.class', 'text-[2rem]');
cy.get('[data-test="card-body"]').should('have.class', 'text-[1rem]');
});

it('should keep internal links in the current tab', () => {
cy.mount(
<Card
icon='/icons/eye.svg'
title='Introduction'
body='Discover JSON Schema.'
link='/overview/what-is-jsonschema'
/>,
);

cy.get('[data-test="card-link"]')
.should('have.attr', 'href', '/overview/what-is-jsonschema')
.and('not.have.attr', 'target', '_blank');
});

// Render the Roadmap card with default header and body sizes
it('should render Roadmap card with default header and body sizes', () => {
const missingSizes = {
Expand Down
29 changes: 29 additions & 0 deletions cypress/components/StyledMarkdownBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { StyledMarkdownBlock } from '~/components/StyledMarkdownBlock';
import mockNextRouter from '../plugins/mockNextRouterUtils';

describe('StyledMarkdownBlock links', () => {
beforeEach(() => {
mockNextRouter();
});

it('keeps internal and fragment links in the current tab while opening external links in a new tab', () => {
cy.mount(
<StyledMarkdownBlock
markdown={
'[Internal](/docs/guide)\n\n[Section](#overview)\n\n[External](https://example.com)'
}
/>,
);

cy.contains('a', 'Internal')
.should('have.attr', 'href', '/docs/guide')
.and('not.have.attr', 'target');
cy.contains('a', 'Section')
.should('have.attr', 'href', '#overview')
.and('not.have.attr', 'target');
cy.contains('a', 'External')
.should('have.attr', 'href', 'https://example.com')
.and('have.attr', 'target', '_blank');
});
});
Loading