Shortlinks tool updates with new shortlinks

This commit is contained in:
JP Hastings-Spital 2024-07-16 17:17:59 +01:00
parent bc0b882242
commit 49fedda65e
4 changed files with 46 additions and 17 deletions

View file

@ -10,6 +10,7 @@ tags:
topics:
- IndieWeb
summary: Archiving my Instagram account, and releasing the code that imported it to this Hugo-driven site.
shortlink: insta
---
I spent a little time this weekend reminding myself of what writing code is like (before I start work again on Tuesday, after [4 months travelling](https://adventure.awaits.us)). After my recent post on [leaving Meta's social networks](/posts/goodbye-big-social) I decided to create a script to convert the GDPR-provided archive of all my activity on Instagram into this blog.

View file

@ -35,8 +35,8 @@ Things I might work on within my personal blog's software.
- [x] Create suitable `_redirects` files
- [x] Import pre-existing shortlinks
- [x] Add shortlink as `<link rel="shortlink">` on all relevant pages
- [x] Add additional logic to _combine_ the redirects files as needed
- [ ] Upload new redirects to byjp.fyi repo
- [ ] Add additional logic to _combine_ the redirects files as needed
### Done ✓

View file

@ -1,16 +0,0 @@
#!/bin/bash
rm **/_redirects
while IFS=$'\n' read -r line
do
# Extract the absolute file path
outdir=$(dirname $(echo "$line" | cut -d " " -f1) | cut -d "/" -f2)
if [[ -n "$outdir" ]]; then
[[ ! -d $outdir ]] && mkdir -p $outdir
echo $line | cut -c $((${#outdir}+2))- >> "./$outdir/_redirects"
else
echo $line >> "_redirects"
fi
done < "${1:-/dev/stdin}"

View file

@ -0,0 +1,44 @@
#!/bin/bash
# Usage:
# 1. Build www.byjp.me
# 2. Clone and enter a local copy of the byjp.fyi repo
# 3. `cat www.byjp.me/public/index.redirects | www.byjp.me/tools/redirects/update-shortlinks.bash``
# 4. `git commit -am "Update blog redirects" && git push`
# This script assumes it is being executed at the root of the byjp.fyi repo
gitRoot=$(git rev-parse --show-toplevel) || { echo "Please run inside the byjp.fyi git repo"; exit 1; }
if [[ "$(git remote get-url origin)" != "https://github.com/by-jp/byjp.fyi.git" ]]; then
echo "This repo doesn't have the byjp.fyi repo as the origin"
exit 2
fi
cd $gitRoot
# Prepare existing files for new _redirects info
mv public/_redirects public/_redirects.previous
find public -type f -iname "_redirects" -delete
# Create local _redirects files
while IFS=$'\n' read -r line
do
# Extract the absolute file path
outdir=$(dirname $(echo "$line" | cut -d " " -f1) | cut -d "/" -f2)
if [[ -n "$outdir" ]]; then
[[ ! -d $outdir ]] && mkdir -p "public/$outdir"
echo $line | cut -c $((${#outdir}+2))- >> "public/$outdir/_redirects"
else
echo $line >> "public/_redirects"
fi
done < "${1:-/dev/stdin}"
# Combine new (root) _redirects and _redirects.previous
start_line=$(awk '/^# From blog/{print NR; exit}' public/_redirects.previous)
end_line=$(awk 'NR > '"$start_line"' && /^#/{print NR; exit}' public/_redirects.previous)
sed "$((start_line + 1)),$((end_line - 2))d" public/_redirects.previous > old_removed.txt
awk 'NR=='"$((start_line + 1))"'{system("cat public/_redirects")} 1' old_removed.txt > public/_redirects.new
# Remove temporary files
mv public/_redirects.new public/_redirects
rm public/_redirects.previous
rm old_removed.txt