From cf005d5fa3af320ffe127838230712a7f2d2efb8 Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Wed, 30 Oct 2024 14:34:16 +0530 Subject: [PATCH 1/6] fix: issue of internal and exterrnal links in article --- .../src/components/NodeContent/NodeContent.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx index 293a6447f..87a4e4543 100644 --- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx +++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx @@ -172,7 +172,10 @@ const LinkComponent: CustomComponentMapping['a'] = ({ children, href, title }) = const decodedUrl = decodeURIComponent(href); const decodedResolvedUriWithoutAnchor = decodeURIComponent(resolvedUriWithoutAnchor); const edge = node.outbound_edges.find( - edge => edge.uri === decodedUrl || edge.uri === decodedResolvedUriWithoutAnchor, + edge => + edge.uri === decodedUrl || + edge.uri === decodedResolvedUriWithoutAnchor || + edge.uri + '/' === decodedResolvedUriWithoutAnchor, ); if (edge) { From a27f231cc50a2a04e908086f7a492c6cae817dc6 Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Fri, 8 Nov 2024 14:19:07 +0530 Subject: [PATCH 2/6] added comment about change --- .../src/components/NodeContent/NodeContent.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx index 87a4e4543..9ecb8e7e7 100644 --- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx +++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx @@ -171,6 +171,8 @@ const LinkComponent: CustomComponentMapping['a'] = ({ children, href, title }) = const [resolvedUriWithoutAnchor, hash] = resolvedUri.split('#'); const decodedUrl = decodeURIComponent(href); const decodedResolvedUriWithoutAnchor = decodeURIComponent(resolvedUriWithoutAnchor); + + //added extra condition to handle internal link. const edge = node.outbound_edges.find( edge => edge.uri === decodedUrl || From 06e454c6bf85bacbf02080c3577113f7eaa4de14 Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Tue, 19 Nov 2024 20:03:50 +0530 Subject: [PATCH 3/6] fix:handle internal links --- .../src/components/NodeContent/NodeContent.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx index 9ecb8e7e7..c57fb2eab 100644 --- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx +++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx @@ -171,13 +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('#'); - //added extra condition to handle internal link. const edge = node.outbound_edges.find( edge => edge.uri === decodedUrl || edge.uri === decodedResolvedUriWithoutAnchor || - edge.uri + '/' === decodedResolvedUriWithoutAnchor, + pagePathWithoutHash === `/${edge.slug}`, ); if (edge) { From e2623bad13cd941359a9ab7b10b391d99234e8df Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Tue, 19 Nov 2024 20:04:15 +0530 Subject: [PATCH 4/6] fix: added unit test case --- .../NodeContent/NodeContent.spec.tsx | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx index c7d53b2b2..a7cb0d10d 100644 --- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx +++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx @@ -1,5 +1,5 @@ import { CustomLinkComponent } from '@stoplight/elements-core'; -import { render, screen } from '@testing-library/react'; +import { render, screen, fireEvent } from '@testing-library/react'; import * as React from 'react'; import { MemoryRouter } from 'react-router-dom'; @@ -10,6 +10,31 @@ const DummyLink: CustomLinkComponent = ({ children, ...propsRest }) => { return {children}; }; +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\nGo to Google', +}; describe(NodeContent.name, () => { it('renders correctly', async () => { const { unmount } = render( @@ -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( + + + , + ); + + 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; + }); + }); +}); From 1bb523ff361ff70c8113325d45ceb8805aca061e Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Thu, 21 Nov 2024 19:42:23 +0530 Subject: [PATCH 5/6] chor(deps): bump elements-core-dev-portal to 2.4.9V --- packages/elements-dev-portal/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/elements-dev-portal/package.json b/packages/elements-dev-portal/package.json index be450bc79..760441ab1 100644 --- a/packages/elements-dev-portal/package.json +++ b/packages/elements-dev-portal/package.json @@ -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": [ From 31f575c85f26e278998f7b1e0b2b4e1c1227ac61 Mon Sep 17 00:00:00 2001 From: "yogesh.jadhav@smartbear.com" Date: Thu, 21 Nov 2024 19:49:03 +0530 Subject: [PATCH 6/6] fix:lint issue fixed --- .../src/components/NodeContent/NodeContent.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx index a7cb0d10d..1a1ee4554 100644 --- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx +++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.spec.tsx @@ -1,5 +1,5 @@ import { CustomLinkComponent } from '@stoplight/elements-core'; -import { render, screen, fireEvent } from '@testing-library/react'; +import { fireEvent, render, screen } from '@testing-library/react'; import * as React from 'react'; import { MemoryRouter } from 'react-router-dom';