Skip to content

Commit

Permalink
Add support for disabling the fallback (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinking authored and sindresorhus committed Dec 19, 2019
1 parent 42fd059 commit bc2fdaa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import terminalLink from 'terminal-link';

const Link = props => {
return (
<Text unstable__transformChildren={children => terminalLink(children, props.url)}>
<Text unstable__transformChildren={children => terminalLink(children, props.url, {fallback: props.fallback})}>
{props.children}
</Text>
);
Expand All @@ -16,11 +16,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
};

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.1.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Type: `string`

The URL to link to.

#### fallback

Type: `boolean`\
Default: `true`

Determines whether the URL should be printed in parens after the text for unsupported terminals: `My website (https://sindresorhus.com)`.

## Related

Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,29 @@ 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());
});

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

const {lastFrame} = render(
<Link fallback url="https://sindresorhus.com">
My Website
</Link>
);
console.log(lastFrame());
t.snapshot(lastFrame());
});
14 changes: 13 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,10 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
'My Website (https://sindresorhus.com)'
'My Website (​https://sindresorhus.com​)'

## include fallback if explicitly enabled

> Snapshot 1
'My Website (​https://sindresorhus.com​)'
Binary file modified test.js.snap
Binary file not shown.

0 comments on commit bc2fdaa

Please sign in to comment.