Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Last active September 8, 2020 10:05
Show Gist options
  • Select an option

  • Save davidsharp/f3edb3e4c41af21062d78ebbb04ce283 to your computer and use it in GitHub Desktop.

Select an option

Save davidsharp/f3edb3e4c41af21062d78ebbb04ce283 to your computer and use it in GitHub Desktop.
quick node terminal script to get a shareable Chrome text highlight
#!/usr/bin/env node
const encode = text => `#:-:text=${encodeURI(shorten(text))}`
const shorten = text => {
const words = text.split(' ')
return words.length>10?
`${words.slice(0,5).join(' ')},${words.slice(words.length-5).join(' ')}`:
text
}
const input = process.argv[2]
if(input)return console.log(encode(input))
//else assume piped // can be used: pbpaste | node text-hl
const stdin = process.openStdin();
let data = "";
stdin.on('data', chunk=>data+=chunk);
stdin.on('close', ()=>console.log(encode(data.slice(0,-1))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment