Zettelkasten Forum


[SOLVED] [REQUEST] Finding orphans

The thread about it’s a good idea to always link a new note from a structured one sparked an idea: It would be neat if The Archive could show me all notes that no other note is linking to. That would be a way to surface notes that risk disappear in the note collection.

Post edited by ctietze on

Comments

  • Oh yes! I'm slowly migrating a less-organized collection of notes and this would be a great time-saver.

  • I have a blog post draft and script lying around that does this. The Archive will one day get script support, so you can run that script in your archive for closer analysis. Until then, you have to invoke it manually.

    I'm going to run a couple of tests to verify it works properly, but if you want to give it a shot now, here's the current version. It's a ruby script. The configuration options are at the top.

    #!/usr/bin/env ruby
    
    # Change the path here:
    ARCHIVE = '~/Archive/'
    
    EXTENSIONS = %w{.md .txt .markdown .mdown .text}
    
    #################################################################
    
    def zettel_id(filename)
      filename[/^[0-9][0-9_\-]+[0-9]/]
    end
    
    INPUT_DIR = File.expand_path(ARCHIVE)
    files = Dir.entries(INPUT_DIR)
      .keep_if { |path| EXTENSIONS.include?(File.extname(path)) }
      .freeze
    
    identifiers = files.map { |f| zettel_id(f) }
    
    orphans = identifiers.dup.compact
    initial_count = orphans.count
    
    puts "Analyzing #{orphans.count} Zettel IDs ..."
    
    files.each do |file|
       content = File.read(File.join(INPUT_DIR, file))
       orphans.delete_if { |identifier| content.include?(identifier) }
    end
    
    zettel = identifiers.zip(files)
      .delete_if { |combo| combo[0].nil? }
      .to_h
      .freeze
    
    orphaned_files = orphans.map { |o| zettel[o] }
    score = (orphaned_files.count.to_f / initial_count.to_f).round(2)
    
    puts orphaned_files
    puts "----------------------"
    puts orphaned_files.count.to_s + " of #{initial_count} notes are orphans (score: #{score})"
    

    Author at Zettelkasten.de • https://christiantietze.de/

  • Great news this is something you are looking into. Will perhaps give this a shot, but I'm a bit out on deep water with Ruby scripts. :)

  • edited August 2018

    @thoresson Just a suggestion: If you're not comfortable with running a script, you can always open your archive with sublimeless_zk and use the built-in Find Notes by Number of Links to them. This will show all orphan notes by default. [edit: No it won't. You have to set maximum to 0]. You can also enter minimum and maximum number of links there:

    When you found your orphans, you can copy their ID and return back to the Archive.

    To make this functionality available with one click, you can enter the following into the saved searches box (of sublimeless_zk), as described here:

    unreferenced    : =refcounts(min:0, max:0) {sortby: mtime}
    
    most-referenced : =refcounts(min:1, max:1000) {sortby: refcount, order: desc}
    

    The above lines will become clickable, showing you orphan and most linked-to notes.

    Maybe that's more convenient for you than switching to the command line.

  • Thanks @rene, will give it a try!

  • Stumbled upon this through the search and want to leave a pointer to the thread "Quality control: finding orphaned Zettel" from 2020: https://forum.zettelkasten.de/discussion/comment/5702

    Am also closing this request because scriptability will cover this all.

    Author at Zettelkasten.de • https://christiantietze.de/

This discussion has been closed.