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

feat(addon-notes): use @storybook/router <Link> to render links in notes #6398

Merged
merged 5 commits into from
Apr 26, 2019
Merged
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
1 change: 1 addition & 0 deletions addons/notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@storybook/client-logger": "5.1.0-alpha.34",
"@storybook/components": "5.1.0-alpha.34",
"@storybook/core-events": "5.1.0-alpha.34",
"@storybook/router": "5.1.0-alpha.34",
"@storybook/theming": "5.1.0-alpha.34",
"core-js": "^2.6.5",
"global": "^4.3.2",
Expand Down
26 changes: 24 additions & 2 deletions addons/notes/src/Panel.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import { Link } from '@reach/router';
import { SyntaxHighlighter as SyntaxHighlighterBase } from '@storybook/components';
import { SyntaxHighlighter } from './Panel';
import { SyntaxHighlighter, NotesLink } from './Panel';

describe('NotesPanel', () => {
describe('SyntaxHighlighter component', () => {
Expand All @@ -20,4 +21,25 @@ describe('NotesPanel', () => {
expect(syntaxHighlighterBase.prop('language')).toBe('jsx');
});
});

describe('NotesLink component', () => {
it('should render storybook links with @storybook/router Link', () => {
const component = mount(
<NotesLink href="/story/addon-notes" title="title">
Storybook Link
</NotesLink>
);
expect(component.find(Link).prop('to')).toBe('/?path=/story/addon-notes');
expect(component.find(Link).prop('title')).toBe('title');
});
it('should render absolute links as <a>', () => {
const component = mount(
<NotesLink href="https://example.com" title="title">
Storybook Link
</NotesLink>
);
expect(component.find('a').prop('href')).toBe('https://example.com');
expect(component.find('a').prop('title')).toBe('title');
});
});
});
24 changes: 24 additions & 0 deletions addons/notes/src/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ReactElement, Fragment, ReactNode } from 'react';
import { types } from '@storybook/addons';
import { API, Consumer, Combo } from '@storybook/api';
import { Link as RouterLink } from '@storybook/router';
import { styled } from '@storybook/theming';

import {
Expand Down Expand Up @@ -66,11 +67,34 @@ export const SyntaxHighlighter = ({ className, children, ...props }: SyntaxHighl
);
};

interface NotesLinkProps {
href: string;
children: ReactElement;
}
export const NotesLink = ({ href, children, ...props }: NotesLinkProps) => {
/* https://github.com/sindresorhus/is-absolute-url/blob/master/index.js */
const isAbsoluteUrl = /^[a-z][a-z0-9+.-]*:/.test(href);
if (isAbsoluteUrl) {
return (
<a href={href} {...props}>
{children}
</a>
);
}

return (
<RouterLink to={href} {...props}>
{children}
</RouterLink>
);
};

// use our SyntaxHighlighter component in place of a <code> element when
// converting markdown to react elements
const defaultOptions = {
overrides: {
code: SyntaxHighlighter,
a: NotesLink,
Giphy: {
component: Giphy,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ storiesOf('Addon|Notes', module)
() => ({
Component: ButtonView,
}),
{ notes: 'My notes on the ButtonView component' }
{ notes: 'My notes on the [ButtonView](/story/addon-notes--simple-note) component' }
)
.add(
'Note with HTML',
Expand Down