Last active
October 14, 2022 20:39
-
-
Save pnovotnak/f6d7192d02bb9ca847484840026e5076 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 -e | |
set -o pipefail | |
# This should be run within a folder for the repsitory within the obsidian vault. It will create | |
# symlinks to every file linked in the root markdown file provided within this directory. | |
# | |
# Links must be relative! | |
obsidian_deeplink_readme() { | |
# The root file. Typically README.md | |
local root_markdown="$1" | |
local root_markdown_path="$(dirname "$root_markdown")" | |
local linked_files=0 | |
# Print unsupported links | |
if rg --with-filename --pcre2 '\[[^\]]+\]\(((?=/|\.)[^\)]+)\)' "$root_markdown"; then | |
cat <<EOF | |
^^ these links are unsupported and will be skipped ^^ | |
See: | |
- https://forum.obsidian.md/t/enable-use-of-hidden-files-dotfiles-within-obsidian/26908 | |
Additionally, absolute links do not work properly. | |
EOF | |
fi | |
# Link the markdown file itself | |
ln -f "$root_markdown" "$(basename "$root_markdown")" | |
((linked_files=linked_files+1)) | |
# Find all file links in the markdown | |
while read relative_file_path; do | |
external_path="$root_markdown_path/$relative_file_path" | |
if [[ -f "$external_path" ]]; then | |
# echo "linking file $external_path -> $relative_file_path" | |
mkdir -p "$(dirname "$relative_file_path")" | |
ln -f "$external_path" "$relative_file_path" | |
((linked_files=linked_files+1)) | |
fi | |
done < <(rg --pcre2 '\[[^\]]+\]\(((?!http|#|/)[^\)]+)\)' "$root_markdown" -Nor '$1') | |
echo "linked $linked_files files" | |
} | |
obsidian_deeplink_readme "${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment