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

Added support for external links in sections #921

Merged
merged 15 commits into from Aug 27, 2018

Conversation

BTMPL
Copy link
Contributor

@BTMPL BTMPL commented Apr 4, 2018

This PR adds support for external links in the sidebar (for example to link to external documentation or more extensive demos).

The two added flags are:

section.external - for the link
section.skip - skips rendering of the section in the main content (for both external links and logical grouping in the sidebar only)

Copy link
Member

@sapegin sapegin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request, that’s a nice feature!

Couple of comments below.

@@ -110,6 +110,8 @@ Each section consists of (all fields are optional):
* `sections` — array of subsections (can be nested).
* `description` — A small description of this section.
* `ignore` — string/array of globs that should not be included in the section.
* `external` - an URL to be opened (in a new tab) instead of navigating to the section content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe href would be more clear. external feels like a boolean flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with href is that ComponentsListRenderer already has a prop called href, and we can't easily use it to determine if it should be opened in new window or not because the href can be generated as a fully qualified URL for sections with absolute = true.

Should I try to figure this out or use externalHref or some other field?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would make this part even more clear:

href: item.href || getUrl({ ...
external: !!item.href

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to use the same API for users and developers ;-)

},
{
name: 'Live Demo',
skip: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t skip be implied for external links?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so basically remove the "skip" and just have the renderer skip external sections ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would make the API easier to use.

{ origin, pathname } = window.location
) {
let url = pathname;

if (external) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move it before let url….

@@ -23,3 +23,13 @@ it('should compose passed class names', () => {

expect(actual.find('a').prop('className')).toBe('baseLinkClass customClass');
});

it('should open external links in new window', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’re not really testing that it opens a link in a new window ;-) “Should render external links with _blank target” would be more clear.

@codecov-io
Copy link

codecov-io commented Apr 4, 2018

Codecov Report

Merging #921 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted Files Coverage Δ
loaders/utils/getSections.js 100% <ø> (ø) ⬆️
...omponents/ComponentsList/ComponentsListRenderer.js 100% <100%> (ø) ⬆️
src/rsg-components/Sections/Sections.js 100% <100%> (ø) ⬆️
...rc/rsg-components/ComponentsList/ComponentsList.js 100% <100%> (ø) ⬆️
src/rsg-components/Argument/ArgumentRenderer.js 100% <0%> (ø) ⬆️
loaders/utils/getProps.js 100% <0%> (ø) ⬆️

@BTMPL
Copy link
Contributor Author

BTMPL commented Apr 9, 2018

Hello @sapegin - I've update as per your recommendation

@BTMPL
Copy link
Contributor Author

BTMPL commented Apr 9, 2018

I see that the coverage is lowered - please let me know if I should provide additional tests.

Copy link
Member

@sapegin sapegin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is very very close to done, jus a few comments ;-)

About tests — this branch isn't tested:

href: item.href
  ? item.href

anchor: !useIsolatedLinks,
isolated: useIsolatedLinks,
}),
target: item.href ? '_blank' : undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

external would be better, _blank is an implementation detail, we shouldn't know about that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to use external string instead of _blank? If so, I'm not sure about that one. _blank is guaranteed to open a new tab every time, if we use and external multiple links will open in the same window.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean making an API that isn't tied to implementation: external: true and then:

target={item.external ? '_blank' : undefined}

@@ -6,7 +6,9 @@ import SectionsRenderer from 'rsg-components/Sections/SectionsRenderer';
export default function Sections({ sections, depth }) {
return (
<SectionsRenderer>
{sections.map((section, idx) => <Section key={idx} section={section} depth={depth} />)}
{sections
.filter(section => !section.href)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case for sections without href?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently any section added to the configuration will result in a section space being generated in the main content - this would also include the href sections which should open in new window.

This will filter out those sections from being rendered in the main content area.

{sections.map((section, idx) => <Section key={idx} section={section} depth={depth} />)}
{sections
.filter(section => !section.href)
.map((section, idx) => <Section key={idx} section={section} depth={depth} />)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can improve key={idx} now? Is there anything unique for each section that we can use as a key instead of an index?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont thinks so, unless we assume that all sections have unique hrefs, which they might now have, so I guess we are stuck with the index for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then title + href should be unique enough, I'd really like to avoid indexes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that some sections do not have slug, but are rendered with an array of components - this is for example visible in sections example, WrappedButton link.

docs/Cookbook.md Outdated
@@ -119,7 +119,7 @@ export default icons

````jsx
// IconGallery.md
```jsx noeditor
;```jsx noeditor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's going on here; this looks to be changed by a pre-commit hook, and probably should?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the file was edited in the browser so the precommit hook wasn't run ;-/ Could you please change jsx to markdown for the whole block? It'll stop Prettier from adding ;.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume jsx static won't work here? Updating.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit confusing: here's an example of what you should write in Markdown, so the outer tag should be markdown instead of jsx. jsx noeditor part is fine — it's part of an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was thinking ````jsx static would work in order to keep highlights. Should be like you asked now; I think the newline was added by precommit hook again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line is fine!

@sapegin sapegin added this to the 7.3.0 milestone Aug 14, 2018
@bluetidepro bluetidepro merged commit 54ab94a into styleguidist:master Aug 27, 2018
bluetidepro added a commit that referenced this pull request Aug 27, 2018
👋 **[Support Styleguidist](https://opencollective.com/styleguidist) on Open Collective** 👋

## New features

* Added support for external links in sections ([#921](#921) by @BTMPL)

Two flags added to `Sections` config:

* `href` - An URL to navigate to instead of navigating to the section content
* `external` - If set, the link will open in a new window
@styleguidist-bot
Copy link
Collaborator

🎉 This PR is included in version 7.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

artem0723 pushed a commit to artem0723/React-Styleguidist that referenced this pull request Oct 28, 2021
👋 **[Support Styleguidist](https://opencollective.com/styleguidist) on Open Collective** 👋

## New features

* Added support for external links in sections ([#921](styleguidist/react-styleguidist#921) by @BTMPL)

Two flags added to `Sections` config:

* `href` - An URL to navigate to instead of navigating to the section content
* `external` - If set, the link will open in a new window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants