Skip to content

Commit

Permalink
fix(elements-core): article component should not require Router (stop…
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Apr 3, 2023
1 parent e92ec42 commit c254cc8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/elements-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-core",
"version": "7.7.14",
"version": "7.7.15",
"sideEffects": [
"web-components.min.js",
"src/web-components/**",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { screen } from '@testing-library/dom';
import { render } from '@testing-library/react';
import { flow } from 'lodash';
import * as React from 'react';

import { withPersistenceBoundary } from '../../../context/Persistence';
import { withMosaicProvider } from '../../../hoc/withMosaicProvider';
import { withRouter } from '../../../hoc/withRouter';
import type { RoutingProps } from '../../../types';
import { Article as _Article } from './index';

const Article = withMosaicProvider(withPersistenceBoundary(_Article));

const ArticleWithRouter = flow(
withRouter,
withPersistenceBoundary,
withMosaicProvider,
)((props: RoutingProps & { data: string }) => {
return <_Article data={props.data} />;
});

describe('Article', () => {
it('should not require any router', () => {
const { unmount } = render(<Article data="## Hey there" />);

expect(screen.getByText(/Hey there/i)).toBeInTheDocument();

unmount();
});

it('given hash router, should have correct links', () => {
location.hash = '#/test';
const { unmount } = render(<ArticleWithRouter router="hash" data="[abc](#abc)" />);

expect(screen.getByText(/abc/i)).toHaveAttribute('href', '#/test#abc');

unmount();
});
});
23 changes: 18 additions & 5 deletions packages/elements-core/src/components/Docs/Article/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@ import { withErrorBoundary } from '@stoplight/react-error-boundary';
import * as React from 'react';
import { useLocation } from 'react-router-dom';

import { RouterTypeContext } from '../../../context/RouterType';
import { useRouterType } from '../../../context/RouterType';
import { MarkdownViewer } from '../../MarkdownViewer';
import { DocsComponentProps } from '..';

type ArticleProps = DocsComponentProps<IMarkdownViewerProps['markdown']>;

const ArticleComponent = React.memo<ArticleProps>(({ data }) => {
const ArticleComponentForHashRouter = React.memo<ArticleProps>(({ data }) => {
const { pathname } = useLocation();
const routerKind = React.useContext(RouterTypeContext);
const basePath = routerKind === 'hash' ? `#${pathname.split('#')[0]}` : '';
const basePath = `#${pathname.split('#')[0]}`;

return <BaseArticleComponent data={data} tocBasePath={basePath} />;
});

const BaseArticleComponent = React.memo<ArticleProps & { tocBasePath?: string }>(({ data, ...props }) => {
return (
<Box className="sl-elements-article">
<MarkdownViewer className="sl-elements-article-content" markdown={data} includeToc tocBasePath={basePath} />
<MarkdownViewer className="sl-elements-article-content" markdown={data} includeToc {...props} />
</Box>
);
});

const ArticleComponent = React.memo<ArticleProps>(({ data }) => {
const routerKind = useRouterType();

if (routerKind === 'hash') {
return <ArticleComponentForHashRouter data={data} />;
}

return <BaseArticleComponent data={data} />;
});

export const Article = withErrorBoundary<ArticleProps>(ArticleComponent, { recoverableProps: ['data'] });
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
]
},
"dependencies": {
"@stoplight/elements-core": "~7.7.14",
"@stoplight/elements-core": "~7.7.15",
"@stoplight/markdown-viewer": "^5.5.0",
"@stoplight/mosaic": "^1.33.0",
"@stoplight/path": "^1.3.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements",
"version": "7.7.14",
"version": "7.7.15",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down Expand Up @@ -62,7 +62,7 @@
]
},
"dependencies": {
"@stoplight/elements-core": "~7.7.14",
"@stoplight/elements-core": "~7.7.15",
"@stoplight/http-spec": "^5.1.4",
"@stoplight/json": "^3.18.1",
"@stoplight/mosaic": "^1.33.0",
Expand Down

0 comments on commit c254cc8

Please sign in to comment.