├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── browser.js ├── index.js ├── license ├── package.json ├── readme.md ├── test.js └── visual-test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [package.json] 15 | indent_style = space 16 | indent_size = 4 17 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | .nyc_output 4 | coverage 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | - '4' 6 | after_script: 7 | - './node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov' 8 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function () { 3 | throw new Error('Hyperlinker is not supported in the browser. Use the `supports-hyperlinks` module to avoid calling it'); 4 | }; 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* 4 | A hyperlink is opened upon encountering an OSC 8 escape sequence with the target URI. The syntax is 5 | 6 | OSC 8 ; params ; URI BEL|ST 7 | 8 | Following this, all subsequent cells that are painted are hyperlinks to this target. A hyperlink is closed with the same escape sequence, omitting the parameters and the URI but keeping the separators: 9 | 10 | OSC 8 ; ; BEL|ST 11 | 12 | const ST = '\u001B\\'; 13 | */ 14 | const OSC = '\u001B]'; 15 | const BEL = '\u0007'; 16 | const SEP = ';'; 17 | 18 | const PARAM_SEP = ':'; 19 | const EQ = '='; 20 | 21 | module.exports = (text, uri, params) => { 22 | params = params || {}; 23 | 24 | return [ 25 | OSC, 26 | '8', 27 | SEP, 28 | Object.keys(params).map(key => key + EQ + params[key]).join(PARAM_SEP), 29 | SEP, 30 | uri, 31 | BEL, 32 | text, 33 | OSC, 34 | '8', 35 | SEP, 36 | SEP, 37 | BEL 38 | ].join(''); 39 | }; 40 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) James Talmage (github.com/jamestalmage) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hyperlinker", 3 | "version": "1.0.0", 4 | "description": "Write hyperlinks in the terminal.", 5 | "license": "MIT", 6 | "repository": "jamestalmage/hyperlinker", 7 | "author": { 8 | "name": "James Talmage", 9 | "email": "james@talmage.io", 10 | "url": "github.com/jamestalmage" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && nyc ava" 17 | }, 18 | "files": [ 19 | "index.js", 20 | "browser.js" 21 | ], 22 | "keywords": [ 23 | "terminal", 24 | "link", 25 | "hyperlink", 26 | "cli" 27 | ], 28 | "browser": "browser.js", 29 | "dependencies": {}, 30 | "devDependencies": { 31 | "ava": "^0.20.0", 32 | "chalk": "^2.3.0", 33 | "codecov": "^2.2.0", 34 | "nyc": "^11.0.0", 35 | "xo": "^0.18.2" 36 | }, 37 | "nyc": { 38 | "reporter": [ 39 | "lcov", 40 | "text" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # hyperlinker [![Build Status](https://travis-ci.org/jamestalmage/hyperlinker.svg?branch=master)](https://travis-ci.org/jamestalmage/hyperlinker) [![codecov](https://codecov.io/gh/jamestalmage/hyperlinker/badge.svg?branch=master)](https://codecov.io/gh/jamestalmage/hyperlinker?branch=master) 2 | 3 | > Write hyperlinks in the terminal. 4 | 5 | Terminal emulators are [starting to support hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda). While many terminals have long detected URL's and linkified them, allowing you to Command-Click or Control-Click them to open a browser, you were forced to print the long unsightly URL's on the screen. As of spring 2017 [a few terminals](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) began supporting HTML like links, where the link text and destination could be specified separately. 6 | 7 | This module will create hyperlinks when printed to a supported terminal. 8 | 9 | ## Install 10 | 11 | ``` 12 | $ npm install hyperlinker 13 | ``` 14 | 15 | 16 | ## Usage 17 | 18 | ```js 19 | const hyperlinker = require('hyperlinker'); 20 | 21 | console.log(hyperlinker('some text', 'https://example.com') + ' <--- command + click here!'); 22 | // some text <-- command + click here! 23 | 24 | ``` 25 | 26 | Note that this module does not check to see if hyperlinks are supported in the current Terminal. In unsupported terminals, users will likely only see the `text` command. You should use module [`supports-hyperlinks`](https://github.com/jamestalmage/supports-hyperlinks) if you want to provide an alternate presentation based on Terminal support. 27 | 28 | ```js 29 | const supportsHyperlinks = require('supports-hyperlinks'); 30 | const hyperlinker = require('hyperlinker'); 31 | 32 | if (supportsHyperlinks.stdout) { 33 | console.log(hyperlinker('click here', 'https://example.com')); 34 | } else { 35 | console.log('Copy and paste the following in your browser: \n\t https://example.com'); 36 | } 37 | ``` 38 | 39 | ## API 40 | 41 | ### hyperlinker(text, uri, [params]) 42 | 43 | #### text 44 | 45 | Type: `string` 46 | 47 | The text that will be visible in the link. This is equivalent to the text between the opening `` and closing `` tags in HTML. 48 | 49 | #### uri 50 | 51 | Type: `string` 52 | 53 | A URI (i.e `https://example.com`) where the link will point to. This is equivalent to the context of the `href` attribute in an HTML `` tag. 54 | 55 | #### params 56 | 57 | Type: `Object`
58 | *Optional* 59 | 60 | A collection of key value pairs, that will be printed as hidden `params`. There's not a lot of use for these right now, except for maybe [an id param](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#hover-underlining-and-the-id-parameter). It is intended to allow extension of the spec in the future. 61 | 62 | ## Related 63 | 64 | * [`supports-hyperlinks`](https://github.com/jamestalmage/supports-hyperlinks): Detect hyperlink support for the running terminal. 65 | 66 | 67 | ## License 68 | 69 | MIT © [James Talmage](https://github.com/jamestalmage) 70 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test('simple link', t => { 5 | t.is(m('foo', 'example.com'), '\u001B]8;;example.com\u0007foo\u001B]8;;\u0007'); 6 | }); 7 | 8 | test('with id', t => { 9 | t.is(m('foo', 'example.com', {id: '3'}), '\u001B]8;id=3;example.com\u0007foo\u001B]8;;\u0007'); 10 | }); 11 | 12 | test('with multiple params', t => { 13 | t.is(m('foo', 'example.com', {id: '3', bar: 'baz'}), '\u001B]8;id=3:bar=baz;example.com\u0007foo\u001B]8;;\u0007'); 14 | }); 15 | -------------------------------------------------------------------------------- /visual-test.js: -------------------------------------------------------------------------------- 1 | // Just run "node ./visual-test" in a supported terminal to ensure things work. 2 | const chalk = require('chalk'); 3 | const link = require('./'); 4 | 5 | console.log('-------'); 6 | console.log(); 7 | console.log(link('example', 'http://example.com'), '<-- Try holding command and clicking here.'); 8 | console.log(); 9 | console.log(link('hyperlinker', 'https://github.com/jamestalmage/hyperlinker') + ' is awesome!'); 10 | console.log(); 11 | console.log(chalk.cyan([ 12 | 'Module ', 13 | link('hyperlinker', 'https://github.com/jamestalmage/hyperlinker'), 14 | 'works with', 15 | link('chalk', 'https://github.com/chalk/chalk') 16 | ].join(' '))); 17 | console.log(); 18 | console.log( 19 | 'Use the', 20 | link('id param', 'https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#hover-underlining-and-the-id-parameter', {id: 'foo'}), 21 | 'to visually connect\n two', 22 | link('separate links', 'https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#hover-underlining-and-the-id-parameter', {id: 'foo'}), 23 | ' (not widely supported).' 24 | ); 25 | console.log(); 26 | console.log(link( 27 | [ 28 | 'Link', 29 | link('within', 'https://github.com/jamestalmage/hyperlinker'), 30 | 'a link' 31 | ].join(' '), 32 | 'https://example.com' 33 | )); 34 | console.log(); 35 | console.log('---------'); 36 | --------------------------------------------------------------------------------