Skip to content

Commit

Permalink
Add fallback: false option to disable the fallback (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinking authored and sindresorhus committed Dec 18, 2019
1 parent 10072b8 commit adf3b1c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare namespace terminalLink {
interface Options {
/**
Override the default fallback.
Override the default fallback. If false, the fallback will be disabled.
@default `${text} (${url})`
*/
fallback?: (text: string, url: string) => string;
fallback?: ((text: string, url: string) => string) | false;
}
}

Expand All @@ -14,7 +14,8 @@ declare const terminalLink: {
Create a clickable link in the terminal's stdout.
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda)
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`.
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`,
unless the fallback is disabled by setting the `fallback` option to `false`.
@param text - Text to linkify.
@param url - URL to link to.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const supportsHyperlinks = require('supports-hyperlinks');

const terminalLink = (text, url, {target = 'stdout', ...options} = {}) => {
if (!supportsHyperlinks[target]) {
// If the fallback has been explicitly disabled, don't modify the text itself.
if (options.fallback === false) {
return text;
}

return options.fallback ? options.fallback(text, url) : `${text} (\u200B${url}\u200B)`;
}

Expand Down
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ expectType<string>(
})
);

expectType<string>(
terminalLink('text', 'url', {
fallback: false
})
);

expectType<boolean>(terminalLink.isSupported);

// stderr
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ Type: `object`

##### fallback

Type: `Function`
Type: `Function | false`

Override the default fallback. The function receives the `text` and `url` as parameters and is expected to return a string.

If set to `false`, the fallback will be disabled when a terminal is unsupported.

### terminalLink.isSupported

Type: `boolean`
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ test('default fallback', t => {
t.is(actual, 'My Website (\u200Bhttps://sindresorhus.com\u200B)');
});

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

const actual = terminalLink('My Website', 'https://sindresorhus.com', {
fallback: false
});
console.log(actual);
t.is(actual, 'My Website');
});

test('stderr default fallback', t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
Expand Down

0 comments on commit adf3b1c

Please sign in to comment.