Skip to content

Commit 7fd874b

Browse files
authored
Merge pull request #2144 from tbhaxor/feature/url-plugin
2 parents ff12759 + 0805de5 commit 7fd874b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

clean_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ plugins/available/rbenv.plugin.bash
125125
plugins/available/ruby.plugin.bash
126126
plugins/available/textmate.plugin.bash
127127
plugins/available/todo.plugin.bash
128+
plugins/available/url.plugin.bash
128129
plugins/available/xterm.plugin.bash
129130
plugins/available/zoxide.plugin.bash
130131

plugins/available/url.plugin.bash

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# shellcheck shell=bash
2+
cite about-plugin
3+
about-plugin 'Basic url handling and manipulation functions'
4+
5+
function slugify() {
6+
about 'takes the text and transform to slug url, also supports formats like (html,link,rst,md)'
7+
group 'url'
8+
param "1: Text to transform (optional)"
9+
param "2: Output format (html,rst,link,md). Omit or pass any text to return only output"
10+
11+
local TXT=$1
12+
local OUTPUT=$2
13+
local SLUG
14+
15+
if [[ -z $TXT ]]; then
16+
read -rp "Enter the valid string: " TXT
17+
fi
18+
19+
# Pass 1 - Clean the url
20+
# Credits: https://stackoverflow.com/a/20007549/10362396
21+
SLUG=$(echo -n "$TXT" | tr -cd ' [:alnum:]._-' | tr -s ' ')
22+
23+
# Pass 2 - Transformation
24+
SLUG=$(echo -n "$SLUG" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
25+
26+
case "$OUTPUT" in
27+
html | htm)
28+
echo "<a href=\"#$SLUG\">$TXT</a>"
29+
;;
30+
href | link)
31+
echo "#$SLUG"
32+
;;
33+
md)
34+
echo "[$TXT](#$SLUG)"
35+
;;
36+
rst)
37+
echo "\`$TXT <#$SLUG>\`_"
38+
;;
39+
40+
*)
41+
echo "$SLUG"
42+
;;
43+
esac
44+
45+
}

0 commit comments

Comments
 (0)