Skip to content

Instantly share code, notes, and snippets.

@minthemiddle
Last active September 23, 2024 13:39
Show Gist options
  • Save minthemiddle/0cec547fcc630ad5b86b58d34ecba1bd to your computer and use it in GitHub Desktop.
Save minthemiddle/0cec547fcc630ad5b86b58d34ecba1bd to your computer and use it in GitHub Desktop.
#!/bin/bash
# @parameters file workspace
# Set locale to UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
# Get the current file and workspace paths
current_file="$1"
workspace="$2"
# Get the selected text from the clipboard
selected_text=$(pbpaste)
# Extract the timestamp from the selected text
timestamp=$(echo "$selected_text" | sed -n 's/^## \([0-9]\{10\}\).*/\1/p')
if [ -z "$timestamp" ]; then
echo "Error: No timestamp found in the selected text"
exit 1
fi
# Create the new file path
new_file_path="$workspace/_Posteingang/$timestamp.md"
# Extract the content, replace ## with # for the first heading
extracted_content=$(echo "$selected_text" | sed '1s/^## /# /')
# Save the extracted content to the new file with UTF-8 encoding
echo "$extracted_content" | iconv -f UTF-8 -t UTF-8 > "$new_file_path"
# Create the wiki link
first_line=$(echo "$selected_text" | sed -n '2p' | cut -c 1-16)
wiki_link="## $timestamp\n\n[[$timestamp]]"
# Copy the wiki link to clipboard
echo -e "$wiki_link" | iconv -f UTF-8 -t UTF-8 | pbcopy
# Notify the user
echo "Extracted note saved to: $new_file_path"
echo "Wiki link copied to clipboard. Please paste it in place of the selected text."
@minthemiddle
Copy link
Author

I had problems with non-ascii code in the initial script.
This one uses UTF-8 explicitely and works with non-ascii (like German Umlaut and emojis) super well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment