Skip to content

Instantly share code, notes, and snippets.

@JohnAtl
Created February 7, 2020 20:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JohnAtl/6f719f0249c881a5f7ff4c465909ae42 to your computer and use it in GitHub Desktop.
Save JohnAtl/6f719f0249c881a5f7ff4c465909ae42 to your computer and use it in GitHub Desktop.
Code to help with note-link-janitor
#!/usr/bin/ruby
#
# Inserts the file's name as the first header in the file, if it isn't already there.
# Assumes filename begins with a 14 digit zettelkasten ID.
# Original file saved as filename.bak.
#
# As always, caveat emptor. This code could harm your files, so try it on a backup.
#
Dir.glob('*.md') do |filename|
puts "working on: #{filename}"
id_text = nil
line_cnt = 0
should_add = false
File.open('temp.txt', 'w') do |temp|
File.foreach(filename) do |line|
line_cnt += 1
if line_cnt == 1
if line !~ /# \d{14} .+/
temp.puts("# " + filename + "\n\n")
end
if line =~ /(# \d{14} .+)\.md/
line = $1
end
end
temp.puts(line)
end
end
File.rename(filename, filename + '.bak')
File.rename('temp.txt', filename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment