summaryrefslogtreecommitdiff
path: root/packages/md.source.sh
diff options
context:
space:
mode:
authorPaul Bütow <snonux@users.noreply.github.com>2021-05-21 05:12:35 +0100
committerGitHub <noreply@github.com>2021-05-21 05:12:35 +0100
commitb2d00167fa2df42808e2c95cba47d28279ce81cd (patch)
tree35b44d3a2795f8064e12562d69d226d128f79561 /packages/md.source.sh
parent7868506c413b453c8c1935f953a0e35a0b4c4ef9 (diff)
parent9dbd7f008192fd506bf642944232334fad0ed55c (diff)
Merge pull request #1 from snonux/master
Master
Diffstat (limited to 'packages/md.source.sh')
-rw-r--r--packages/md.source.sh63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/md.source.sh b/packages/md.source.sh
new file mode 100644
index 0000000..e7bfae1
--- /dev/null
+++ b/packages/md.source.sh
@@ -0,0 +1,63 @@
+# Make a Markdown image.
+md::make_img () {
+ local link="$1"; shift
+ local descr="$1"; shift
+
+ if [ -z "$descr" ]; then
+ echo "[![$link]($link)]($link) "
+ else
+ echo "[![$descr]($link \"$descr\")]($link) "
+ fi
+}
+
+# Make a Markdown hyperlink.
+md::make_link () {
+ local link="$1"; shift
+ local descr="$1"; shift
+
+ if ! $GREP -F -q '://' <<< "$link"; then
+ link=${link/.gmi/.md}
+ fi
+ if [[ -z "$descr" ]]; then
+ descr="$link"
+ fi
+
+ echo "[$descr]($link) "
+}
+
+# Convert Gemtext to Markdown.
+md::fromgmi () {
+ while IFS='' read -r line; do
+ case "$line" in
+ '=> '*)
+ generate::make_link md "$line"
+ ;;
+ *)
+ echo "$line"
+ ;;
+ esac
+ done
+}
+
+# Test the Markdown package.
+md::test () {
+ local line='=> https://example.org'
+ assert::equals "$(generate::make_link md "$line")" \
+ '[https://example.org](https://example.org) '
+
+ line='=> index.md'
+ assert::equals "$(generate::make_link md "$line")" \
+ '[index.md](index.md) '
+
+ line='=> http://example.org Description of the link'
+ assert::equals "$(generate::make_link md "$line")" \
+ '[Description of the link](http://example.org) '
+
+ line='=> http://example.org/image.png'
+ assert::equals "$(generate::make_link md "$line")" \
+ '[![http://example.org/image.png](http://example.org/image.png)](http://example.org/image.png) '
+
+ line='=> http://example.org/image.png Image description'
+ assert::equals "$(generate::make_link md "$line")" \
+ '[![Image description](http://example.org/image.png "Image description")](http://example.org/image.png) '
+}