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

Add support for disabling the fallback #5

Merged
merged 5 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import PropTypes from 'prop-types';
import terminalLink from 'terminal-link';

const Link = props => {
const options = {
fallback: (text, url) => {
return props.fallback ? `${text} (\u200B${url}\u200B)` : text;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to open a PR to terminal-link to export the default fallback function, if we want to avoid duplicating it here.

Copy link
Owner

Choose a reason for hiding this comment

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

I would make terminal-link support {fallback: false} to disable the fallback. Then we don’t need to export it. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Down for that, too! Can open a PR in a sec.

Copy link
Owner

Choose a reason for hiding this comment

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

@colinking Friendly bump :)

Copy link
Contributor Author

@colinking colinking Dec 17, 2019

Choose a reason for hiding this comment

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

Thanks for the bump @sindresorhus! Opened a PR here: sindresorhus/terminal-link#13

I'll update this PR when a new version of terminal-link is released :)

}
};

return (
<Text unstable__transformChildren={children => terminalLink(children, props.url)}>
<Text unstable__transformChildren={children => terminalLink(children, props.url, options)}>
{props.children}
</Text>
);
Expand All @@ -16,11 +22,13 @@ Link.propTypes = {
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired,
url: PropTypes.string
url: PropTypes.string,
fallback: PropTypes.bool
};

Link.defaultProps = {
url: ''
url: '',
fallback: true
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
};

module.exports = Link;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
],
"dependencies": {
"prop-types": "^15.7.2",
"terminal-link": "^1.2.0"
"terminal-link": "^2.0.0"
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Type: `string`

The URL to link to.

#### fallback

Type: `boolean`

For unsupported terminals, `fallback` determines whether the URL should be printed in parens after the text: `My website (https://sindresorhus.com)`. Defaults to `true`.

## Related

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ test('render fallback', t => {
console.log(lastFrame());
t.snapshot(lastFrame());
});

test('exclude fallback if disabled', t => {
process.env.FORCE_HYPERLINK = 0;
const Link = require('.');

const {lastFrame} = render(
<Link url="https://sindresorhus.com" fallback={false}>
My Website
</Link>
);
console.log(lastFrame());
t.snapshot(lastFrame());
});
8 changes: 7 additions & 1 deletion test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).

## exclude fallback if disabled

> Snapshot 1

'My Website'

## render

> Snapshot 1
Expand All @@ -14,4 +20,4 @@ Generated by [AVA](https://ava.li).

> Snapshot 1

'My Website (https://sindresorhus.com)'
'My Website (https://sindresorhus.com)'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

adds zero-width spaces, after bumping to terminal-link@2.0.0

Binary file modified test.js.snap
Binary file not shown.