Skip to content

Commit

Permalink
[scripts] ,linkify: add support for Teams links
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Dec 23, 2023
1 parent 1b7bc4b commit fcabd5d
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions scripts/.local/bin/random/,linkify
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ url="$1"
[ -z "$url" ] && url="$(pbpaste)"

if ! echo "$url" | grep -qE '^https?://'; then
echo Not a link
exit 1
echo Not a link
exit 1
fi

get_title() {
curl -s "$1" | pup 'title text{}'
curl -s "$1" | pup 'title text{}'
}

link="$(
if echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/issues'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/issues/\([0-9]*\)|[\1#\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/pull'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/pull/\([0-9]*\)|[\1#\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/commit'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/commit/\([0-9a-z]\{7\}\).*|[\1\@\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/?$'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/$]*\)|[\1]($url)|"
elif echo "$url" | grep -qE '^https://linear.app/[^/]*/issue/[^/]*/[^/]*$'; then
printf "%s" "$url" | sed "s|https://linear.app/\([^/]*\)/issue/\([^/]*\)/\([^/]*\)|[linear:\1#\2]($url)|"
else
title="$(get_title "$url")"
[ -z "$title" ] && title="$url"
printf "[%s](%s)" "$title" "$url"
fi
)"

printf "%s" "$link" | pbcopy
echo "$link"
if echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/issues'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/issues/\([0-9]*\)|[\1#\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/pull'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/pull/\([0-9]*\)|[\1#\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/commit'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/]*\)/commit/\([0-9a-z]\{7\}\).*|[\1\@\2]($url)|"
elif echo "$url" | grep -qE '^https://github.com/[^/]*/[^/]*/?$'; then
printf "%s" "$url" | sed "s|https://github.com/\([^/]*/[^/$]*\)|[\1]($url)|"
elif echo "$url" | grep -qE '^https://linear.app/[^/]*/issue/[^/]*/[^/]*$'; then
printf "%s" "$url" | sed "s|https://linear.app/\([^/]*\)/issue/\([^/]*\)/\([^/]*\)|[linear:\1#\2]($url)|"
elif echo "$url" | grep -qE '^https://teams.microsoft.com'; then
# Using sed here was causing issues
team="$(echo "$url" | grep -o 'teamName=[^&]*' | cut -d= -f2)"
channel="$(echo "$url" | grep -o 'channelName=[^&]*' | cut -d= -f2)"
printf "[Teams message in %s](%s)" "$team#$channel" "$url"
else
title="$(get_title "$url")"
[ -z "$title" ] && title="$url"
printf "[%s](%s)" "$title" "$url"
fi | tee /dev/stderr | pbcopy

0 comments on commit fcabd5d

Please sign in to comment.