Skip to content
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: issue of internal and exterrnal links in article #2731

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "2.4.8",
"version": "2.4.9",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomLinkComponent } from '@stoplight/elements-core';
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import * as React from 'react';
import { MemoryRouter } from 'react-router-dom';

Expand All @@ -10,6 +10,31 @@ const DummyLink: CustomLinkComponent = ({ children, ...propsRest }) => {
return <a {...propsRest}>{children}</a>;
};

const data = {
id: '35hsi28m3b2m0',
branch_node_id: -1,
type: 'article',
uri: '/docs/test.md',
slug: '35hsi28m3b2m0-header',
title: 'Header',
summary:
'This is the main header section.\nThe beginning of an awesome article...\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetu...',
project_id: 'cHJqOjQz',
branch_id: 'YnI6MjQw',
branch: 'main',
links: {},
outbound_edges: [
{
id: '35hsi28m3b2m0',
type: 'article',
slug: '35hsi28m3b2m0-header',
title: 'Header',
uri: '/docs/test.md',
},
],
inbound_edges: [],
data: '---\nstoplight-id: 35hsi28m3b2m0\n---\n\n# Header\nThis is the main header section.\nThe beginning of an awesome article...\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Naggggm quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,\n\n## Subheader\nThis is the subheader section. More ContentHere is some more dummy content to fill the page.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia odio vitae vestibulum vestibulum. Cras venenatis euismod malesuada.\nThe beginning of an awesome article...\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Naggggm quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,\n### External\nLinkFor more information, visit Google.\nThe beginning of an awesome article...\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Naggggm quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,\n\n[Go to header](#header)\n\n[Go to subheader](#subheader)\n\n<a href="https://www.google.com">Go to Google</a>',
};
describe(NodeContent.name, () => {
it('renders correctly', async () => {
const { unmount } = render(
Expand Down Expand Up @@ -65,3 +90,39 @@ describe(NodeContent.name, () => {
unmount();
});
});

describe('NodeContent Component Navigation Links', () => {
const originalLocation = window.location;
beforeAll(() => {
delete (window as any).location;
(window as any).location = {
...originalLocation,
pathname: data.slug,
};
});

it('renders links with correct href attributes and navigates to target sections on click', () => {
const { unmount } = render(
<MemoryRouter>
<NodeContent node={data} Link={DummyLink} hideSecurityInfo />
</MemoryRouter>,
);

const headerElement = screen.getByRole('link', { name: /Go to header/i });
const subheaderElement = screen.getByRole('link', { name: /Go to subheader/i });

expect(headerElement).toHaveAttribute('href', '#header');
expect(subheaderElement).toHaveAttribute('href', '#subheader');
fireEvent.click(headerElement);

expect(document.getElementById('header')).toBeInTheDocument();
fireEvent.click(subheaderElement);
expect(document.getElementById('subheader')).toBeInTheDocument();

unmount();

afterAll(() => {
window.location = originalLocation;
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ const LinkComponent: CustomComponentMapping['a'] = ({ children, href, title }) =
const [resolvedUriWithoutAnchor, hash] = resolvedUri.split('#');
const decodedUrl = decodeURIComponent(href);
const decodedResolvedUriWithoutAnchor = decodeURIComponent(resolvedUriWithoutAnchor);
const [pagePathWithoutHash] = window.location.pathname.split('#');

const edge = node.outbound_edges.find(
SB-venkatyadavilli marked this conversation as resolved.
Show resolved Hide resolved
edge => edge.uri === decodedUrl || edge.uri === decodedResolvedUriWithoutAnchor,
edge =>
edge.uri === decodedUrl ||
edge.uri === decodedResolvedUriWithoutAnchor ||
pagePathWithoutHash === `/${edge.slug}`,
);

if (edge) {
Expand Down