Last active
September 1, 2021 19:58
-
-
Save cesalazar/0edeb4ce7daf173f864a1cbef4d87fde to your computer and use it in GitHub Desktop.
Create symlinks inside of an Obsidian vault, pointing to files located outside of the vault
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
#!/bin/bash | |
# | |
# Create inside of an Obsidian vault symlinks to files located elsewhere | |
# Debug options | |
# set -euxo pipefail | |
# Obsidian vault where the symlinks will be created | |
vault=${HOME}/Sync/obsidian/obsidian-vault/no-sync | |
show_msg() { | |
local message=${*} | |
printf "%b\n" "${message}" | |
} | |
show_help() { | |
show_msg """ | |
coln - create symlinks to an Obsidian vault | |
Usage: | |
coln [files...] | |
Example: | |
coln filename.md path/to/another/file.ext file_without_extension | |
""" | sed -E 's/^\s{,4}//' | |
} | |
# Remove leading dots to make a path or file visible in Obsidian | |
set_unhidden() { | |
echo "${1}" | sed -E "s/\/\./\//g;s/^\.//" | |
} | |
# Exit if no arguments was given, or is asking for help | |
[[ -z $* || $1 == -? || $1 == -h || $1 == --help ]] && show_help && exit 0 | |
# Path of the folder where this script is being executed | |
current_folder=$(pwd -P) | |
# Use the last segment in the path as the name for a new folder in the vault | |
# to prevent collisions in the filenames | |
new_folder=$(set_unhidden "${current_folder##*/}") | |
# echo $new_folder | |
# Path where the symlinks will be created | |
target_folder=${vault}/${new_folder} | |
symlinks_created=0 | |
# Create the folder inside of the vault if doesn't exist | |
[[ -d "${target_folder}" ]] || mkdir -p "${target_folder}" | |
create_symlink() { | |
local file_full_path=${*} | |
local path=${file_full_path%/*} | |
local filename=${file_full_path##*/} | |
local symlink_path=${target_folder}/${path} | |
# Remove leading dots to prevent it being hidden | |
symlink_path=$(set_unhidden "${symlink_path}") | |
# Does the argument contains folders? | |
if [[ ${file_full_path} == *"/"* ]]; then | |
# Then create in the vault the folder structure | |
mkdir -p "${symlink_path}" | |
else | |
# Except if it's a regular file | |
[[ -f ${path} ]] && symlink_path=${target_folder} | |
fi | |
# Always add a .md extension to the symlink, otherwise Obsidian ignores it | |
local symlink=${symlink_path}/${filename%%.md}.md | |
# Remove leading dots to prevent it being hidden | |
symlink=$(set_unhidden "${symlink}") | |
# Create the symlink, removing leading dots to prevent it from being hidden | |
ln -sf "${current_folder}/${file_full_path}" "${symlink}" | |
return $? | |
} | |
for arg in "${@}"; do | |
file=${current_folder}/${arg} | |
error="" | |
[[ -d "${file}" ]] && error="is a directory" | |
[[ ! -f "${file}" && ! ${error} ]] && error="does not exist" | |
if [[ ${error} ]]; then | |
show_msg "Error: ${arg} ${error}, ignoring it" | |
else | |
create_symlink "${arg}" && ((symlinks_created++)) | |
fi | |
done | |
[[ ${symlinks_created} == 0 ]] && show_msg "No symlink created" && exit 1 | |
show_msg "Created ${symlinks_created} symlinks in ${target_folder}" | |
# vim: fdm=manual tabstop=4 softtabstop=4 shiftwidth=4 expandtab: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment