Last active
January 28, 2023 01:28
-
-
Save shazow/0a9f4e0afc9f5e7b85401ab0bf8b322e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env nix-shell | |
#!nix-shell -i bash -p jq curl | |
set -e | |
prefix() { | |
declare token="${1}" | |
while read line; do | |
echo "${token}${line}" | |
done | |
} | |
urlnormalize() { | |
# Strip scheme and trailing slash | |
sed -e 's|^\w*://||g' | sed -e 's|/$||g' | |
} | |
urlencode() { | |
curl -s -w "%{url_effective}" --get --data-urlencode "@-" "example.com" | cut -d '?' -f2- | |
} | |
render() { | |
declare line=$1 | |
declare url=$(echo $line | jq -j -r .href); | |
declare title=$(echo $line | jq -j -r .description); | |
declare description=$(echo $line | jq -j -r .extended); | |
declare timestamp=$(echo $line | jq -j -r .time); | |
declare tags=$(echo $line | jq -j -r .tags | sed 's|\s| #|g'); | |
echo "# $title" | |
echo "" | |
echo "Link: $url" | |
echo "Timestamp: $timestamp" | |
if [[ "$tags" ]]; then | |
echo "Tags: #$tags" | |
else | |
"Tags: " | |
fi | |
echo "" | |
if [[ "$description" ]]; then | |
echo "## Description" | |
echo "$description" | sed 's|<.\?blockquote>||g' | prefix "> " | |
echo "" | |
fi | |
} | |
jq -c .[] $1 | while read line; do | |
declare path="$(echo $line | jq -j -r .href | urlnormalize | urlencode).md" | |
if [[ -f "$path" ]]; then | |
echo "Skipping: $path" | |
continue | |
fi | |
echo "Converting: $path" | |
render "$line" > $path | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment