Vinzent03 / find-unlinked-files

Find files, which are nowhere linked, so they are maybe lost in your vault.
MIT License
293 stars 14 forks source link

【feature request】create files for broken links #34

Closed suliveevil closed 1 year ago

suliveevil commented 1 year ago

find-unlinked-files is my favorite obsidian-plugin! It will be even more awesome if we can create files for broken links.

Vinzent03 commented 1 year ago

What is wrong with the Find broken links command?

suliveevil commented 1 year ago

It's great but I have about 1000 broken links in broken links output, I have to click on these links one by one or learn how to use some script to create files for these broken links.

Vinzent03 commented 1 year ago

Oh, you mean to create the files of that broken links. Sorry, misunderstood you.

suliveevil commented 1 year ago

😂 In the forum I found a great script to do this, this function will be awesome integrated in find-unlinked-files

#!/usr/bin/env bash
# Create empty notes for anything linked

archive=${OBSIDIAN_ARCHIVE:-$HOME/Documents/GitHub/Obsidian}

ext=md
placeholder="#placeholder"

cd "$archive"

# note this is one long pipeline
grep --include '*.'$ext --exclude-dir '.obsidian' -orh '\[\[[^]]*]]' . |  # get all links with brackets
grep -oh '[^][]\{1,\}' | # remove the brackets
grep -oh '[^|]*' | # remove the aliases
sed 's/^ *//;s/ $//' | # trim spaces
sort | uniq | # make unique
while read -r file; # for each expected file
do
    if [[ "$file" == *../* ]]  # if it contains a parent ../ link in it anywhere we can't process it
    then
        echo "'[[$file]]' has a parent directory in its path and will not be processed"
    elif ! find . -ipath "*/$file.$ext*" | grep . > /dev/null # if it's not in the notes tree
    then
        echo "$file.$ext does not exist and is being created"
        echo "# ${file##*/}" > "$file.$ext" # create with an H1 placeholder of its own name
        echo "$placeholder" >> "$file.$ext" # add any custom placeholder text, plus an eol
    fi
done
suliveevil commented 1 year ago

I was using this plugin for creating files, which is a little bit laggy but can create files to certain folder.

https://github.com/ivaneye/obsidian-files-cooker

Tested creating 1000 files using file-cooker found by find-unlinked-files.

I upgrade find-unlinked-files and found another 7 broken links 😂 the creating file function is great!

Vinzent03 commented 1 year ago

You can now set a whitelist of directories, which should allow you just that.